Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Web Services > Impersonation and switching back to ASPNET user priviledges

Reply
Thread Tools

Impersonation and switching back to ASPNET user priviledges

 
 
nano2k
Guest
Posts: n/a
 
      06-27-2007
Hi

In my webservice, for certain requests, I need to start another
process on the server side.
To start My process, I need to have administrative rights, so i'm
using the impersonation mechanism using a predefined fixed user
account on server machine.
All works fine, no problem, but after the process starts, I need to
"revert" to ASPNET or NETWORK SERVICES user account priviledges. This
part is what I'm missing.

To impersonate, i'm using this code:

public static bool impersonateValidUser(String userName, String
domain, String password) {
WindowsIdentity tempWindowsIdentity;
IntPtr token = IntPtr.Zero;
IntPtr tokenDuplicate = IntPtr.Zero;

if(WinAPI.RevertToSelf()) {
if(WinAPI.LogonUserA(userName, domain, password,
WinAPI.LOGON32_LOGON_INTERACTIVE,
WinAPI.LOGON32_PROVIDER_DEFAULT, ref token) != 0) {
if(WinAPI.DuplicateToken(token, 2, ref tokenDuplicate) != 0) {
tempWindowsIdentity = new WindowsIdentity(tokenDuplicate);
impersonationContext = tempWindowsIdentity.Impersonate();
if (impersonationContext != null) {
WinAPI.CloseHandle(token);
WinAPI.CloseHandle(tokenDuplicate);
return true;
}
}
}
}
if(token!= IntPtr.Zero)
WinAPI.CloseHandle(token);
if(tokenDuplicate!=IntPtr.Zero)
WinAPI.CloseHandle(tokenDuplicate);
return false;
}

I tried using the above method like this:

//save current user account:
string name = Environment.UserName;
string domain = Environment.UserDomainName;

bool b = impersonateValidUser("admin_user", "domain", "pass");
//b gets the value of true, so impersonation succeeded
//now, start the process
.....
//succeeded
//trying to revert to previous user account (ASPNET or NETWORK
SERVICES for server systems):
b = impersonateValidUser(name, domain, string.Empty);
//b is false - it seems that the ASPNET has a default password (?)

Any ideas? Thanks.

 
Reply With Quote
 
 
 
 
nano2k
Guest
Posts: n/a
 
      06-27-2007
I think I found my answer.
Calling WinAPI.RevertToSelf() after finishing all operations that
required impersonation seems to work.



nano2k a scris:
> Hi
>
> In my webservice, for certain requests, I need to start another
> process on the server side.
> To start My process, I need to have administrative rights, so i'm
> using the impersonation mechanism using a predefined fixed user
> account on server machine.
> All works fine, no problem, but after the process starts, I need to
> "revert" to ASPNET or NETWORK SERVICES user account priviledges. This
> part is what I'm missing.
>
> To impersonate, i'm using this code:
>
> public static bool impersonateValidUser(String userName, String
> domain, String password) {
> WindowsIdentity tempWindowsIdentity;
> IntPtr token = IntPtr.Zero;
> IntPtr tokenDuplicate = IntPtr.Zero;
>
> if(WinAPI.RevertToSelf()) {
> if(WinAPI.LogonUserA(userName, domain, password,
> WinAPI.LOGON32_LOGON_INTERACTIVE,
> WinAPI.LOGON32_PROVIDER_DEFAULT, ref token) != 0) {
> if(WinAPI.DuplicateToken(token, 2, ref tokenDuplicate) != 0) {
> tempWindowsIdentity = new WindowsIdentity(tokenDuplicate);
> impersonationContext = tempWindowsIdentity.Impersonate();
> if (impersonationContext != null) {
> WinAPI.CloseHandle(token);
> WinAPI.CloseHandle(tokenDuplicate);
> return true;
> }
> }
> }
> }
> if(token!= IntPtr.Zero)
> WinAPI.CloseHandle(token);
> if(tokenDuplicate!=IntPtr.Zero)
> WinAPI.CloseHandle(tokenDuplicate);
> return false;
> }
>
> I tried using the above method like this:
>
> //save current user account:
> string name = Environment.UserName;
> string domain = Environment.UserDomainName;
>
> bool b = impersonateValidUser("admin_user", "domain", "pass");
> //b gets the value of true, so impersonation succeeded
> //now, start the process
> ....
> //succeeded
> //trying to revert to previous user account (ASPNET or NETWORK
> SERVICES for server systems):
> b = impersonateValidUser(name, domain, string.Empty);
> //b is false - it seems that the ASPNET has a default password (?)
>
> Any ideas? Thanks.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Process Switching vs. Fast/CEF Switching? asdf Cisco 7 05-29-2007 05:26 PM
switching between HTML and ASPNET =?Utf-8?B?S2VubnkgTS4=?= ASP .Net 7 04-29-2006 06:11 AM
Active Directory Authentication and Priviledges problem deathbydisco@hotmail.com ASP .Net 1 05-04-2005 08:58 PM
ASPNET priviledges to correctly run Crystal Reports M. Simioni ASP .Net 1 04-17-2005 03:08 PM
Re: Impersonation in ASPNET and LogonUser Mary Chipman ASP .Net 0 09-03-2003 03:48 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57