2012-06-21 21 views
7

爲什麼當我運行我的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); 

回答

1

好像需要一些其他的方法來確定用戶。
這裏來自msdn的描述屬性:
「獲取一個用戶主體對象,該對象表示線程正在運行的當前用戶。」
因此,UserPrincipal.Current返回運行IIS的用戶。

http://msdn.microsoft.com/en-us/library/system.directoryservices.accountmanagement.userprincipal.aspx

+0

我認爲你是對的。 這也不管用... var wi = HttpContext.Current.User.Identity as WindowsIdentity;使用(wi.Impersonate()) UserPrincipal up = UserPrincipal.Current; activeUserUPN = up.UserPrincipalName; activeUserSid = up.Sid.ToString(); } – RichardD

+1

「在IIS下運行線程的當前用戶」很可能是IIS App Pool Identity;但是當在Visual Studio,Casini或IIS Express上運行您的PC時,它就像您一樣運行。在這種情況下,您的身份由請求頁面的客戶端和本地運行的開發服務器共享。 –

3

我在部署UserPrincipal.Current時遇到了很多問題,但仍不能完全理解原因。

我最終結束了使用PrincipalSearcher,並創建了以下函數來執行我認爲UserPrincipal.Current正在做的事情。

private UserPrincipal GetActiveDirectoryUser(string userName) 
{ 
    using(var ctx = new PrincipalContext(ContextType.Domain)) 
    using(var user = new UserPrincipal(ctx) { SamAccountName = userName}) 
    using(var searcher = new PrincipalSearcher(user)) 
    { 
     return searcher.FindOne() as UserPrincipal; 
    } 
} 

和我經過System.Web.HttpContext.Current.User.Identity.Name到該法作爲用戶名。

+0

我得到這個錯誤:'System.ObjectDisposedException:無法訪問一個處置的對象。對象名稱:'PrincipalContext'。 System.DirectoryServices.AccountManagement.PrincipalContext.CheckDisposed()at System.DirectoryServices.AccountManagement.PrincipalContext.get_ContextType()at System.DirectoryServices.AccountManagement.Principal.get_Name()at System.String.Concat(Object [] args)at Admin_Click (Object sender,EventArgs e)' – SearchForKnowledge

+1

我在我的MVC應用程序中嘗試了多個東西。 DirectoryEntry de = new DirectoryEntry(「WinNT://」+ Environment。UserDomainName +「/」+ userId); 字符串username = de.Properties [ 「全名」] Value.ToString();' 然後我試圖'System.DirectoryServices.AccountManagement.UserPrincipal.Current.DisplayName;' 然後試圖解決方案,並且它是隻有一個部署到我的生產服務器時工作......不知道爲什麼。所有在當地工作... –

相關問題