Domain account creation failure through Asp.net Program

Source: Internet
Author: User

Account or user simulating IIS Authentication

To receive an ASP. NET applicationProgramThe Microsoft Internet Information Service (IIS) authentication user must be simulated for each request on each page. The web. config file of this application must contain<Identity>Mark andImpersonateSet propertyTrue. For example:

<Identity impersonate = "true"/>

Simulate a specific user for all requests of ASP. NET Applications

To simulate a specific user for all requests on all pages of an ASP. NET application<Identity>MarkUsernameAndPasswordAttribute. For example:

<Identity impersonate = "true" username = "accountname" Password = "password"/>

Note:The process ID simulating a specific user on the thread must have the "as part of the operating system" permission. The aspnet_wp.exe process runs under a computer account named ASPnet. However, this account does not have the permissions required to simulate a specific user. If you try to simulate a specific user, an error message is displayed.

To solve this problem, use one of the following methods:

Grant the "as part of the operating system" permission to the ASPNET account (the account with the lowest permissions.

Note:Although this method can solve the problem, Microsoft does not recommend this method.

In the <processmodel> Configuration section of the machine. config file, change the account used to run the aspnet_wp.exe process to the system account.

InCodeSimulated identity authentication user

To simulate identity authentication only when a specific part of the code is running (User. Identity), You can use the following code. This method requires that the type of the user identity for identity authentication isWindowsidentity.

Visual Basic. net

Dim impersonationcontext as system. Security. Principal. windowsimpersonationcontext
Dim currentwindowsidentity as system. Security. Principal. windowsidentity
Currentwindowsidentity = ctype (user. Identity, system. Security. Principal. windowsidentity)
Impersonationcontext = currentwindowsidentity. Impersonate ()
'Insert your code that runs under the security context of the Authenticating user here.
Impersonationcontext. Undo ()

Visual C #. net

System. Security. Principal. windowsimpersonationcontext impersonationcontext;
Impersonationcontext =
(System. Security. Principal. windowsidentity) user. Identity). Impersonate ();
// Insert your code that runs under the security context of the Authenticating user here.
Impersonationcontext. Undo ();

Visual J #. net

System. Security. Principal. windowsimpersonationcontext impersonationcontext;
Impersonationcontext =
(System. Security. Principal. windowsidentity) get_user (). get_identity (). Impersonate ();
// Insert your code that runs under the security context of the Authenticating user here.
Impersonationcontext. Undo ();

Simulate a specific user in the code

To simulate a specific user only when running a specific part of the code, use the following code:

Visual Basic. net

<% @ Page Language = "VB" %>
<% @ Import namespace = "system. Web" %>
<% @ Import namespace = "system. Web. Security" %>
<% @ Import namespace = "system. Security. Principal" %>
<% @ Import namespace = "system. runtime. interopservices" %>
<SCRIPT runat = Server>
Dim logon32_logon_interactive as integer = 2
Dim logon32_provider_default as integer = 0
Dim impersonationcontext as windowsimpersonationcontext
Declare 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 lib "advapi32.dll "(_
Byval existingtokenhandle as intptr ,_
Byval impersonationlevel as integer ,_
Byref duplicatetokenhandle as intptr) as integer
Declare auto function reverttoself lib "advapi32.dll" () as long
Declare auto function closehandle lib "kernel32.dll" (byval handle as intptr) as long
Public 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
'Ur impersonation failed. Therefore, include a fail-safe mechanic here.
End if
End sub
Private 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 ()
Impersonationcontext. Undo ()
End sub
</SCRIPT>

Visual C #. net

<% @ Page Language = "C #" %>
<% @ Import namespace = "system. Web" %>
<% @ Import namespace = "system. Web. Security" %>
<% @ Import namespace = "system. Security. Principal" %>
<% @ Import namespace = "system. runtime. interopservices" %>
<SCRIPT runat = Server>
Public const int logon32_logon_interactive = 2;
Public const int logon32_provider_default = 0;
Windowsimpersonationcontext impersonationcontext;
[Dllimport ("advapi32.dll")]
Public static extern int logonusera (string lpszusername,
String lpszdomain,
String lpszpassword,
Int dwlogontype,
Int dwlogonprovider,
Ref intptr phtoken );
[Dllimport ("advapi32.dll", charset = charset. Auto, setlasterror = true)]
Public static extern int duplicatetoken (intptr htoken,
Int impersonationlevel,
Ref intptr hnewtoken );
[Dllimport ("advapi32.dll", charset = charset. Auto, setlasterror = true)]
Public static extern bool reverttoself ();
[Dllimport ("kernel32.dll", charset = charset. Auto)]
Public static extern bool closehandle (intptr handle );
Public void page_load (Object S, eventargs E)
{
If (impersonatevaliduser ("username", "Domain", "password "))
{
// 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.
}
}
Private bool impersonatevaliduser (string username, string domain, string password)
{
Windowsidentity tempwindowsidentity;
Intptr token = intptr. zero;
Intptr tokenduplicate = intptr. zero;
If (reverttoself ())
{
If (logonusera (username, domain, password, logon32_logon_interactive,
Logon32_provider_default, ref token )! = 0)
{
If (duplicatetoken (token, 2, ref tokenduplicate )! = 0)
{
Tempwindowsidentity = new windowsidentity (tokenduplicate );
Impersonationcontext = tempwindowsidentity. Impersonate ();
If (impersonationcontext! = NULL)
{
Closehandle (token );
Closehandle (tokenduplicate );
Return true;
}
}
}
}
If (Token! = Intptr. Zero)
Closehandle (token );
If (tokenduplicate! = Intptr. Zero)
Closehandle (tokenduplicate );
Return false;
}
Private void undoimpersonation ()
{
Impersonationcontext. Undo ();
}
</SCRIPT>

Visual J #. net

<% @ Page Language = "VJ #" %>
<% @ Import namespace = "system. Web" %>
<% @ Import namespace = "system. Web. Security" %>
<% @ Import namespace = "system. Security. Principal" %>
<% @ Import namespace = "system. runtime. interopservices" %>
<SCRIPT runat = Server>
Public static int logon32_logon_interactive = 2;
Public static int logon32_provider_default = 0;
Windowsimpersonationcontext impersonationcontext;
/** @ Attribute dllimport ("advapi32.dll ")*/
Public static native int logonusera (string lpszusername,
String lpszdomain,
String lpszpassword,
Int dwlogontype,
Int dwlogonprovider,
System. intptr [] phtoken );
/** @ Attribute dllimport ("advapi32.dll ",
Charset = charset. Auto, setlasterror = true )*/
Public static native int duplicatetoken (system. intptr htoken,
Int impersonationlevel,
System. intptr [] hnewtoken );
/** @ Attribute dllimport ("kernel32.dll", charset = charset. Auto )*/
Public static native Boolean closehandle (system. intptr [] handle );
/** @ Attribute dllimport ("advapi32.dll ",
Charset = charset. Auto, setlasterror = true )*/
Public static native Boolean reverttoself ();
Public void page_load (Object S, system. eventargs E)
{
If (impersonatevaliduser ("username", "Domain", "password "))
{
// 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.
}
}
Private Boolean impersonatevaliduser (string username, string domain, string password)
{
Windowsidentity tempwindowsidentity;
System. intptr [] token = new system. intptr [1];
System. intptr [] tokenduplicate = new system. intptr [1];
If (reverttoself ())
{
If (logonusera (username, domain, password, logon32_logon_interactive,
Logon32_provider_default, token )! = 0)
{
If (duplicatetoken (token [0], 2, tokenduplicate )! = 0)
{
Tempwindowsidentity = new windowsidentity (tokenduplicate [0]);
Impersonationcontext = tempwindowsidentity. Impersonate ();
If (impersonationcontext! = NULL)
{
Closehandle (tokenduplicate );
Closehandle (token );
Return true;
}
}
}
}
If (! Token [0]. Equals (system. intptr. Zero ))
Closehandle (token );
If (! Tokenduplicate [0]. Equals (system. intptr. Zero ))
Closehandle (tokenduplicate );
Return false;
}
Private void undoimpersonation ()
{
Impersonationcontext. Undo ();
}
</SCRIPT>

Note:The process ID simulating a specific user on the thread must have the "as part of the operating system" permission. The aspnet_wp.exe process runs under a computer account named ASPnet. However, this account does not have the permissions required to simulate a specific user. If you try to simulate a specific user, an error message is displayed.

To solve this problem, use one of the following methods:

Grant the "as part of the operating system" permission to the ASPNET account.

In the <processmodel> Configuration section of the machine. config file, change the account used to run the aspnet_wp.exe process to the system account.

Related Article

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.