爲什麼當我運行我的web應用程序本地主機時,下面的代碼工作正常,但是當我將它安裝到IIS服務器時,不能正常工作?System.DirectoryServices.AccountManagement.UserPrincipal - localhost但不是iis
using (HostingEnvironment.Impersonate())
{
UserPrincipal activeUser = UserPrincipal.Current;
String activeUserSid = activeUser.Sid.ToString();
String activeUserUPN = activeUser.UserPrincipalName;
}
請不要建議我堅持HttpContext.Current.User
,因爲它不提供訪問SID或UPN沒有到Active Directory的其他電話。
Web應用程序將由來自三個獨立域的Windows身份驗證用戶使用,Web服務器位於第四個域中。應用程序池配置爲在NetworkService
身份下運行,並且Web應用程序配置將身份模擬設置爲true。
的錯誤消息,當它運行在IIS是:
Error in Page_Load(): UserPrincipal.Current.
System.InvalidCastException: Unable to cast object of type 'System.DirectoryServices.AccountManagement.GroupPrincipal' to type 'System.DirectoryServices.AccountManagement.UserPrincipal'.
at System.DirectoryServices.AccountManagement.UserPrincipal.FindByIdentity(PrincipalContext context, IdentityType identityType, String identityValue)
at System.DirectoryServices.AccountManagement.UserPrincipal.get_Current()
at webapp.Details.Default.Page_Load(Object sender, EventArgs e)
編輯: 嘗試以下兩個不幸得到同樣的錯誤。
UserPrincipal userPrincipal = UserPrincipal.Current;
Response.Write(userPrincipal.Name);
Principal userOrGroup = UserPrincipal.Current;
Response.Write(userOrGroup.Name);
我認爲你是對的。 這也不管用... var wi = HttpContext.Current.User.Identity as WindowsIdentity;使用(wi.Impersonate()) UserPrincipal up = UserPrincipal.Current; activeUserUPN = up.UserPrincipalName; activeUserSid = up.Sid.ToString(); } – RichardD
「在IIS下運行線程的當前用戶」很可能是IIS App Pool Identity;但是當在Visual Studio,Casini或IIS Express上運行您的PC時,它就像您一樣運行。在這種情況下,您的身份由請求頁面的客戶端和本地運行的開發服務器共享。 –