Ric*_*rdD 9 asp.net impersonation userprincipal
为什么我运行我的Web应用程序localhost时,下面的代码工作正常,但是当我将它安装到IIS服务器时却没有?
using (HostingEnvironment.Impersonate())
{
UserPrincipal activeUser = UserPrincipal.Current;
String activeUserSid = activeUser.Sid.ToString();
String activeUserUPN = activeUser.UserPrincipalName;
}
Run Code Online (Sandbox Code Playgroud)
请不要建议我坚持使用HttpContext.Current.User,因为如果没有额外的Active Directory调用,它就无法访问SID或UPN.
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(对象发件人,EventArgs的)
编辑:尝试以下两个,不幸的是得到相同的错误.
UserPrincipal userPrincipal = UserPrincipal.Current;
Response.Write(userPrincipal.Name);
Run Code Online (Sandbox Code Playgroud)
Principal userOrGroup = UserPrincipal.Current;
Response.Write(userOrGroup.Name);
Run Code Online (Sandbox Code Playgroud)
似乎需要一些其他方法来确定用户。
msdn 中对属性的描述如下:
“获取一个用户主体对象,该对象表示线程正在其下运行的当前用户。”
因此,UserPrincipal.Current 返回 IIS 正在运行的用户。
http://msdn.microsoft.com/en-us/library/system.directoryservices.accountmanagement.userprincipal.aspx