Impersonation on Windows Server 2003

Ask a Question related to ASP.NET Security, Design and Development.

  1. #1

    Default Impersonation on Windows Server 2003

    I have an ASP.NET application that I'm developing on Windows 2003 and IIS 6.
    I have included the < identity impersonate="true" /> in the web.config file
    to force the application code to run under the currently logged in user, and
    the site disalows Anonymous access.

    The problem I'm running into is this: At key points in the application
    code, I need to write to the disk and perform other 'administrative' tasks
    that will [in most cases] require code to run under an administrative or
    utility account.

    I have tried to impersonate the administrative account two different ways:
    via an interop call to ImpersonateLoggedOnUser(securityToken) and via
    calling the Impersonate() method against a WindowsIdentity object.

    Both of these work, however the context I then run under is _severly_
    limited. (The second method works better by the way, as you cannot
    accidently drop down to the service account.)

    For example, when under this impersonation context, I cannot access the
    ConfigurationSettings.AppSettings object (which is what I really need), or
    even the WindowsIdentity.GetCurrent() method. This is the result even if
    the account I impersonate is in the Administrators group on the server.

    I know that impersonation works differently on Windows 2000 as opposed to
    Windows XP - so I'm assuming that the same is true in Windows 2003; but I
    can't seem to find the methodology for doing what I need to in either 2000
    or 2003.

    Can anyone offer some insight?

    Thanks,

    hb.


    Halcyon Woodward Guest

  2. Similar Questions and Discussions

    1. Are hash rules available in Windows 2000, or is this something new that comes with Windows Server 2003?
      Experts, We run a native-mode Windows 2000 single domain. We want to create a Group Policy object (GPO), perhaps named Software Restrictions, to...
    2. Windows 2000 Server/Windows Server 2003 Dual Boot
      I have been asked to provide a dual boot server using both Windows 2000 Server and Windows Server 2003 Std Edt. Can anyone tell me whether this is...
    3. Windows Server 2003 member in Windows 2000 domain
      At a primary school where I sometimes aid in the network administration we are going to add a file server to our existing network. Currently the...
    4. Windows 2003 Server can't see members of Windows 2000 Domain
      Hi there, The problem I have is quite straight-forward to describe: I have an existing Windows 2000 domain to which I've recently added a new...
    5. Explorer Search (F3) find a text/keyword in files is defect in Windows XP SP1 und Windows 2003 Server
      Is this issue known? And do anybody knows, if there is an Microsoft update is available? (no hint in Microsoft Updates) Or do anybody know a real...
  3. #2

    Default RE: Impersonation on Windows Server 2003

    Hello Halcyon,

    Impersonating a windowsidentity did not help the process using the new windows credential. The process would still use the
    credentials of aspnet_wp.exe, typically ASPNET.

    So for disk writing issue, it is better for you to add ACL to that folder to allow aspnet account to write to it. Besides, we could
    use CreateProcessAsUser to spawn a new process under new credential.

    Here is one sample for you:

    The user credential being used for spawning the process must have Replace A Process Level Token and Adjust Memory
    Quotas privs for this to work.


    Imports System.Data

    Imports System.Data.SqlClient

    Imports System.Globalization

    Imports System.Diagnostics

    Imports System.Runtime.InteropServices

    Imports System.Security.Principal

    Imports System.Security.Permissions





    Public Class WebForm1

    Inherits System.Web.UI.Page



    <StructLayout(LayoutKind.Sequential)> Public Structure STARTUPINFO

    Public cb As Int32

    Public lpReserved As String

    Public lpDesktop As String

    Public lpTitle As String

    Public dwX As UInt32

    Public dwY As UInt32

    Public dwXSize As UInt32

    Public dwYSize As UInt32

    Public dwXCountChars As UInt32

    Public dwYCountChars As UInt32

    Public dwFillAttribute As UInt32

    Public dwFlags As UInt32

    Public wShowWindow As Int16

    Public cbReserved2 As Int16

    Public lpReserved2 As IntPtr

    Public hStdInput As IntPtr

    Public hStdOutput As IntPtr

    Public hStdError As IntPtr

    End Structure



    <StructLayout(LayoutKind.Sequential)> Public Structure PROCESS_INFORMATION

    Public hProcess As IntPtr

    Public hThread As IntPtr

    Public dwProcessId As UInt32

    Public dwThreadId As UInt32

    End Structure



    <StructLayout(LayoutKind.Sequential)> Public Structure SECURITY_ATTRIBUTES

    Public Length As Int32

    Public lpSecurityDescriptor As IntPtr

    Public bInheritHandle As Boolean

    End Structure



    <DllImport("kernel32.dll", EntryPoint:="CloseHandle", SetLastError:=True, CharSet:=CharSet.Auto, CallingConvention:
    =CallingConvention.StdCall)> _

    Public Shared Function CloseHandle(ByVal handle As IntPtr) As Boolean

    End Function



    <DllImport("advapi32.dll", EntryPoint:="CreateProcessAsUser", SetLastError:=True, CharSet:=CharSet.Ansi,
    CallingConvention:=CallingConvention.StdCall)> _

    Public Shared Function CreateProcessAsUser(ByVal hToken As IntPtr, ByVal lpApplicationName As String, ByVal
    lpCommandLine As String, ByRef lpProcessAttributes As SECURITY_ATTRIBUTES, _

    ByRef lpThreadAttributes As SECURITY_ATTRIBUTES, ByVal bInheritHandle As Boolean, ByVal dwCreationFlags As
    Int32, ByVal lpEnvironment As IntPtr, _

    ByVal lpCurrentDirectory As String, ByRef lpStartupInfo As STARTUPINFO, ByRef lpProcessInformation As
    PROCESS_INFORMATION) As Boolean

    End Function



    <DllImport("advapi32.dll", EntryPoint:="DuplicateTokenEx")> _

    Public Shared Function DuplicateTokenEx(ByVal ExistingTokenHandle As IntPtr, ByVal dwDesiredAccess As Int32, _

    ByRef lpThreadAttributes As SECURITY_ATTRIBUTES, ByVal TokenType As Int32, _

    ByVal ImpersonationLevel As Int32, ByRef DuplicateTokenHandle As IntPtr) As Boolean

    End Function



    <DllImport("advapi32.dll")> _

    Public Shared Function LogonUser(ByVal lpszUsername As String, ByVal lpszDomain As String, ByVal lpszPassword As
    String, _

    ByVal dwLogonType As Integer, ByVal dwLogonProvider As Integer, ByRef phToken As Integer) As Boolean

    End Function



    Private Sub RunProcessAsUser(ByVal strUid As String, ByVal strPwd As String, ByVal strDomain As String, ByVal
    strProcessPath As String)

    ''The Windows NT user token.

    'Dim token1 As Integer



    ''Get the user token for the specified user, machine, and password using the unmanaged LogonUser method.



    ''The parameters for LogonUser are the user name, computer name, password,

    ''Logon type (LOGON32_LOGON_NETWORK_CLEARTEXT), Logon provider (LOGON32_PROVIDER_DEFAULT),

    ''and user token.

    'Dim loggedOn As Boolean = LogonUser(strUid, strDomain, strPwd, 3, 0, token1)

    'Dim token2 As IntPtr = New IntPtr(token1)



    'Dim mWI2 As WindowsIdentity = New WindowsIdentity(token2)



    ''Impersonate the user.

    'Dim mWIC As WindowsImpersonationContext = mWI2.Impersonate()



    Dim Token As IntPtr = New IntPtr(0)

    Token = WindowsIdentity.GetCurrent().Token ˇ®will either use ASPNET, or the impersonated windows credential if
    above section is uncommented



    Dim ret As Boolean



    Dim sa As SECURITY_ATTRIBUTES = New SECURITY_ATTRIBUTES()

    sa.bInheritHandle = False

    sa.Length = Marshal.SizeOf(sa)

    sa.lpSecurityDescriptor = IntPtr.op_Explicit(0)



    Const GENERIC_ALL As Int32 = &H10000000



    Const SecurityImpersonation As Int32 = 2

    Const TokenType As Int32 = 1

    Dim DupedToken As IntPtr = New IntPtr(0)



    ret = DuplicateTokenEx(Token, GENERIC_ALL, sa, SecurityImpersonation, TokenType, DupedToken)





    Dim si As STARTUPINFO = New STARTUPINFO()

    si.cb = Marshal.SizeOf(si)

    si.lpDesktop = ""



    Dim pi As PROCESS_INFORMATION = New PROCESS_INFORMATION()



    ret = CreateProcessAsUser(DupedToken, strProcessPath, "", sa, sa, False, 0, IntPtr.op_Explicit(0), "c:\\", si, pi)

    'ret = CreateProcessAsUser(DupedToken, strProcessPath, "", IntPtr.Zero, IntPtr.Zero, True, 0, IntPtr.Zero, "C:\\", si, pi)



    'Revert to previous identity.

    'mWIC.Undo()



    'ret = CloseHandle(token2)

    ret = CloseHandle(DupedToken)



    End Sub





    End Class



    Best regards,
    Yanhong Huang
    Microsoft Online Partner Support

    Get Secure! - [url]www.microsoft.com/security[/url]
    This posting is provided "AS IS" with no warranties, and confers no rights.

    --------------------
    !From: "Halcyon Woodward" <halcyondaze73@hotmail.com>
    !Subject: Impersonation on Windows Server 2003
    !Date: Wed, 16 Jul 2003 15:29:50 -0700
    !Lines: 35
    !X-Priority: 3
    !X-MSMail-Priority: Normal
    !X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
    !X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
    !Message-ID: <OBefGn#SDHA.2316@tk2msftngp13.phx.gbl>
    !Newsgroups: microsoft.public.dotnet.framework.aspnet.security
    !NNTP-Posting-Host: nausers.mccann.com 199.4.18.2
    !Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!tk2msftn gp13.phx.gbl
    !Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.framework.aspnet.security: 5921
    !X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.security
    !
    !I have an ASP.NET application that I'm developing on Windows 2003 and IIS 6.
    !I have included the < identity impersonate="true" /> in the web.config file
    !to force the application code to run under the currently logged in user, and
    !the site disalows Anonymous access.
    !
    !The problem I'm running into is this: At key points in the application
    !code, I need to write to the disk and perform other 'administrative' tasks
    !that will [in most cases] require code to run under an administrative or
    !utility account.
    !
    !I have tried to impersonate the administrative account two different ways:
    !via an interop call to ImpersonateLoggedOnUser(securityToken) and via
    !calling the Impersonate() method against a WindowsIdentity object.
    !
    !Both of these work, however the context I then run under is _severly_
    !limited. (The second method works better by the way, as you cannot
    !accidently drop down to the service account.)
    !
    !For example, when under this impersonation context, I cannot access the
    !ConfigurationSettings.AppSettings object (which is what I really need), or
    !even the WindowsIdentity.GetCurrent() method. This is the result even if
    !the account I impersonate is in the Administrators group on the server.
    !
    !I know that impersonation works differently on Windows 2000 as opposed to
    !Windows XP - so I'm assuming that the same is true in Windows 2003; but I
    !can't seem to find the methodology for doing what I need to in either 2000
    !or 2003.
    !
    !Can anyone offer some insight?
    !
    !Thanks,
    !
    !hb.
    !
    !
    !


    Yan-Hong Huang[MSFT] Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

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 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139