Create a domain account failure through the ASP.net program

Source: Internet
Author: User
Tags bool config iis thread
Asp.net| Program | Create a batch of domain accounts with mailboxes that I successfully created with Windows programs, but when I transfer this code to a colleague of mine (who is responsible for developing a Web application) to migrate to ASP.net, only domain accounts can be created and mailboxes cannot be created. Why, then?

We consulted with the Microsoft engineer, who told us that this was due to ASP.net's lack of authority and that we should simulate the user in asp.net so that we could create it successfully.

I will extract the relevant articles from Microsoft:



An account or user that simulates IIS authentication

To simulate a Microsoft Internet information Services (IIS) authentication user when you receive each request for each page in the ASP.net application, you must include the <identity> tag in the Web.config file for this application and Impe The Rsonate property is set to true. For example:

<identity impersonate= "true"/>


Impersonate a specific user for all requests for a asp.net application

To impersonate a specific user for all requests on all pages of a asp.net application, you can specify the UserName and Password properties in the <identity> tag of the application's Web.config file. For example:

<identity impersonate= "true" Username= "AccountName" password= "password"/>


Note: The identity of a process that simulates a particular user on a thread must have the "as part of the operating system" right. By default, the Aspnet_wp.exe process runs under a computer account named Aspnet. However, this account does not have the required permissions to impersonate a particular user. If you try to impersonate a specific user, an error message appears.

To resolve this issue, use one of the following methods:



Grant the "as part of the operating system" permission to the ASPNET account (the least privileged account).

Note: Although this method solves the problem, Microsoft does not recommend using this method.



In the <processModel> configuration section of the Machine.config file, change the account that is used to run the Aspnet_wp.exe process to the System account.

Impersonate an authentication user in code

To impersonate an authentication user (user.identity) only when the code-specific section is running, you can use the following code. This method requires the authentication user identity to be of type WindowsIdentity.

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 code

To simulate a specific user only when you are running a specific part of your 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.
Undoimpersonation ()
Else
' Your impersonation failed. Therefore, include a fail-safe mechanism 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 are 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.
Undoimpersonation ();
}
Else
{
Your impersonation failed. Therefore, include a fail-safe mechanism 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.
Undoimpersonation ();
}
Else
{
Your impersonation failed. Therefore, include a fail-safe mechanism 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 identity of a process that simulates a particular user on a thread must have the "as part of the operating system" right. By default, the Aspnet_wp.exe process runs under a computer account named Aspnet. However, this account does not have the required permissions to impersonate a particular user. If you try to impersonate a specific user, an error message appears.

To resolve this issue, use one of the following methods:



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



In the <processModel> configuration section of the Machine.config file, change the account that is used to run the Aspnet_wp.exe process to the System account.

Back to the top of the page





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.