从Active Directory获取GUID或本机GUID

smr*_*mr5 4 c# directoryservices web-services active-directory

我正在编写一个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;      
}
Run Code Online (Sandbox Code Playgroud)

当我试图获得GUID或者NativeGUIDID每次都为不同的用户返回相同的时间.

我可以采用不同的方法来获取UNIQUE IDActive Directory中的不同对象吗?

谢谢

mar*_*c_s 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;
    }
}
Run Code Online (Sandbox Code Playgroud)

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