zoukankan      html  css  js  c++  java
  • .Netcore AD 操作

    1.   NUget 引入:

         System.DirectoryServices

                      System.DirectoryServices.AccountManagement

    2  登陆种情况:有登陆界面和无登陆界面

    2.1. 有登陆界面,有表单,输入用户名密码,后台拿到用户名密码,找AD 服务器验证

    protected virtual bool ValidateCredentials(PrincipalContext principalContext, string userNameOrEmailAddress, string plainPassword)
            {
                return principalContext.ValidateCredentials(userNameOrEmailAddress, plainPassword, ContextOptions.Negotiate);
            }

    2.2. 无登陆界面,浏览器弹出密码输入框。

    需要做的有:

    1. IIS 支持安装  Windows Authentication
    2. .NetCore 选择windows 认证。
    3. Action上带上 Authoriz(…)   (.NetCore 3.1 模板,自己会加上这一步。)

    3.根据组名获取邮箱(下面代码是先获取组成员,再得到他的邮箱):

    public string[] GetEmails(string groupName)
            {
                DirectorySearcher ds = new DirectorySearcher();
                ds.SearchRoot = new DirectoryEntry("LDAP://" + _appConfiguration["Container"]);
                ds.Filter = $"(&(objectClass=group)(cn={groupName}))";
                var ms = new List<string>();
                foreach (SearchResult result in ds.FindAll())
                {
                    DirectoryEntry deGroup = new DirectoryEntry(result.Path);
                    var pcoll = deGroup.Properties["member"];
                    for (int l = 0; l < pcoll.Count; l++)
                    {
                        var n = pcoll[l].ToString();
                        DirectoryEntry deUser = new DirectoryEntry($"LDAP://{n}");
                        ms.Add(deUser.Properties["mail"][0].ToString());
                    }
                }
                return ms.ToArray();
            } 

     4.对于Windows认证,很容易获取到 Name.

    怎么根据Name,获取组呢? 既然在域中,网站服务器知道AD服务器在哪儿,代码不用关心这个了。

    下面代码实现这个。

    DirectoryEntry entry = new DirectoryEntry();
    
    DirectorySearcher search2 = new DirectorySearcher(entry);
    search2.Filter = $"(cn={Name})";
    search2.PropertiesToLoad.Add("memberOf");
               
               
    SearchResult result2 = search2.FindOne();
    
    foreach (var item in result2.Properties["memberof"])
        {
            Console.WriteLine(item.ToString());
        } 
                    
    Console.Read();
    

      

    气功波(18037675651)
  • 相关阅读:
    【数学】三分法
    【数学】【背包】【NOIP2018】P5020 货币系统
    【数学】【CF27E】 Number With The Given Amount Of Divisors
    【单调队列】【P3957】 跳房子
    【极值问题】【CF33C】 Wonderful Randomized Sum
    【DP】【CF31E】 TV Game
    【神仙题】【CF28D】 Don't fear, DravDe is kind
    【线段树】【CF19D】 Points
    【字符串】KMP字符串匹配
    【二维树状数组】【CF10D】 LCIS
  • 原文地址:https://www.cnblogs.com/qgbo/p/13573263.html
Copyright © 2011-2022 走看看