c# - 从 Active Directory 获取 GUID 或 native GUID

标签 c# web-services active-directory directoryservices

我正在编写一个 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 中的不同对象获取 UNIQUE ID 吗?

谢谢

最佳答案

如果您使用的是 .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 中与用户和组一起玩真的很容易!我现在没有要测试的广告 - 但我希望这确实会给你用户对象的 objectGuid 属性值。

关于c# - 从 Active Directory 获取 GUID 或 native GUID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27447823/

相关文章:

asp.net - ASP.NET Web 服务应用程序中 INI 文件的保存位置

java - Jboss EAP6.4是否支持 Jersey Web服务的 "JSONObject"映射?

java - 从 Java 中的 SOAP C# Web 服务获取单个数据

active-directory - Tridion CME 如何从包含许多 AD-LDAP 的域中选择特定的 AD-LDAP?

c# - 如何查询 Active Directory 中的更改,包括已删除的对象?

c# - 使用 NEST 将 List<object> 插入 Elasticsearch

c# - 如何在Windows Phone 8中使用经度和纬度获取位置

c# - 删除文本文件中的行并替换字符串

c# - AAD : How uniquely identify users in a Web API?

c# - 如何在 C# 中将 char 转换为 char*?