如何从内部aspweb应用程序中获取经过windows身份验证的用户guid

本文关键字:windows 经过 身份验证 guid 用户 获取 内部 aspweb 应用程序 | 更新日期: 2023-09-27 18:29:23

好的,所以这在我的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)+788System.DirectoryServices.DirectoryEntry.Bind()+44
System.DirectoryServices.DirectoryEntry.get_AdsObject()+42
System.DirectoryServices.PropertyValueCollection.PopulateList()+29
System.DirectoryServices.PropertyValueCollection.ctor(DirectoryEntryentry,String propertyName)+63
System.DirectoryServices.PropertyCollection.get_Item(字符串propertyName)+163
System.DirectoryServices.AccountManagement.PPrincipalContext.DoLDAPDirectoryInitNoContainer()+436 System.DirectoryServices.AccountManagement.PPrincipalContext.DoDomainInit()+51 System.DirectoryServices.AccountManagement.PrinipalContext.Initialize()+141 System.DirectoryServices.AccountManagement.PPrincipalContext.get_QueryCtx()+42 System.DirectoryServices.AccountManagement.Prinipal.FindByIdentityWithTypeHelper(PrincipalContextcontext,Type principalType,可为null `1 identityType,StringidentityValue,DateTime refDate)+29
System.DirectoryServices.AccountManagement.UserPrincipal.FindByIdentity(PrincipalContextcontext,String identityValue)+95 TicketsToMe.GetUserGuid()+123
TicketsToMe.Page_Load(对象发件人,EventArgs e)+38
System.Web.Util.CaliHelper.EventArgFunctionCaller(IntPtr-fp,对象o、 对象t,EventArgs e)+25 System.Web.UI.Control.LoadRecursive()+71 System.Web.UI.Page.ProcessRequestMain(布尔值包括同步点之前的阶段,布尔值包括异步点之后的阶段)+3048

如何从内部aspweb应用程序中获取经过windows身份验证的用户guid

如果某个东西在本地运行,但不在IIS上运行,则可能是权限问题。您是否检查了IIS的信任级别?本地VS测试服务器运行在完全信任上,但IIS(尤其是7)网站可能设置为Medium或更低。这以前给我带来过类似的问题,即在本地运行但不在IIS上运行的东西。

此外,值得检查应用程序池用户的权限。