我如何使用另一个身份在asp.net中执行我的code提供我有一个用户名和密码 [英] how do i use another identity to execute my code in asp.net provided i have a username and password

查看:101
本文介绍了我如何使用另一个身份在asp.net中执行我的code提供我有一个用户名和密码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要建一个小型的基于Web的管理应用程序。
在这我需要连接到通过WMI使用不同的账户不同的服务器。

I'm building a small web based management app. Within it I need to connect to different servers using different accounts via wmi.

我要的是要告诉我的应用程序:你现在被USER1运行,做到这一点,这一点。
然后我想告诉它:现在你是用户2,做到这一点,这

What I want is to tell my app: you are now run by user1, do this and this. And then I want to tell it: now you are user2, do this and this.

我想,我是不是都在我的问题说清楚,我会重构它。

I guess, I'm not all that clear with my question, I'll refactor it.

推荐答案

您会写一个单独的一块净code(有些umanaged要求太)来执行用户的模拟,然后打电话给你的$ C $ ç而假冒该用户。然后,您可以还原用户帐户算账:

You would have to write a seperate piece of .Net code (some umanaged calls too) to perform impersonation of your user then call your code whilst impersonating that user. You can then restore the user account afterwards:

对于VB样本道歉,但这样会很容易地移植到C#。

Apologies for the VB sample, but this would be easy to port to C#.


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

这篇关于我如何使用另一个身份在asp.net中执行我的code提供我有一个用户名和密码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆