通过ASP.net程序创建域帐户故障
我曾经成功地使用windows程序成功的创建了一批带邮箱的域帐户,但是,当我把这段代码交给我的一个同事(她负责开发Web应用)迁移到asp.net中后,只能创建域帐户,不能创建邮箱。为什么呢?
我们咨询了微软的工程师,他告诉我们,这是由于asp.net的权限不够,我们应该在asp.net模拟用户,这样就可以成功创建。
我将微软的相关文章摘录下来:
模拟IIS验证的帐户或用户
若要在收到ASP.NET应用程序中每个页的每个请求时模拟MicrosoftInternet信息服务(IIS)身份验证用户,必须在此应用程序的Web.config文件中包含<identity>标记,并将impersonate属性设置为true。例如:
<identityimpersonate="true"/> |
为ASP.NET应用程序的所有请求模拟特定用户
若要为ASP.NET应用程序的所有页面上的所有请求模拟特定用户,可以在该应用程序的Web.config文件的<identity>标记中指定userName和password属性。例如:
<identityimpersonate="true"userName="accountname"password="password"/> |
注意:在线程上模拟特定用户的进程的标识必须具有“作为操作系统的一部分”权限。默认情况下,Aspnet_wp.exe进程在名为ASPNET的计算机帐户下运行。不过,此帐户没有模拟特定用户所需的权限。如果您尝试模拟特定用户,则会出现一条错误信息。
要解决此问题,请使用下列方法之一:
•为ASPNET帐户(权限最低的帐户)授予“作为操作系统的一部分”权限。
注意:虽然此方法可以解决问题,但Microsoft不建议使用此方法。
•在Machine.config文件的<processModel>配置部分中,将运行Aspnet_wp.exe进程所使用的帐户更改为System帐户。
在代码中模拟身份验证用户
若要仅在运行代码特定部分时模拟身份验证用户(User.Identity),您可以使用以下代码。此方法要求身份验证用户标识的类型为WindowsIdentity。
VisualBasic.NET
DimimpersonationContextAsSystem.Security.Principal.WindowsImpersonationContext DimcurrentWindowsIdentityAsSystem.Security.Principal.WindowsIdentity currentWindowsIdentity=CType(User.Identity,System.Security.Principal.WindowsIdentity) impersonationContext=currentWindowsIdentity.Impersonate() 'Insertyourcodethatrunsunderthesecuritycontextoftheauthenticatinguserhere. impersonationContext.Undo() |
VisualC#.NET
System.Security.Principal.WindowsImpersonationContextimpersonationContext; |
VisualJ#.NET
System.Security.Principal.WindowsImpersonationContextimpersonationContext; impersonationContext= ((System.Security.Principal.WindowsIdentity)get_User().get_Identity()).Impersonate(); //Insertyourcodethatrunsunderthesecuritycontextoftheauthenticatinguserhere. impersonationContext.Undo(); |
在代码中模拟特定用户
若要仅在运行代码特定部分时模拟特定用户,请使用以下代码:
VisualBasic.NET
<%@PageLanguage="VB"%> |
VisualC#.NET
<%@PageLanguage="C#"%> <%@ImportNamespace="System.Web"%> <%@ImportNamespace="System.Web.Security"%> <%@ImportNamespace="System.Security.Principal"%> <%@ImportNamespace="System.Runtime.InteropServices"%> <scriptrunat=server> publicconstintLOGON32_LOGON_INTERACTIVE=2; publicconstintLOGON32_PROVIDER_DEFAULT=0; WindowsImpersonationContextimpersonationContext; [DllImport("advapi32.dll")] publicstaticexternintLogonUserA(StringlpszUserName, StringlpszDomain, StringlpszPassword, intdwLogonType, intdwLogonProvider, refIntPtrphToken); [DllImport("advapi32.dll",CharSet=CharSet.Auto,SetLastError=true)] publicstaticexternintDuplicateToken(IntPtrhToken, intimpersonationLevel, refIntPtrhNewToken); [DllImport("advapi32.dll",CharSet=CharSet.Auto,SetLastError=true)] publicstaticexternboolRevertToSelf(); [DllImport("kernel32.dll",CharSet=CharSet.Auto)] publicstaticexternboolCloseHandle(IntPtrhandle); publicvoidPage_Load(Objects,EventArgse) { if(impersonateValidUser("username","domain","password")) { //Insertyourcodethatrunsunderthesecuritycontextofaspecificuserhere. undoImpersonation(); } else { //Yourimpersonationfailed.Therefore,includeafail-safemechanismhere. } } privateboolimpersonateValidUser(StringuserName,Stringdomain,Stringpassword) { WindowsIdentitytempWindowsIdentity; IntPtrtoken=IntPtr.Zero; IntPtrtokenDuplicate=IntPtr.Zero; if(RevertToSelf()) { if(LogonUserA(userName,domain,password,LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT,reftoken)!=0) { if(DuplicateToken(token,2,reftokenDuplicate)!=0) { tempWindowsIdentity=newWindowsIdentity(tokenDuplicate); impersonationContext=tempWindowsIdentity.Impersonate(); if(impersonationContext!=null) { CloseHandle(token); CloseHandle(tokenDuplicate); returntrue; } } } } if(token!=IntPtr.Zero) CloseHandle(token); if(tokenDuplicate!=IntPtr.Zero) CloseHandle(tokenDuplicate); returnfalse; } privatevoidundoImpersonation() { impersonationContext.Undo(); } </script> |
VisualJ#.NET
<%@Pagelanguage="VJ#"%> |
注意:在线程上模拟特定用户的进程的标识必须具有“作为操作系统的一部分”权限。默认情况下,Aspnet_wp.exe进程在名为ASPNET的计算机帐户下运行。不过,此帐户没有模拟特定用户所需的权限。如果您尝试模拟特定用户,则会出现一条错误信息。
要解决此问题,请使用下列方法之一:
•为ASPNET帐户授予“作为操作系统的一部分”权限。
•在Machine.config文件的<processModel>配置部分中,将运行Aspnet_wp.exe进程所使用的帐户更改为System帐户。
最后更新:2017-04-02 00:06:36