从活动目录获取 GUID 或本机 GUID

4
我正在编写一个Web服务,用于检查用户是否存在于Active Directory中并且用户帐户已启用。一旦检查通过,我会验证他们的用户帐户。一旦他们成功输入用户名和密码,我想获取我正在验证的人的GUIDNativeGuid。我想使用GUIDNativeGUID在SQL Server数据库内建立关系。
以下是我的实现方式:
public string isAuthenticated (string serverName, string userName, string pwd)
{
    string _serverName, _userName, _pwd;
    _serverName = serverName;
    _userName = userName;
    _pwd = pwd;

    string message;

    if (DoesUserExist (_userName) == true)
    {
        if (isActive(userName) == true)
        {
            try
            {
                DirectoryEntry entry = new DirectoryEntry(_serverName, _userName, _pwd);
                object nativeObject = entry.NativeObject;
                //issue is here
                string GUID = entry.Guid.ToString();
                string GUIDID = entry.NativeGuid;
                //end of issue
                message = "Successfully authenticated";
            }
            catch(DirectoryServicesCOMException ex)
            {
                    message = ex.Message;
            }
        }
        else
        {
                message = "Account is disabled";
        }
    }
    else
    {
        message = "There's an issue with your account.";
    }
    return message;      
}

当我尝试获取GUIDNativeGUID时,对于不同的用户,它每次都返回相同的ID

是否有其他方法可以获取Active Directory中不同对象的唯一标识符

谢谢

1个回答

6
如果您正在使用 .NET 3.5 或更新版本,您应该查看 System.DirectoryServices.AccountManagement(S.DS.AM)命名空间。在这里可以阅读有关此命名空间的所有信息: 基本上,您可以定义域上下文并轻松查找 AD 中的用户和/或组。
// set up domain context
using (PrincipalContext ctx = new PrincipalContext(ContextType.Domain))
{
    // find a user
    UserPrincipal user = UserPrincipal.FindByIdentity(ctx, _userName);

    if(user != null)
    {
       // get the GUID
       var objectGuid = user.Guid;
    }
}

新的S.DS.AM使得在AD中玩弄用户和组变得非常容易! 我现在没有AD来测试,但我希望这确实会给你用户对象的objectGuid属性值。

是的,这个可以工作。想要粘贴几乎相同的代码。UPV - MajkeloDev

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接