用于应用程序角色映射的Active Directory集成

问题描述 投票:0回答:1

我正在构建REST API(带有Entity Framework的.NET Core)和Angular前端。就安全性而言,我想要实现的是某种AD集成。我想向AD组和用户授予应用程序权限。将有一个管理员用户将这些用户/组映射到角色。最好的方法是什么?我是否应该使微服务每分钟查询一次AD,并刷新应用中的所有组和用户?还是每次管理员用户想要授予/撤消某人的许可时都与AD建立实时连接?我认为第二种方法可能不起作用,因为每次用户登录时,我都需要查询AD以查看其AD组并检查用户是否具有访问权限。

所以到目前为止,我想到的是JWT令牌身份验证和微服务,该服务将导入所有用户/组和它们之间的映射,因此所有身份验证都在应用程序/应用程序的数据库内进行,而不是不断查询AD(这很漂亮速度比我记得的要慢)。

您能给我一些改进的想法吗?也许我缺少一些解决方案? JWT令牌是前往此处的最佳方式吗?

c# security .net-core architecture active-directory
1个回答
0
投票

也许此代码可以为您提供帮助。注意Parameter.GetStringValue是个人自定义方法,请将其替换为所需的任何内容。


using System;
using System.Text;
using System.DirectoryServices;

    public class Ldap
    {
        private string _path;
        private string _filterAttribute;

        /// <summary>
        /// prepares LDAP path to use in this class. Something like LDAP://myLANdomain.local
        /// </summary>
        /// <param name="path">If empty will get the value on Parameter 'LANdomain'</param>
        public Ldap(string path = "")
        {
            if (String.IsNullOrEmpty(path))
            {
                string domain = Parameter.GetStringValue("LANdomain");
                if (String.IsNullOrEmpty(domain))
                    throw new Exception("Domain is not defined");

                path = "LDAP://" + domain; //Fully-qualified Domain Name
            }

            _path = path;
        }

        //Error Error 0x80005000 go to IIS and recycle App Pool
        public bool IsAuthenticated(string username, string pwd, string domain ="")
        {

            if (String.IsNullOrEmpty(domain))
                domain = Parameter.GetStringValue("LANdomain");

            if (String.IsNullOrEmpty(domain))
                throw new Exception("Domain is not defined");

            string domainAndUsername = domain + @"\" + username;
            using (DirectoryEntry entry = new DirectoryEntry(_path, domainAndUsername, pwd))
            {
                try
                {
                    //Bind to the native AdsObject to force authentication.
                    object obj = entry.NativeObject;

                    using (DirectorySearcher search = new DirectorySearcher(entry))
                    {
                        //"(&(objectClass=user)(objectCategory=person)(|(SAMAccountName=*{0}*)(cn=*{0}*)(gn=*{0}*)(sn=*{0}*)(email=*{0}*)))"
                        search.Filter = "(&(objectCategory=person)(objectClass=user)(SAMAccountName=" + username + "))";
                        search.PropertiesToLoad.Add("cn");
                        SearchResult result = search.FindOne();

                        if (null == result)
                        {
                            return false;
                        }

                        //Update the new path to the user in the directory.
                        _path = result.Path;
                        _filterAttribute = (String)result.Properties["cn"][0];

                        return true;
                    }


                }
                //catch (DirectoryServicesCOMException cex)
                catch //(Exception ex)
                {
                    throw;
                    //throw new Exception("Error authenticating LDAP user. " + ex.Message);
                }


            }


        }



    }
© www.soinside.com 2019 - 2024. All rights reserved.