zoukankan      html  css  js  c++  java
  • .net core在Linux下获取AD域信息

    .net core在Linux下获取AD域信息

    .net Core 2.1.4

    .net core现在System.DirectoryServices只支持Windows平台下使用。

    参考:

    https://github.com/dotnet/standard/pull/444

    https://github.com/dotnet/corefx/issues/2089

    private Dictionary<string,string> AuthenticateActiveDirectory(string username, string password)
    {
        Dictionary<string, string> dic = new Dictionary<string, string>();
        DirectoryEntry entry = new DirectoryEntry(_appConfiguration["LDAP:DE"], username, password);
        try
        {
            DirectorySearcher search = new DirectorySearcher(entry); 
            search.Filter = $"(SAMAccountName={username})";  
            SearchResult result = search.FindOne();
            if (result != null)
            {
                dic.Add("state","true");
                dic.Add("displayname", result.Properties["displayname"]?[0].ToString());
                dic.Add("mail",result.Properties["mail"]?[0].ToString());
            }
        }
        catch (Exception ex)
        {
            dic.Add("state", "false");
            dic.Add("errMsg",ex.Message);
        }
        return dic;
    }

    Novell.Directory.Ldap

    Novell.Directory.Ldap支持.net core2 Linux环境。

    public Dictionary<string, string> LdapAuthenticate(string username, string password)
    {
        Dictionary<string, string> dic = new Dictionary<string, string>();
        var ldapHost = _appConfiguration["LDAP:Host"];
        var ldapPort = _appConfiguration.GetValue<int>("LDAP:Port");
        var mailSuffix = _appConfiguration["LDAP:MailSuffix"];
        var searchBase = _appConfiguration["LDAP:SearchBase"];
        var loginDN = username;
        var sAMAccountName = username;
        if (username.Contains(mailSuffix))
            sAMAccountName = username.Substring(0, username.IndexOf(mailSuffix));
        else
            loginDN = $"{username}{mailSuffix}";
        
        var searchFilter = $"(sAMAccountName={sAMAccountName})";
        var attrs = _appConfiguration["LDAP:Attrs"].Split('|');
        try
        {
            var conn = new LdapConnection();
            conn.Connect(ldapHost, ldapPort);
            conn.Bind(loginDN, password);
            var lsc = conn.Search(searchBase, LdapConnection.SCOPE_SUB, searchFilter, attrs, false);
    
            while (lsc.hasMore())
            {
                LdapEntry nextEntry = null;
                try
                {
                    nextEntry = lsc.next();
                }
                catch (LdapException ex)
                {
                    Logger.Debug(ex.ToString(), ex);
                    continue;
                }
                var attributeSet = nextEntry.getAttributeSet();
                var ienum = attributeSet.GetEnumerator();
                while (ienum.MoveNext())
                {
                    var attribute = (LdapAttribute)ienum.Current;
                    var attributeName = attribute.Name.ToLower();
                    var attributeVal = attribute.StringValue;
                    if (attrs.Contains(attributeName))
                    {
                        dic.Add(attributeName, attributeVal);
                    }
                }
                dic.Add("state", "true");
            }
    
            conn.Disconnect();
        }
        catch (Exception ex)
        {
            dic.Add("state", "false");
            dic.Add("errMsg", ex.Message);
            Logger.Debug(ex.ToString(), ex);
        }
        return dic;
    }

    以上配置信息如下:

      "LDAP": {
        "_comment": "域帐号登录配置",
        "DE": "LDAP://xxx.com",
        "Host": "xx.xx.xx.xx",
        "Port": 389,
        "MailSuffix": "@xxx.com",
        "Attrs": "displayname|mail|sn",
        "SearchBase": "DC=xxx,DC=com",
        "UserRole": "User"
      },
  • 相关阅读:
    Why Choose Jetty?
    Jetty 的工作原理以及与 Tomcat 的比较
    Tomcat设计模式
    Servlet 工作原理解析
    Tomcat 系统架构
    spring boot 打包方式 spring boot 整合mybaits REST services
    wireshark udp 序列号 User Datagram Protocol UDP
    Maven 的聚合(多模块)和 Parent 继承
    缓存策略 半自动化就是mybaitis只支持数据库查出的数据映射到pojo类上,而实体到数据库的映射需要自己编写sql语句实现,相较于hibernate这种完全自动化的框架我更喜欢mybatis
    Mybatis解决sql中like通配符模糊匹配 构造方法覆盖 mybits 增删改
  • 原文地址:https://www.cnblogs.com/ddrsql/p/8516226.html
Copyright © 2011-2022 走看看