【发布时间】:2012-06-23 16:48:30
【问题描述】:
为什么下面的代码在我运行 Web 应用程序 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 上运行时的错误信息是:
Page_Load() 中的错误:UserPrincipal.Current。
System.InvalidCastException:无法转换类型的对象 'System.DirectoryServices.AccountManagement.GroupPrincipal' 键入 'System.DirectoryServices.AccountManagement.UserPrincipal'。
在 System.DirectoryServices.AccountManagement.UserPrincipal.FindByIdentity(PrincipalContext 上下文,IdentityType identityType,字符串 identityValue)
在 System.DirectoryServices.AccountManagement.UserPrincipal.get_Current()
在 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);
【问题讨论】:
标签: asp.net impersonation userprincipal