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)
  • 相关阅读:
    requests-验证码登录
    python接口
    Xmanager6
    jmeter提取变量注意事项
    badboy录制
    Config 多账户多区域数据聚合
    AWS Aurora数据库 Multi-Master
    确定客户主密钥的过去使用情况
    将应用程序部署到 AWS Elastic Beanstalk 环境
    VPC Peering 具有特定路由的配置
  • 原文地址:https://www.cnblogs.com/qgbo/p/13573263.html
Copyright © 2011-2022 走看看