【问题标题】:Integrated Windows Authentication in ABP frameworkABP 框架中的集成 Windows 身份验证
【发布时间】:2018-02-16 06:03:06
【问题描述】:

我正在尝试将 ABP 与 Windows 身份验证一起使用,而不是基于表的身份验证。

计划是要有框架:

  1. 检测网站在 Windows 安全上下文中并绕过 登录页面。
  2. 然后关联 Windows 身份/角色并使用它们来映射 数据库中定义的角色/权限。

我在文档中没有看到任何关于这种 Windows 集成方法的内容。

如果有人以前这样做过,我很感激任何提示。

我认为我最好的选择是使用基于策略的授权。因此,在控制器当前使用 ABP 身份验证属性的情况下,我将恢复为正常的 ASP.NET 属性。

例如[Authorize(Policy = "MyAppAdmin")]

【问题讨论】:

    标签: asp.net-core windows-authentication aspnetboilerplate


    【解决方案1】:

    要通过官方 AspNet Boilerplate API 登录用户(拥有角色和其他东西),您可以使用外部身份验证。这正是您正在寻找的;

    https://aspnetboilerplate.com/Pages/Documents/Zero/User-Management#external-authentication

    【讨论】:

    • 在 Windows 集成/经过身份验证的上下文中甚至可以取消登录表单。我不确定这会有多困难......
    【解决方案2】:

    我试着按照约翰的建议去做,但我不得不做一些改变,所以我就是这样做的。

    "angular\src\account\login\login.component.ts"

    class LoginComponent {    
      ngOnInit() {
        this.loginService.authenticateModel.userNameOrEmailAddress = 'foo';
        this.loginService.authenticateModel.password = 'bar';
        this.login();
      }
    }
    

    "aspnet-core\src\ProjectName.Core\Authentication\AlwaysTrue\AlwaysTrueExternalAuthSource.cs"

    public class AlwaysTrueExternalAuthSource: DefaultExternalAuthenticationSource<Tenant, User>, ITransientDependency
    {
      public override string Name => "AlwaysTrueExternalAuthSource";
    
      public override Task<bool> TryAuthenticateAsync(string userNameOrEmailAddress, string plainPassword, Tenant tenant)
      {
        return Task.FromResult(true);
      }
    }
    

    "aspnet-core\src\ProjectName.Core\ProjectNameCoreModule.cs"

    public class ProjectNameCoreModule : AbpModule
    {
      public override void PreInitialize()
      {
        Configuration.Modules.Zero().UserManagement.ExternalAuthenticationSources.Add<AlwaysTrueExternalAuthSource>();
      }
    }
    

    "aspnet-core\src\ProjectName.Web.Core\Controllers\TokenAuthController.cs"

    public class TokenAuthController : ProjectNameControllerBase
    {
      [HttpPost]
      public async Task<AuthenticateResultModel> Authenticate([FromBody] AuthenticateModel model)
      {
        var windowsIdentity = WindowsIdentity.GetCurrent();
        model.UserNameOrEmailAddress = windowsIdentity.Name.ToLowerInvariant().Replace("\\","");
    
        var loginResult = await GetLoginResultAsync(...)
      }
    }
    

    【讨论】:

      【解决方案3】:

      本着分享的精神,我是如何设法规避使用登录屏幕进行 Window Authenticated 上下文的。

      1. 隐藏登录面板并在用户名/密码控件上设置一些虚拟数据(这些虚拟数据并未实际使用)。
      2. 在js文件中立即运行登录动作(无用户交互)

        abp.ajax({
            contentType: 'application/x-www-form-urlencoded',
            url: $loginForm.attr('action'),
            data: $loginForm.serialize()
        });
        
      3. 在 AccountController 中:

        var windowsIdentity = WindowsIdentity.GetCurrent();
        loginModel.UsernameOrEmailAddress = windowsIdentity.Name;
        
        var count = (from x in windowsIdentity.Claims where x.Value == "myclaim" select x).Count();
        
        if (count == 0)
        {
            throw _abpLoginResultTypeHelper.CreateExceptionForFailedLoginAttempt(AbpLoginResultType.InvalidUserNameOrEmailAddress, loginModel.UsernameOrEmailAddress, null);
        }
        
      4. 如上述答案中所述创建一个 ExternalAuthSource。我们将始终返回true,因为真正的身份验证已经完成。
        public override Task<bool> TryAuthenticateAsync(string userNameOrEmailAddress, string plainPassword, Tenant tenant)
        {
            return Task.FromResult(true);
        }
        
        它还有一个额外的优势,即经过身份验证的用户是由 ABP 框架自动创建的。新用户分配的角色取决于 Default 是哪个角色 - 请参阅表 AbpUserRoles

      希望这有助于尝试在 Windows 身份验证上下文中使用该框架的人。

      【讨论】:

      • 感谢您的反馈。从 Intranet 网络外登录怎么样。尝试使用您的手机登录,看看您是否得到相同的结果。我猜你无法检索 WindowsIdentity.GetCurrent()
      • 如果用户退出网络,则他们的 Windows 会话结束,Web 应用程序也随之结束(即用户在 Windows 开始菜单上单击“注销”)。现在我无法使用移动设备登录我们的网络(内网)。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-10-08
      • 2010-12-08
      • 2019-01-31
      • 2018-09-28
      • 1970-01-01
      • 1970-01-01
      • 2019-11-23
      相关资源
      最近更新 更多