2012-06-21 29 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;' 然后试图解决方案,并且它是只有一个部署到我的生产服务器时工作......不知道为什么。所有在当地工作... –

相关问题