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.
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);
}
}