【问题标题】:.NET Core 6 Windows auth and Active Directory group based permissions.NET Core 6 Windows 身份验证和基于 Active Directory 组的权限
【发布时间】:2022-12-22 04:09:55
【问题描述】:

如何从 dotnet core 6 中公司的 Active Directory (WinServer) 获取用户数据(用户名和姓氏以及用户组)?

我安装了 Identity 包,但该应用程序需要使用 Windows Auth 和 Active Directory 组才能获得权限。

如何

【问题讨论】:

标签: asp.net-mvc .net-core active-directory windows-authentication asp.net-core-6.0


【解决方案1】:

经过更多谷歌搜索后,我发现它对我有用

  1. 创建一个将扩展 IClaimsTransformation 的新类。

    public class ClaimsTransformer : IClaimsTransformation  
    {  
        public Task<ClaimsPrincipal> TransformAsync(ClaimsPrincipal principal)  
        {  
            var wi = (WindowsIdentity)principal.Identity;  
            if (wi.Groups != null)  
            {  
                foreach (var group in wi.Groups) //-- Getting all the AD groups that user belongs to---  
                    {  
                        try  
                        {  
                            var claim = new Claim(wi.RoleClaimType, group.Value);  
                            wi.AddClaim(claim);                          
                        }  
                        catch (Exception ex)  
                        {  
                           throw ex;  
                        }  
                     }  
             }              
              return Task.FromResult(principal);  
        }  
    }
    
  2. 在 Program.cs 中将 Singleton 添加到构建器

    builder.Services.AddSingleton<IClaimsTransformation, ClaimsTransformer>();
    
  3. 在控制器中使用 [Authorize(Roles = "YourGroupName")]

    对于单个链接:

    [Authorize(Roles = "YourGroupName")]
    public IActionResult Privacy()
    {
       return View();
    }
    

    对于整个控制器:

    [Authorize(Roles = "YourGroupName")]
    public class HomeController : Controller
    {
        public IActionResult Index()
        {
            return View();
        }
        
    }
    

    指南来自: https://www.c-sharpcorner.com/article/authorization-using-windows-active-directory-groups-in-net-core-2-razor-pages/

【讨论】:

  • 是的,这是个好方法。请注意,硬编码您的组名不是最佳做法。您至少可以将其设置为常量,但这不允许您将不同的组用于较低的环境与生产。我所做的是: - 为不同角色创建一组常量(如果每个环境只有一个组,则可能只有一个 - 将允许的组放入 appsettings - 将 appsettings 类注入声明转换器 - 声明转换器具有将 appsettings 组映射到常量并将常量放入声明中的逻辑 - 授权标记引用常量
  • 感谢您的评论。在生产中,我用静态细节制作了单独的类,我把我需要的所有组都放在那里。遗憾的是我的知识水平不足以注射,但是当我学习这个话题时,我会回到你的建议
【解决方案2】:
【解决方案3】:

用于连接到 AD

   using(  PrincipalContext ctx = new PrincipalContext(ContextType.Domain, "YOURDOMAIN")){}

将您的群组放入“YourDomain”

要从 AD 获取信息,请使用此代码

using (var context = new PrincipalContext(ContextType.Domain, "yourdomain.com"))
{
    using (var searcher = new PrincipalSearcher(new UserPrincipal(context)))
    {
        foreach (var result in searcher.FindAll())
        {
            DirectoryEntry de = result.GetUnderlyingObject() as DirectoryEntry;
            Console.WriteLine("First Name: " + de.Properties["givenName"].Value);
            Console.WriteLine("Last Name : " + de.Properties["sn"].Value);
            Console.WriteLine("SAM account name   : " + de.Properties["samAccountName"].Value);
            Console.WriteLine("User principal name: " + de.Properties["userPrincipalName"].Value);
            Console.WriteLine();
        }
    }
}
Console.ReadLine();

通过此代码,您将获得所有用户信息

如果您想从 Active Directory 登录或编辑用户信息,我会向您发送完整代码

【讨论】:

  • 谢谢你的代码!我不需要编辑用户,只需获取用户和用户组。如果我需要限制活动目录组的访问?例如,我有用户、主管和管理员。我的 MVC 应用程序中有区域。另外,我现在正在为帐户使用 Microsoft Identity,我需要保存一些额外的信息。有一些外部库,如 Facebook、Twitter 等,但没有 Active Directory。
猜你喜欢
  • 1970-01-01
  • 2017-09-05
  • 1970-01-01
  • 1970-01-01
  • 2018-05-18
  • 2022-10-04
  • 1970-01-01
  • 2020-03-28
  • 2019-10-31
相关资源
最近更新 更多