123
技術社區[雲棲]
通過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