仅在运行代码的特定部分时模拟特定用户

本文介绍如何使用VB.NET通过调用Windows API实现模拟登录,具体包括使用LogonUserA函数进行用户验证,DuplicateToken函数创建复制令牌,以及WindowsIdentity类创建Windows身份。此方法相比Web.config文件更安全。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本文引用下面的 Microsoft .NET Framework 类库命名空间:
 

System.Web.Security
System.Security.Principal
System.Runtime.InteropServices

 

ContractedBlock.gif ExpandedBlockStart.gif 模拟本地用户运行特定程序
None.gifDim LOGON32_LOGON_INTERACTIVE As Integer = 2
None.gif
Dim LOGON32_PROVIDER_DEFAULT As Integer = 0
None.gif
None.gif
Dim impersonationContext As WindowsImpersonationContext
None.gif
ExpandedBlockStart.gifContractedBlock.gif
Declare Function LogonUserA()Function LogonUserA Lib "advapi32.dll" (ByVal lpszUsername As String, _
InBlock.gif                        
ByVal lpszDomain As String, _
InBlock.gif                        
ByVal lpszPassword As String, _
InBlock.gif                        
ByVal dwLogonType As Integer, _
InBlock.gif                        
ByVal dwLogonProvider As Integer, _
InBlock.gif                        
ByRef phToken As IntPtr) As Integer
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
Declare Auto Function DuplicateToken()Function DuplicateToken Lib "advapi32.dll" ( _
InBlock.gif                        
ByVal ExistingTokenHandle As IntPtr, _
InBlock.gif                        
ByVal ImpersonationLevel As Integer, _
InBlock.gif                        
ByRef DuplicateTokenHandle As IntPtr) As Integer
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
Declare Auto Function RevertToSelf()Function RevertToSelf Lib "advapi32.dll" () As Long
ExpandedSubBlockStart.gifContractedSubBlock.gif
Declare Auto Function CloseHandle()Function CloseHandle Lib "kernel32.dll" (ByVal handle As IntPtr) As Long
InBlock.gif
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
Public Sub Page_Load()Sub Page_Load(ByVal s As ObjectByVal e As EventArgs)
InBlock.gif    
If impersonateValidUser("username""domain""password"Then
InBlock.gif        
'Insert your code that runs under the security context of a specific user here.
InBlock.gif
        undoImpersonation()
InBlock.gif    
Else
InBlock.gif        
'Your impersonation failed. Therefore, include a fail-safe mechanism here.
InBlock.gif
    End If
ExpandedSubBlockEnd.gif
End Sub

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
Private Function impersonateValidUser()Function impersonateValidUser(ByVal userName As String, _
InBlock.gif
ByVal domain As StringByVal password As StringAs Boolean
InBlock.gif
InBlock.gif    
Dim tempWindowsIdentity As WindowsIdentity
InBlock.gif    
Dim token As IntPtr = IntPtr.Zero
InBlock.gif    
Dim tokenDuplicate As IntPtr = IntPtr.Zero
InBlock.gif    impersonateValidUser 
= False
InBlock.gif
InBlock.gif    
If RevertToSelf() Then
InBlock.gif        
If LogonUserA(userName, domain, password, LOGON32_LOGON_INTERACTIVE,
InBlock.gif                     LOGON32_PROVIDER_DEFAULT, token) 
<> 0 Then
InBlock.gif            
If DuplicateToken(token, 2, tokenDuplicate) <> 0 Then
InBlock.gif                tempWindowsIdentity 
= New WindowsIdentity(tokenDuplicate)
InBlock.gif                impersonationContext 
= tempWindowsIdentity.Impersonate()
InBlock.gif                
If Not impersonationContext Is Nothing Then
InBlock.gif                    impersonateValidUser 
= True
InBlock.gif                
End If
InBlock.gif            
End If
InBlock.gif        
End If
InBlock.gif    
End If
InBlock.gif    
If Not tokenDuplicate.Equals(IntPtr.Zero) Then
InBlock.gif        CloseHandle(tokenDuplicate)
InBlock.gif    
End If
InBlock.gif    
If Not token.Equals(IntPtr.Zero) Then
InBlock.gif        CloseHandle(token)
InBlock.gif    
End If
ExpandedSubBlockEnd.gif
End Function

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
Private Sub undoImpersonation()Sub undoImpersonation()
InBlock.gif    impersonationContext.Undo()
ExpandedSubBlockEnd.gif
End Sub

InBlock.gif

以前用  Web.config 文件的 <identity> 标记,还是改成这种比较安全。
原文地址:http://support.microsoft.com/default.aspx?scid=kb;zh-cn;306158#4

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值