【问题标题】:how do i use another identity to execute my code in asp.net provided i have a username and password如果我有用户名和密码,我如何使用另一个身份在 asp.net 中执行我的代码
【发布时间】:2009-02-20 10:01:51
【问题描述】:

我正在构建一个基于 Web 的小型管理应用程序。 在其中,我需要通过 wmi 使用不同的帐户连接到不同的服务器。

我想告诉我的应用程序:你现在由 user1 运行,做这个和这个。 然后我想告诉它:现在你是user2,做这个做这个。

我想,我的问题不是很清楚,我会重构它。

【问题讨论】:

    标签: c# asp.net security wmi


    【解决方案1】:

    您必须编写一段单独的 .Net 代码(也有一些非托管调用)来模拟您的用户,然后在模拟该用户的同时调用您的代码。然后您可以在之后恢复用户帐户:

    为 VB 示例道歉,但这很容易移植到 C#。


    VB 示例

    Public Class UserImpersonation
    
        Private Declare Auto Function LogonUser Lib "advapi32.dll" (ByVal lpszUsername As [String], _
            ByVal lpszDomain As [String], ByVal lpszPassword As [String], _
            ByVal dwLogonType As Integer, ByVal dwLogonProvider As Integer, _
            ByRef phToken As IntPtr) As Boolean
    
        <DllImport("kernel32.dll")> _
        Private Shared Function FormatMessage(ByVal dwFlags As Integer, ByRef lpSource As IntPtr, _
            ByVal dwMessageId As Integer, ByVal dwLanguageId As Integer, ByRef lpBuffer As [String], _
            ByVal nSize As Integer, ByRef Arguments As IntPtr) As Integer
    
        End Function
    
        Private Declare Auto Function CloseHandle Lib "kernel32.dll" (ByVal handle As IntPtr) As Boolean
    
        Private Declare Auto Function DuplicateToken Lib "advapi32.dll" (ByVal ExistingTokenHandle As IntPtr, _
                ByVal SECURITY_IMPERSONATION_LEVEL As Integer, _
                ByRef DuplicateTokenHandle As IntPtr) As Boolean
    
        <PermissionSetAttribute(SecurityAction.Demand, Name:="FullTrust")> _
        Public Shared Function ImpersonateUser(ByVal strDomain As String, ByVal strUserid As String, ByVal strPassword As String) As WindowsImpersonationContext
    
            Dim tokenHandle As New IntPtr(0)
            Dim dupeTokenHandle As New IntPtr(0)
    
            Try
                ' Get the user token for the specified user, domain, and password using the 
                ' unmanaged LogonUser method.  
                ' The local machine name can be used for the domain name to impersonate a user on this machine.
    
                Const LOGON32_PROVIDER_DEFAULT As Integer = 0
                'This parameter causes LogonUser to create a primary token.
                Const LOGON32_LOGON_INTERACTIVE As Integer = 2
    
                tokenHandle = IntPtr.Zero
    
                ' Call LogonUser to obtain a handle to an access token.
                Dim returnValue As Boolean = LogonUser(strUserid, strDomain, strPassword, LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT, tokenHandle)
    
                If returnValue = False Then
                    Dim ret As Integer = Marshal.GetLastWin32Error()
                    Throw New System.ComponentModel.Win32Exception(ret)
                Else
                    ' Use the token handle returned by LogonUser.
                    Dim newId As New WindowsIdentity(tokenHandle)
                    Dim ImpersonatedUser As WindowsImpersonationContext = newId.Impersonate()
    
                    Return ImpersonatedUser
                End If
    
            Catch ex As Exception
                Console.WriteLine("UserImpersonation.impersonateUser Exception Occurred: " + ex.Message)
    
                Return Nothing
            End Try
    
            ' Free the tokens.
            If Not System.IntPtr.op_Equality(tokenHandle, IntPtr.Zero) Then
                CloseHandle(tokenHandle)
            End If
        End Function
    
    
        Public Shared Function UndoImpersonate(ByVal WIC As WindowsImpersonationContext) As Boolean
            Try
                ' Stop impersonating the user.
                WIC.Undo()
    
                Return True
            Catch ex As Exception
                Console.WriteLine(("Exception occurred. " + ex.Message))
    
                Return False
            End Try
    
        End Function
    End Class
    

    【讨论】:

      猜你喜欢
      • 2014-12-23
      • 1970-01-01
      • 2023-04-09
      • 2013-01-18
      • 2017-08-08
      相关资源
      最近更新 更多