Simulate a specific user only when running a specific part of the code

Source: Internet
Author: User

This document references the following Microsoft. NET Framework class library namespace:

System. Web. Security
System. Security. Principal
System. runtime. interopservices

 

Simulate a local user to run a specific program
Dim Logon32_logon_interactive As   Integer   =   2
Dim Logon32_provider_default As   Integer   =   0

Dim Impersonationcontext As Windowsimpersonationcontext

Declare   Function logonusera () Function Logonusera 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   Integer

Declare   Auto   Function duplicatetoken () Function Duplicatetoken Lib   " Advapi32.dll " (_
Byval Existingtokenhandle As Intptr ,_
Byval Impersonationlevel As   Integer ,_
Byref Duplicatetokenhandle As Intptr) As   Integer

Declare   Auto   Function reverttoself () Function Reverttoself Lib   " Advapi32.dll " () As   Long
Declare   Auto   Function closehandle () Function Closehandle Lib   " Kernel32.dll " ( Byval Handle As Intptr) As   Long


Public   Sub page_load () Sub Page_load ( Byval S As   Object , Byval E As Eventargs)
If Impersonatevaliduser ( " Username " , " Domain " , " Password " ) Then
' Insert your code that runs under the security context of a specific user here.
Undoimpersonation ()
Else
' Your impersonation failed. Therefore, include a fail-safe mechanic here.
End   If
End sub

Private   Function impersonatevaliduser () Function Impersonatevaliduser ( Byval Username As   String ,_
Byval Domain As   String , Byval Password As   String ) As   Boolean

Dim Tempwindowsidentity As Windowsidentity
Dim Token As Intptr = Intptr. Zero
Dim Tokenduplicate As Intptr = Intptr. Zero
Impersonatevaliduser =   False

If Reverttoself () Then
If Logonusera (username, domain, password, logon32_logon_interactive,
Logon32_provider_default, token) <>   0   Then
If Duplicatetoken (token, 2 , Tokenduplicate) <>   0   Then
Tempwindowsidentity =   New Windowsidentity (tokenduplicate)
Impersonationcontext = Tempwindowsidentity. Impersonate ()
If   Not Impersonationcontext Is   Nothing   Then
Impersonatevaliduser =   True
End   If
End   If
End   If
End   If
If   Not Tokenduplicate. Equals (intptr. Zero) Then
Closehandle (tokenduplicate)
End   If
If   Not Token. Equals (intptr. Zero) Then
Closehandle (token)
End   If
End Function

Private   Sub undoimpersonation () SubUndoimpersonation ()
Impersonationcontext. Undo ()
End sub

previously, it was safer to use the mark in the web. config file.
original address: http://support.microsoft.com/default.aspx? SCID = KB; ZH-CN; 306158 #4

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.