为什么当我运行我的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时,它就像您一样运行。在这种情况下,您的身份由请求页面的客户端和本地运行的开发服务器共享。 –