且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

ABP框架中的集成Windows身份验证

更新时间:2023-12-01 14:21:16

本着共享的精神,我设法绕过了Windows身份验证上下文而避免使用登录屏幕.

in the spirit of sharing here is how i managed to circumvent the use of the login screen for a Window Authenticated context.

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

  1. make the Login panel hidden and set some dummy data on the username/password controls (the dummy data is not actually used).
  2. in the js file run the login action immediately (no user interaction)

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

  • 在AccountController中:

  • In the 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);
    }
    

  • 按照上述答案中的说明创建一个ExternalAuthSource.由于实际身份验证已经完成,因此我们将始终返回true.
    public override Task<bool> TryAuthenticateAsync(string userNameOrEmailAddress, string plainPassword, Tenant tenant)
    {
        return Task.FromResult(true);
    }
    

    它的另一个优点是,通过ABP框架自动创建了经过身份验证的用户.为新用户分配的角色取决于Default的角色-请参见表AbpUserRoles.

  • It has the added advantage that the authenticated user is created by the ABP Framework automatically. The Role the new user is assigned depends on the which role is the Default - see Table AbpUserRoles.

    希望这可以帮助尝试在Windows身份验证的上下文中使用该框架的人.

    Hopefully this helps somebody trying to use the framework in a Windows-Authenticated context.