C# 通过Active Directory登录使用自定义角色的最佳方法
我正在寻找解决我的问题的最佳解决方案,我们想要做的是使用active directory进行基本登录,这样我们就不必管理密码等。但我们希望创建自定义角色,而不是使用广告组。因此,我可能有一个角色,将等于5个不同的广告组 我想做的是通过c#将特定组中的所有用户转储到一个自定义用户表中,该自定义用户表链接到roleid to userid表。有人对此有什么想法吗?但我还需要以某种方式管理自定义用户表,如果有人从其中一个组中删除,那么他们需要释放访问权限,不知道如何处理该访问权限 目前,我的方法如下所示:C# 通过Active Directory登录使用自定义角色的最佳方法,c#,active-directory,roles,C#,Active Directory,Roles,我正在寻找解决我的问题的最佳解决方案,我们想要做的是使用active directory进行基本登录,这样我们就不必管理密码等。但我们希望创建自定义角色,而不是使用广告组。因此,我可能有一个角色,将等于5个不同的广告组 我想做的是通过c#将特定组中的所有用户转储到一个自定义用户表中,该自定义用户表链接到roleid to userid表。有人对此有什么想法吗?但我还需要以某种方式管理自定义用户表,如果有人从其中一个组中删除,那么他们需要释放访问权限,不知道如何处理该访问权限 目前,我的方法如下所
using (PrincipalContext ctx = new PrincipalContext(ContextType.Domain, "SETON"))
{
//Gets all Users in a AD Group
using (GroupPrincipal grp = GroupPrincipal.FindByIdentity(ctx, IdentityType.SamAccountName, groupName))
{
//Mkae sure group is not null
if (grp != null)
{
foreach (Principal p in grp.GetMembers())
{
//Sets up Variables to enter into Finance User Table
string UserName = p.SamAccountName;
string DisplayName = p.Name;
string emailAddress = p.UserPrincipalName;
//Get users detials by user
using (UserPrincipal userPrincipal = UserPrincipal.FindByIdentity(ctx, IdentityType.SamAccountName, UserName))
{
if (userPrincipal != null)
{
//Test to see if user is in AD Group
bool inRole = userPrincipal.IsMemberOf(grp);
if (inRole)
{
//Test to See if UserName already exists in Finance User Table
var ds = User.GetUserList(UserName);
//If don't exist add them and to the new Role
if (ds.Tables[0].Rows.Count == 0)
{
//Add User to FinanceUSer Table
Seton.Roster.User user = new User();
user.UserName = UserName;
user.Name = DisplayName;
int id = user.Save();
//Get RoleID by RoleName with Method
var roleDS = SecurityRole.GetRoleList(roleName);
if (roleDS.Tables[0].Rows.Count > 0)
{
var roleDR = roleDS.Tables[0].Rows[0];
int roleid = Convert.ToInt32(roleDR["roleid"].ToString());
SecurityRoleUserLink.AddNewLink(roleid, id);
}
}
//if they exist just Get thier userid and add them to new Role
else
{
//Get UserID of existing FinanceUser
var userDR = ds.Tables[0].Rows[0];
int id = Convert.ToInt32(userDR["userid"].ToString());
//Get RoleID by RoleName with Method
var roleDS = SecurityRole.GetRoleList(roleName);
if (roleDS.Tables[0].Rows.Count > 0)
{
var roleDR = roleDS.Tables[0].Rows[0];
int roleid = Convert.ToInt32(roleDR["roleid"].ToString());
//Test to see if user already in this role
if(!SecurityRoleUserLink.UserInRole(id,roleid))
SecurityRoleUserLink.AddNewLink(roleid, id);
我认为你在重新发明轮子。看看像(AzMan)这样的产品,或者它是 这些允许您定义“操作”(即用户可以执行的任务),并将这些操作映射到广告组。除此之外,他们会让你做更多的事情,但至少他们肯定会满足你的需求
AzMan配置可以存储在XML、数据库或AD中,因此在这方面也非常灵活。我已经用了几种方法实现了这一点,但答案实际上是关于实现安全性有多复杂。你是做现场级还是记录级
- 优点-帐户在你的数据库中,如果广告有问题,你不是SOL
- 缺点-需要重新填充并按计划进行,并且可能会失去同步
- 优点-让广告做你的工作,这样你就不必在应用程序中管理它