在ABP框架中集成Windows认证
Integrated Windows Authentication in ABP framework
我正在尝试将 ABP 与 Windows 身份验证一起使用,而不是基于 Table 的身份验证。
计划要有框架:
- 检测到该网站处于 Windows 安全上下文中并绕过
登录页面。
- 然后关联 Windows Identity/Roles 并使用它们来映射
Roles/Permissions 在数据库中定义。
我在文档中没有看到关于这种 Windows 集成方法的任何内容。
如果有人以前这样做过,我很感激任何提示。
我认为我最好的选择是使用基于策略的授权。因此,在控制器当前使用 ABP 身份验证属性的地方,我将恢复为正常的 ASP.NET 属性。
例如[Authorize(Policy = "MyAppAdmin")]
要通过官方 AspNet Boilerplate API 登录用户(拥有角色和其他内容),您可以使用外部身份验证。这正是您要找的东西;
https://aspnetboilerplate.com/Pages/Documents/Zero/User-Management#external-authentication
本着在这里分享的精神,我是如何设法绕过 Window 已验证上下文的登录屏幕的使用。
- 隐藏登录面板并在 username/password 控件上设置一些虚拟数据(实际上并未使用虚拟数据)。
在 js 文件中 运行 立即登录操作(无用户交互)
abp.ajax({
contentType: 'application/x-www-form-urlencoded',
url: $loginForm.attr('action'),
data: $loginForm.serialize()
});
在 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。我们将始终 return
true
因为真正的身份验证已经完成。
public override Task<bool> TryAuthenticateAsync(string userNameOrEmailAddress, string plainPassword, Tenant tenant)
{
return Task.FromResult(true);
}
它具有额外的优势,即经过身份验证的用户是由 ABP 框架自动创建的。分配给新用户的角色取决于哪个角色是 Default
- 请参阅 Table AbpUserRoles
.
希望这对尝试在 Windows-Authenticated 环境中使用该框架的人有所帮助。
我试着按照 John 的建议去做,但我不得不做一些改变,所以我就是这样做的。
"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(...)
}
}
我正在尝试将 ABP 与 Windows 身份验证一起使用,而不是基于 Table 的身份验证。
计划要有框架:
- 检测到该网站处于 Windows 安全上下文中并绕过 登录页面。
- 然后关联 Windows Identity/Roles 并使用它们来映射 Roles/Permissions 在数据库中定义。
我在文档中没有看到关于这种 Windows 集成方法的任何内容。
如果有人以前这样做过,我很感激任何提示。
我认为我最好的选择是使用基于策略的授权。因此,在控制器当前使用 ABP 身份验证属性的地方,我将恢复为正常的 ASP.NET 属性。
例如[Authorize(Policy = "MyAppAdmin")]
要通过官方 AspNet Boilerplate API 登录用户(拥有角色和其他内容),您可以使用外部身份验证。这正是您要找的东西;
https://aspnetboilerplate.com/Pages/Documents/Zero/User-Management#external-authentication
本着在这里分享的精神,我是如何设法绕过 Window 已验证上下文的登录屏幕的使用。
- 隐藏登录面板并在 username/password 控件上设置一些虚拟数据(实际上并未使用虚拟数据)。
在 js 文件中 运行 立即登录操作(无用户交互)
abp.ajax({ contentType: 'application/x-www-form-urlencoded', url: $loginForm.attr('action'), data: $loginForm.serialize() });
在 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。我们将始终 return
true
因为真正的身份验证已经完成。
它具有额外的优势,即经过身份验证的用户是由 ABP 框架自动创建的。分配给新用户的角色取决于哪个角色是public override Task<bool> TryAuthenticateAsync(string userNameOrEmailAddress, string plainPassword, Tenant tenant) { return Task.FromResult(true); }
Default
- 请参阅 TableAbpUserRoles
.
希望这对尝试在 Windows-Authenticated 环境中使用该框架的人有所帮助。
我试着按照 John 的建议去做,但我不得不做一些改变,所以我就是这样做的。
"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(...)
}
}