Getting authenticate AD users objectGuid from asp.net

前端 未结 3 1620
名媛妹妹
名媛妹妹 2020-12-30 13:36

I am using windows authentication within an ASP.NET application. I am wondering how to best get the objectGuid from the currently logged in user?

Regards, Egil.

3条回答
  •  鱼传尺愫
    2020-12-30 14:09

    The suggest solutions are rather expensive. Rather than searching by domain and username, a better solution is to use the SID to lookup the account:

    // using System.Security.Principal;
    IPrincipal userPrincipal = HttpContext.Current.User;
    WindowsIdentity windowsId = userPrincipal.Identity as WindowsIdentity;
    if (windowsId != null)
    {
        SecurityIdentifier sid = windowsId.User;
    
        using(DirectoryEntry userDe = new DirectoryEntry("LDAP://"))
        {
            Guid objectGuid = new Guid(userDe.NativeGuid);
        }
    }
    

提交回复
热议问题