gpt4 book ai didi

.NET:如何在 Active Directory 中查找用户?

转载 作者:行者123 更新时间:2023-12-04 02:18:10 25 4
gpt4 key购买 nike

如何在 Active Directory 中查找用户?

一些示例用户名是:

  • avatopia\ian
  • 头像\ian
  • ian@avatopia.com
  • ian@avatopia.local
  • avatopia.com\ian

  • 需要注意的是,我不知道域的名称, and i shouldn't be hard-coding it .

    有一些 sample code on stack-overflow那失败了。
    using System.DirectoryServices;

    /// <summary>
    /// Gets the email address, if defined, of a user from Active Directory.
    /// </summary>
    /// <param name="userid">The userid of the user in question. Make
    /// sure the domain has been stripped first!</param>
    /// <returns>A string containing the user's email address, or null
    /// if one was not defined or found.</returns>
    public static string GetEmail(string userid)
    {
    DirectorySearcher searcher;
    SearchResult result;
    string email;

    // Check first if there is a slash in the userid
    // If there is, domain has not been stripped
    if (!userid.Contains("\\"))
    {
    searcher = new DirectorySearcher();
    searcher.Filter = String.Format("(SAMAccountName={0})", userid);
    searcher.PropertiesToLoad.Add("mail");
    result = searcher.FindOne();
    if (result != null)
    {
    email = result.Properties["mail"][0].ToString();
    }
    }

    return email;
    }

    它特别确保您没有传递完整的用户名。例如
    Bad: avatopia\ian
    Bad: avatar\ian
    Good: ian
    Good: ian

    因为不允许你传域,所以无法区分两个用户
    ian
    ian

    另一个人有 the same question在 sackoverflow 上,但接受的答案说你必须

    first locate the naming context for the required domain



    don't know什么是“命名上下文”,我不知道“所需域”是什么。我真的宁愿不编写正则表达式来尝试将用户名解析为域名和帐户名,例如
    domain.something\user-name

    进入
    domain.something
    user-name

    因为我知道会有一些边缘情况我会出错。我想要在事件目录中查找用户的正确的、预期的方法。

    CodeProject 上有一个不错的页面 How to do almost everything in Active Directory ,但不能通过用户名查找用户信息

    我希望我可以给我的域 Controller ( whoever it is, where ever it is, whatever it's called ) 一个用户名,它会找出该用户属于哪个域,与该域 Controller 交谈,并完成工作。

    最佳答案

    这对我有用。

    您应该能够区分不同域 Controller (即域/用户名)上的不同用户,因为 ldappaths 会有所不同。据你说你不在乎,因为你是not specifying an ldappath .

    您正在就剥离 User.Identity.Name 的域/进行出价交易。但我不确定你在担心什么,你只需要把字符串切成两半,第一次遇到 '\' 是时候切了。

    如果你不喜欢,你可以使用“正确的方式”:http://msdn.microsoft.com/en-us/library/ms973834.aspx

    这个也不错 http://geekswithblogs.net/mhamilton/archive/2005/09/30/55621.aspx

          /// This is some imaginary code to show you how to use it

    Session["USER"] = User.Identity.Name.ToString();
    Session["LOGIN"] = RemoveDomainPrefix(User.Identity.Name.ToString()); // not a real function :D
    string ldappath = "LDAP://your_ldap_path";
    // "LDAP://CN=<group name>, CN =<Users>, DC=<domain component>, DC=<domain component>,..."


    Session["cn"] = GetAttribute(ldappath, (string)Session["LOGIN"], "cn");
    Session["displayName"] = GetAttribute(ldappath, (string)Session["LOGIN"], "displayName");
    Session["mail"] = GetAttribute(ldappath, (string)Session["LOGIN"], "mail");
    Session["givenName"] = GetAttribute(ldappath, (string)Session["LOGIN"], "givenName");
    Session["sn"] = GetAttribute(ldappath, (string)Session["LOGIN"], "sn");


    /// working code

    public static string GetAttribute(string ldappath, string sAMAccountName, string attribute)
    {
    string OUT = string.Empty;

    try
    {
    DirectoryEntry de = new DirectoryEntry(ldappath);
    DirectorySearcher ds = new DirectorySearcher(de);
    ds.Filter = "(&(objectClass=user)(objectCategory=person)(sAMAccountName=" + sAMAccountName + "))";

    SearchResultCollection results = ds.FindAll();

    foreach (SearchResult result in ds.FindAll())
    {
    OUT = GetProperty(result, attribute);
    }
    }
    catch (Exception t)
    {
    // System.Diagnostics.Debug.WriteLine(t.Message);
    }

    return (OUT != null) ? OUT : string.Empty;
    }

    public static string GetProperty(SearchResult searchResult, string PropertyName)
    {
    if (searchResult.Properties.Contains(PropertyName))
    {
    return searchResult.Properties[PropertyName][0].ToString();
    }
    else
    {
    return string.Empty;
    }
    }

    对于域/用户名
        public static string GetDomain(string s)
    {
    int stop = s.IndexOf("\\");
    return (stop > -1) ? s.Substring(0, stop + 1) : null;
    }

    public static string GetLogin(string s)
    {
    int stop = s.IndexOf("\\");
    return (stop > -1) ? s.Substring(stop + 1, s.Length - stop - 1) : null;
    }

    对于 username@domain 样式
       public static string GetDomain(string s) //untested
    {
    int stop = s.IndexOf("@");
    return (stop > -1) ? s.Substring(stop + 1, s.Length - stop - 1) : null;
    }


    public static string GetLogin(string s) //untested
    {
    int stop = s.IndexOf("@");
    return (stop > -1) ? s.Substring(0, stop) : null;
    }

    关于.NET:如何在 Active Directory 中查找用户?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/324518/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com