繁体   English   中英

如何从内部ASP Web应用程序获取Windows身份验证的用户GUID?

[英]how to get windows authenticated user guid from internal asp web application?

好的,所以这在我的VS测试服务器上正常工作,但是一旦发布到IIS,它就会中断。 为此,我需要能够掌握当前登录到计算机的用户的GUID(请不要使用SID)。 使用DirectoryServices,这是我最初的实现:

var guid = UserPrincipal.Current.Guid.ToString();

这给我一个错误,我无法从GroupPrincipal强制转换为UserPrincipal。 因此,听起来该应用程序正试图以某个经过身份验证的组或某种身份运行。 我意识到,通常的方法将类似于HttpContext.Current.User.Identity,但是由于它没有Guid属性,并且当我尝试将其转换为SID并运行LDAP查询时,我不确定该怎么做。 ,它将引发异常。 有人可以帮助我采取必要的措施来实现这一目标吗?

谢谢

更新:好的,这是我最近的尝试:

protected string GetUserGuid()
    {
        var pc = new PrincipalContext(ContextType.Domain);
        var windowsID = HttpContext.Current.User.Identity;
        var up = UserPrincipal.FindByIdentity(pc, windowsID.Name);
        return up.Guid.ToString();
    }

我得到的例外是:

[COMException(0x8007054b):指定的域不存在或无法联系。 ]
System.DirectoryServices.DirectoryEntry.Bind(布尔throwIfFail)+788 System.DirectoryServices.DirectoryEntry.Bind()+44
System.DirectoryServices.DirectoryEntry.get_AdsObject()+42
System.DirectoryServices.PropertyValueCollection.PopulateList()+29
System.DirectoryServices.PropertyValueCollection..ctor(DirectoryEntry条目,字符串propertyName)+63
System.DirectoryServices.PropertyCollection.get_Item(字符串propertyName)+163
System.DirectoryServices.AccountManagement.PrincipalContext.DoLDAPDirectoryInitNoContainer()+436 System.DirectoryServices.AccountManagement.PrincipalContext.DoDomainInit()+51 System.DirectoryServices.AccountManagement.PrincipalContext.Initialize()+141 System.DirectoryServices.AccountManagement.PrincipalContext.get_Querytx +42 System.DirectoryServices.AccountManagement.Principal.FindByIdentityWithTypeHelper(PrincipalContext上下文,类型PrincipalType,可空`1 identityType,字符串identityValue,DateTime refDate)+29
System.DirectoryServices.AccountManagement.UserPrincipal.FindByIdentity(PrincipalContext上下文,字符串identityValue)+95 TicketsToMe.GetUserGuid()+123
TicketsToMe.Page_Load(对象发件人,EventArgs e)+38
System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp,Object o,Object t,EventArgs e)+25 System.Web.UI.Control.LoadRecursive()+71 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint,布尔值includeStagesAfterAsyncPoint)+3048

如果某些内容在本地运行,但不在IIS上运行,则可能是权限问题。 您是否在IIS上检查了信任级别? 本地VS测试服务器在完全信任下运行,但IIS(尤其是7)网站可能设置为“中”或更低。 这导致我以前遇到过类似问题,例如在本地运行但不在IIS上运行的问题。

同样值得检查应用程序池用户的权限。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM