473,802 Members | 2,374 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Impersonation

My goal is to upload/download files to a shared folder. I have been granted
a "generic" account to be used for this purpose. I have designed a page
which will do this download. My quandry is when the user access the page, I
retrieve their "User.Ident ity" and log activity to that user on this screen
to the Database. If I used impersonation in web config file, then I really
loose the true user's identify and can not really log there usage into the
system because the "generic" id is substituted.

I have been reading that I can use Impersonation via code for a portion of
the page. This looks like a solution to my problem but I seem to be limited
to the account that is actually using the application. Is there a way for me
to create a WindowsIdentity object with my generic account? Do you have an
example?
Nov 19 '05 #1
4 2455
Here's what you need:

using System;
using System.Security .Principal;
using System.Security .Permissions;
using System.Runtime. InteropServices ;
using System.Threadin g;

namespace Impersonate
{
/// <summary>
/// Summary description for ImpersonateUser .
/// </summary>
public class ImpersonateUser
{

[DllImport("adva pi32.dll", SetLastError=tr ue)]
private static extern bool LogonUser(strin g lpszUsername,
string lpszDomain,
string lpszPassword,
int dwLogonType,
int dwLogonProvider ,
ref IntPtr phToken);

[DllImport("kern el32.dll", CharSet=CharSet .Auto)]
private static extern bool CloseHandle(Int Ptr handle);

// constants used by LogonUser() method
private const int LOGON32_LOGON_N ETWORK = 3;
private const int LOGON32_PROVIDE R_DEFAULT = 0;

private WindowsImperson ationContext wic = null;
private WindowsIdentity currentIdentity = null;

public ImpersonateUser (string login, string password, string domain)
{
// Get current Identity
currentIdentity = WindowsIdentity .GetCurrent();
// handle returned from the LogonUser() method
IntPtr handle = new IntPtr(0);
handle = IntPtr.Zero;
// try to login to the domain
bool logonUser = LogonUser(login , domain, password,
LOGON32_LOGON_N ETWORK, LOGON32_PROVIDE R_DEFAULT, ref handle);
// login unsuccessful
if(!logonUser)
{
// get the error
int lastWin32Error = Marshal.GetLast Win32Error();
throw new Exception("Impe rsonateUser failed<br>Win32 Error: " +
lastWin32Error) ;
}
// create a new WindowsIdentity , set the CurrentPrincipa l and Impersonate
the user
WindowsIdentity wi
= new WindowsIdentity (handle, "NTLM", WindowsAccountT ype.Normal, true);
Thread.CurrentP rincipal = new WindowsPrincipa l(wi);
wic = wi.Impersonate( );
// close the handle
CloseHandle(han dle);
}

public void Undo()
{
// Impersonate back to original identity
wic.Undo();
Thread.CurrentP rincipal = new WindowsPrincipa l(currentIdenti ty);
currentIdentity .Impersonate();
}

}

}
"Jim Heavey" wrote:
My goal is to upload/download files to a shared folder. I have been granted
a "generic" account to be used for this purpose. I have designed a page
which will do this download. My quandry is when the user access the page, I
retrieve their "User.Ident ity" and log activity to that user on this screen
to the Database. If I used impersonation in web config file, then I really
loose the true user's identify and can not really log there usage into the
system because the "generic" id is substituted.

I have been reading that I can use Impersonation via code for a portion of
the page. This looks like a solution to my problem but I seem to be limited
to the account that is actually using the application. Is there a way for me
to create a WindowsIdentity object with my generic account? Do you have an
example?

Nov 19 '05 #2
Did I not read that this will only work on Windows XP and will not work on
Windows 2000?....
ms-help://MS.VSCC.2003/MS.MSDNQTR.2003 FEB.1033/cpref/html/frlrfSystemSecu rityPrincipalWi ndowsIdentityCl assImpersonateT opic.htm

"Kevin Schlegelmilch" wrote:
Here's what you need:

using System;
using System.Security .Principal;
using System.Security .Permissions;
using System.Runtime. InteropServices ;
using System.Threadin g;

namespace Impersonate
{
/// <summary>
/// Summary description for ImpersonateUser .
/// </summary>
public class ImpersonateUser
{

[DllImport("adva pi32.dll", SetLastError=tr ue)]
private static extern bool LogonUser(strin g lpszUsername,
string lpszDomain,
string lpszPassword,
int dwLogonType,
int dwLogonProvider ,
ref IntPtr phToken);

[DllImport("kern el32.dll", CharSet=CharSet .Auto)]
private static extern bool CloseHandle(Int Ptr handle);

// constants used by LogonUser() method
private const int LOGON32_LOGON_N ETWORK = 3;
private const int LOGON32_PROVIDE R_DEFAULT = 0;

private WindowsImperson ationContext wic = null;
private WindowsIdentity currentIdentity = null;

public ImpersonateUser (string login, string password, string domain)
{
// Get current Identity
currentIdentity = WindowsIdentity .GetCurrent();
// handle returned from the LogonUser() method
IntPtr handle = new IntPtr(0);
handle = IntPtr.Zero;
// try to login to the domain
bool logonUser = LogonUser(login , domain, password,
LOGON32_LOGON_N ETWORK, LOGON32_PROVIDE R_DEFAULT, ref handle);
// login unsuccessful
if(!logonUser)
{
// get the error
int lastWin32Error = Marshal.GetLast Win32Error();
throw new Exception("Impe rsonateUser failed<br>Win32 Error: " +
lastWin32Error) ;
}
// create a new WindowsIdentity , set the CurrentPrincipa l and Impersonate
the user
WindowsIdentity wi
= new WindowsIdentity (handle, "NTLM", WindowsAccountT ype.Normal, true);
Thread.CurrentP rincipal = new WindowsPrincipa l(wi);
wic = wi.Impersonate( );
// close the handle
CloseHandle(han dle);
}

public void Undo()
{
// Impersonate back to original identity
wic.Undo();
Thread.CurrentP rincipal = new WindowsPrincipa l(currentIdenti ty);
currentIdentity .Impersonate();
}

}

}
"Jim Heavey" wrote:
My goal is to upload/download files to a shared folder. I have been granted
a "generic" account to be used for this purpose. I have designed a page
which will do this download. My quandry is when the user access the page, I
retrieve their "User.Ident ity" and log activity to that user on this screen
to the Database. If I used impersonation in web config file, then I really
loose the true user's identify and can not really log there usage into the
system because the "generic" id is substituted.

I have been reading that I can use Impersonation via code for a portion of
the page. This looks like a solution to my problem but I seem to be limited
to the account that is actually using the application. Is there a way for me
to create a WindowsIdentity object with my generic account? Do you have an
example?

Nov 19 '05 #3
I've run it on Windows 2000 and Windows 2003 and both worked for me ...

"Jim Heavey" wrote:
Did I not read that this will only work on Windows XP and will not work on
Windows 2000?....
ms-help://MS.VSCC.2003/MS.MSDNQTR.2003 FEB.1033/cpref/html/frlrfSystemSecu rityPrincipalWi ndowsIdentityCl assImpersonateT opic.htm

"Kevin Schlegelmilch" wrote:
Here's what you need:

using System;
using System.Security .Principal;
using System.Security .Permissions;
using System.Runtime. InteropServices ;
using System.Threadin g;

namespace Impersonate
{
/// <summary>
/// Summary description for ImpersonateUser .
/// </summary>
public class ImpersonateUser
{

[DllImport("adva pi32.dll", SetLastError=tr ue)]
private static extern bool LogonUser(strin g lpszUsername,
string lpszDomain,
string lpszPassword,
int dwLogonType,
int dwLogonProvider ,
ref IntPtr phToken);

[DllImport("kern el32.dll", CharSet=CharSet .Auto)]
private static extern bool CloseHandle(Int Ptr handle);

// constants used by LogonUser() method
private const int LOGON32_LOGON_N ETWORK = 3;
private const int LOGON32_PROVIDE R_DEFAULT = 0;

private WindowsImperson ationContext wic = null;
private WindowsIdentity currentIdentity = null;

public ImpersonateUser (string login, string password, string domain)
{
// Get current Identity
currentIdentity = WindowsIdentity .GetCurrent();
// handle returned from the LogonUser() method
IntPtr handle = new IntPtr(0);
handle = IntPtr.Zero;
// try to login to the domain
bool logonUser = LogonUser(login , domain, password,
LOGON32_LOGON_N ETWORK, LOGON32_PROVIDE R_DEFAULT, ref handle);
// login unsuccessful
if(!logonUser)
{
// get the error
int lastWin32Error = Marshal.GetLast Win32Error();
throw new Exception("Impe rsonateUser failed<br>Win32 Error: " +
lastWin32Error) ;
}
// create a new WindowsIdentity , set the CurrentPrincipa l and Impersonate
the user
WindowsIdentity wi
= new WindowsIdentity (handle, "NTLM", WindowsAccountT ype.Normal, true);
Thread.CurrentP rincipal = new WindowsPrincipa l(wi);
wic = wi.Impersonate( );
// close the handle
CloseHandle(han dle);
}

public void Undo()
{
// Impersonate back to original identity
wic.Undo();
Thread.CurrentP rincipal = new WindowsPrincipa l(currentIdenti ty);
currentIdentity .Impersonate();
}

}

}
"Jim Heavey" wrote:
My goal is to upload/download files to a shared folder. I have been granted
a "generic" account to be used for this purpose. I have designed a page
which will do this download. My quandry is when the user access the page, I
retrieve their "User.Ident ity" and log activity to that user on this screen
to the Database. If I used impersonation in web config file, then I really
loose the true user's identify and can not really log there usage into the
system because the "generic" id is substituted.

I have been reading that I can use Impersonation via code for a portion of
the page. This looks like a solution to my problem but I seem to be limited
to the account that is actually using the application. Is there a way for me
to create a WindowsIdentity object with my generic account? Do you have an
example?

Nov 19 '05 #4
On Thu, 22 Sep 2005 08:06:04 -0700, "Jim Heavey" <Ji*******@disc ussions.microso ft.com> wrote:

¤ My goal is to upload/download files to a shared folder. I have been granted
¤ a "generic" account to be used for this purpose. I have designed a page
¤ which will do this download. My quandry is when the user access the page, I
¤ retrieve their "User.Ident ity" and log activity to that user on this screen
¤ to the Database. If I used impersonation in web config file, then I really
¤ loose the true user's identify and can not really log there usage into the
¤ system because the "generic" id is substituted.
¤

Actually, it's just the opposite. If you implement impersonation the thread operates under the
credentials of the authenticated user (via NTLM), not ASPNET (or NetworkService) .
Paul
~~~~
Microsoft MVP (Visual Basic)
Nov 19 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
10014
by: Ripa Horatiu | last post by:
Does anyone knows how can I impersonate to another user (basically Administrator) for a piece of my code? I've tried the samples provided by MS but they didn't worked. -- Horatiu Ripa
12
2584
by: Anil Krishnamurthy | last post by:
We have an ASP.NET application that uses COM objects through Interop. The web application requires access to network and database resources and hence, needs to impersonate a domain account. The problem is that even when it is configured to run under a certain identity through Web.config, the impersonation is not carried through to COM library. Consequently, the code in COM object runs under a local account and any code that needs to access...
1
3964
by: techfuzz | last post by:
I'm posting my problem experience and solution I found here for other ASP.NET developers. I have a web application that uses Forms Authentication with Active Directory to control access. In this web application, I have search page that utilizes the Windows Indexing Service (MSIDXS provider). For reasons I'm not aware of at this time, setting <identity impersonation="true" /> in the web.config causes an error whenever you try to search.
12
2026
by: Anil Krishnamurthy | last post by:
We have an ASP.NET application that uses COM objects through Interop. The web application requires access to network and database resources and hence, needs to impersonate a domain account. The problem is that even when it is configured to run under a certain identity through Web.config, the impersonation is not carried through to COM library. Consequently, the code in COM object runs under a local account and any code that needs to access...
5
2669
by: =?Utf-8?B?S2l0dHlIYXdr?= | last post by:
I am in the process of migrating an II6 environment from a single server to a network load balanced system. Thus, I am using a virtual directory on a UNC share to house the dynamic data that the web farm will access. Since ASP.NET runs as a local account on the IIS servers, I have to use impersonation to perform any operations on the data that resides on the UNC share. I am hard-coding the impersonation credentials in the web.config files...
0
9562
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10535
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10303
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10061
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9111
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7598
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6838
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5622
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4270
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.