【问题标题】:Active Directory integration for app role mapping用于应用角色映射的 Active Directory 集成
【发布时间】:2020-03-19 13:42:52
【问题描述】:
我正在构建一个 REST API(带有实体框架的 .NET Core)和 Angular 前端。我想在安全方面实现的是某种 AD 集成。我想向 AD 组和用户授予应用程序权限。将有一个管理员用户将这些用户/组映射到角色。最好的方法是什么?我是否应该创建一个每分钟查询一次 AD 并刷新应用中所有组和用户的微服务?或者,每次管理员用户想要授予/撤销某人的权限时,都可能与 AD 建立实时连接?我认为第二种方法可能行不通,因为每次用户登录时,我都需要查询 AD 以查看他/她的 AD 组并检查用户是否具有访问权限。
所以到目前为止我想出的是 JWT 令牌身份验证和一个微服务,它将导入所有用户/组和它们之间的映射,因此所有身份验证都发生在应用程序/应用程序的数据库中,而不是不断查询 AD(即据我记得相当慢)。
你能给我一些改进的想法吗?也许我错过了一些解决方案? JWT 令牌是最好的方式吗?
【问题讨论】:
标签:
c#
security
.net-core
architecture
active-directory
【解决方案1】:
也许这段代码可以帮助你。请注意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);
}
}
}
}