閱讀123 返回首頁    go 技術社區[雲棲]


通過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;
impersonationContext=
((System.Security.Principal.WindowsIdentity)User.Identity).Impersonate();
//Insertyourcodethatrunsunderthesecuritycontextoftheauthenticatinguserhere.
impersonationContext.Undo();

VisualJ#.NET


System.Security.Principal.WindowsImpersonationContextimpersonationContext;
impersonationContext=
((System.Security.Principal.WindowsIdentity)get_User().get_Identity()).Impersonate();
//Insertyourcodethatrunsunderthesecuritycontextoftheauthenticatinguserhere.
impersonationContext.Undo();


在代碼中模擬特定用戶

若要僅在運行代碼特定部分時模擬特定用戶,請使用以下代碼:

VisualBasic.NET


<%@PageLanguage="VB"%>
<%@ImportNamespace="System.Web"%>
<%@ImportNamespace="System.Web.Security"%>
<%@ImportNamespace="System.Security.Principal"%>
<%@ImportNamespace="System.Runtime.InteropServices"%>
<scriptrunat=server>
DimLOGON32_LOGON_INTERACTIVEAsInteger=2
DimLOGON32_PROVIDER_DEFAULTAsInteger=0
DimimpersonationContextAsWindowsImpersonationContext
DeclareFunctionLogonUserALib"advapi32.dll"(ByVallpszUsernameAsString,_
ByVallpszDomainAsString,_
ByVallpszPasswordAsString,_
ByValdwLogonTypeAsInteger,_
ByValdwLogonProviderAsInteger,_
ByRefphTokenAsIntPtr)AsInteger
DeclareAutoFunctionDuplicateTokenLib"advapi32.dll"(_
ByValExistingTokenHandleAsIntPtr,_
ByValImpersonationLevelAsInteger,_
ByRefDuplicateTokenHandleAsIntPtr)AsInteger
DeclareAutoFunctionRevertToSelfLib"advapi32.dll"()AsLong
DeclareAutoFunctionCloseHandleLib"kernel32.dll"(ByValhandleAsIntPtr)AsLong
PublicSubPage_Load(ByValsAsObject,ByValeAsEventArgs)
IfimpersonateValidUser("username","domain","password")Then
'Insertyourcodethatrunsunderthesecuritycontextofaspecificuserhere.
undoImpersonation()
Else
'Yourimpersonationfailed.Therefore,includeafail-safemechanismhere.
EndIf
EndSub
PrivateFunctionimpersonateValidUser(ByValuserNameAsString,_
ByValdomainAsString,ByValpasswordAsString)AsBoolean
DimtempWindowsIdentityAsWindowsIdentity
DimtokenAsIntPtr=IntPtr.Zero
DimtokenDuplicateAsIntPtr=IntPtr.Zero
impersonateValidUser=False
IfRevertToSelf()Then
IfLogonUserA(userName,domain,password,LOGON32_LOGON_INTERACTIVE,
LOGON32_PROVIDER_DEFAULT,token)<>0Then
IfDuplicateToken(token,2,tokenDuplicate)<>0Then
tempWindowsIdentity=NewWindowsIdentity(tokenDuplicate)
impersonationContext=tempWindowsIdentity.Impersonate()
IfNotimpersonationContextIsNothingThen
impersonateValidUser=True
EndIf
EndIf
EndIf
EndIf
IfNottokenDuplicate.Equals(IntPtr.Zero)Then
CloseHandle(tokenDuplicate)
EndIf
IfNottoken.Equals(IntPtr.Zero)Then
CloseHandle(token)
EndIf
EndFunction
PrivateSubundoImpersonation()
impersonationContext.Undo()
EndSub
</script>

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#"%>
<%@ImportNamespace="System.Web"%>
<%@ImportNamespace="System.Web.Security"%>
<%@ImportNamespace="System.Security.Principal"%>
<%@ImportNamespace="System.Runtime.InteropServices"%>
<scriptrunat=server>
publicstaticintLOGON32_LOGON_INTERACTIVE=2;
publicstaticintLOGON32_PROVIDER_DEFAULT=0;
WindowsImpersonationContextimpersonationContext;
/**@attributeDllImport("advapi32.dll")*/
publicstaticnativeintLogonUserA(StringlpszUserName,
StringlpszDomain,
StringlpszPassword,
intdwLogonType,
intdwLogonProvider,
System.IntPtr[]phToken);
/**@attributeDllImport("advapi32.dll",
CharSet=CharSet.Auto,SetLastError=true)*/
publicstaticnativeintDuplicateToken(System.IntPtrhToken,
intimpersonationLevel,
System.IntPtr[]hNewToken);
/**@attributeDllImport("kernel32.dll",CharSet=CharSet.Auto)*/
publicstaticnativebooleanCloseHandle(System.IntPtr[]handle);
/**@attributeDllImport("advapi32.dll",
CharSet=CharSet.Auto,SetLastError=true)*/
publicstaticnativebooleanRevertToSelf();
publicvoidPage_Load(Objects,System.EventArgse)
{
if(impersonateValidUser("username","domain","password"))
{
//Insertyourcodethatrunsunderthesecuritycontextofaspecificuserhere.
undoImpersonation();
}
else
{
//Yourimpersonationfailed.Therefore,includeafail-safemechanismhere.
}
}
privatebooleanimpersonateValidUser(StringuserName,Stringdomain,Stringpassword)
{
WindowsIdentitytempWindowsIdentity;
System.IntPtr[]token=newSystem.IntPtr[1];
System.IntPtr[]tokenDuplicate=newSystem.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=newWindowsIdentity(tokenDuplicate[0]);
impersonationContext=tempWindowsIdentity.Impersonate();
if(impersonationContext!=null)
{
CloseHandle(tokenDuplicate);
CloseHandle(token);
returntrue;
}
}
}
}
if(!token[0].Equals(System.IntPtr.Zero))
CloseHandle(token);
if(!tokenDuplicate[0].Equals(System.IntPtr.Zero))
CloseHandle(tokenDuplicate);
returnfalse;
}
privatevoidundoImpersonation()
{
impersonationContext.Undo();
}
</script>

注意:在線程上模擬特定用戶的進程的標識必須具有“作為操作係統的一部分”權限。默認情況下,Aspnet_wp.exe進程在名為ASPNET的計算機帳戶下運行。不過,此帳戶沒有模擬特定用戶所需的權限。如果您嚐試模擬特定用戶,則會出現一條錯誤信息。

要解決此問題,請使用下列方法之一:

•為ASPNET帳戶授予“作為操作係統的一部分”權限。

•在Machine.config文件的<processModel>配置部分中,將運行Aspnet_wp.exe進程所使用的帳戶更改為System帳戶。

最後更新:2017-04-02 00:06:36

  上一篇:go .NET中如何取得IP或者用戶名等信息
  下一篇:go 在ASP.NET中實現多文件上傳