System.DirectoryServices.AccountManagement.UserPrincipal - 本地主机,但不是IIS [英] System.DirectoryServices.AccountManagement.UserPrincipal - localhost but not iis

查看:442
本文介绍了System.DirectoryServices.AccountManagement.UserPrincipal - 本地主机,但不是IIS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么下面做工精细code,当我跑我的web应用程序本地主机,但不是当我把它安装到IIS服务器?

 使用(HostingEnvironment.Impersonate())
{
    UserPrincipal activeUser = UserPrincipal.Current;
    串activeUserSid = activeUser.Sid.ToString();
    字符串activeUserUPN = activeUser.UserPrincipalName;
}

请不要建议我坚持 HttpContext.Current.User ,因为它不提供没有到Active Directory额外调用访问SID或UPN。

Web应用程序会被Windows身份验证使用来自三个不同的域用户,Web服务器在第四域主办。应用程序池配置为在网​​络服务身份和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);

主要userOrGroup = UserPrincipal.Current;
的Response.Write(userOrGroup.Name);


解决方案

这似乎是需要一些其他的方法来确定用户。

从MSDN这里描述的属性:

获取重新presents线程运行时所使用的当前用户设置用户主体对象。

因此,UserPrincipal.Current返回在什么样的IIS中运行的用户。


<一href=\"http://msdn.microsoft.com/en-us/library/system.directoryservices.accountmanagement.userprincipal.aspx\" rel=\"nofollow\">http://msdn.microsoft.com/en-us/library/system.directoryservices.accountmanagement.userprincipal.aspx

Why does the code below work fine when I run my web application localhost but not when I install it to an IIS server?

using (HostingEnvironment.Impersonate())
{
    UserPrincipal activeUser = UserPrincipal.Current;
    String activeUserSid = activeUser.Sid.ToString();
    String activeUserUPN = activeUser.UserPrincipalName;
}

Please don't suggest I stick with HttpContext.Current.User as it doesn't provide access to SID or UPN without additional calls to Active Directory.

The web application will be used by Windows authenticated users from three separate domains, the web server is hosted in a fourth domain. The Application Pool is configured to run under the NetworkService identity and the web app configuration has identity impersonation set to true.

The error message when it runs on IIS is:

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)

EDIT: Tried both the following and unfortunately get the same error.

UserPrincipal userPrincipal = UserPrincipal.Current;
Response.Write(userPrincipal.Name);

Principal userOrGroup = UserPrincipal.Current;
Response.Write(userOrGroup.Name);

解决方案

It seems like need some other method to determine user.
Here description from msdn for property:
"Gets a user principal object that represents the current user under which the thread is running."
So, UserPrincipal.Current returns user under what IIS running.

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

这篇关于System.DirectoryServices.AccountManagement.UserPrincipal - 本地主机,但不是IIS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆