bbr*_*nck 7 c# openid-connect asp.net-core
因此,我的 .NET Core Web 应用程序正在通过 Azure AD 登录用户,并且我有一个包含用户及其角色的数据库。
我已经创建了 OIDC MiddleWare 来从我的数据库中为尝试登录的用户添加声明。
所以流程是:
所以我的问题是:当用户在数据库中不可用或被阻止时,有没有办法拒绝用户的身份验证?
我现在所做的是设置一个声明,如果该声明不可用,它将被重定向到拒绝访问页面(通过使用授权策略),但我希望用户将被重定向到从 Microsoft/AD 的登录页面(最好的情况是带有一条消息)。
这是否可能以某种方式实现?如果可以,如何实现?
这是我现在的代码:
services.AddAuthentication(AzureADDefaults.AuthenticationScheme)
.AddAzureAD(options => { Configuration.Bind("AzureAd", options); }
);
services.AddControllersWithViews(options =>
{
var policy = new AuthorizationPolicyBuilder()
.RequireAuthenticatedUser()
.RequireAssertion(context =>
{
Claim claim = context.User.Claims.First(claim => claim.Type == ClaimTypes.Expired);
return context.User.HasClaim(x => x.Type == ClaimTypes.Expired) &&
context.User.Claims.First(claim => claim.Type == ClaimTypes.Expired).Value
.Equals("false", StringComparison.OrdinalIgnoreCase);
})
.RequireClaim(ClaimTypes.Name)
.RequireClaim(ClaimTypes.Role)
.Build();
options.Filters.Add(new AuthorizeFilter(policy));
});
// OIDC Middleware, to access the User's Claims while logging in through AzureAD
services.Configure<OpenIdConnectOptions>(AzureADDefaults.OpenIdScheme, options =>
{
options.Events = new OpenIdConnectEvents
{
OnRemoteFailure = ctx =>
{
ctx.Response.Redirect("/");
ctx.HandleResponse();
return Task.CompletedTask;
},
OnSignedOutCallbackRedirect = ctx =>
{
ctx.Response.Redirect("/");
ctx.HandleResponse();
return Task.CompletedTask;
},
OnTokenValidated = ctx =>
{
// Get the user's email
var email = ctx.Principal.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Name)?.Value;
// Query the database to get the role
using (var db = ctx.HttpContext.RequestServices.GetRequiredService<TracingContext>())
{
// Get the Users from the database, with the logged in email address (from Azure)
User user = db.Users.FirstOrDefault(u => u.UPN.Equals(email));
if (user != null)
{
user.LastLogin = DateTime.Now;
db.SaveChanges();
// Add claims
var claims = new List<Claim>
{
new Claim(ClaimTypes.Role, user.Role.ToString()),
new Claim(ClaimTypes.Expired, (!user.IsActivated || user.IsBlocked).ToString())
};
// Save the claim
var appIdentity = new ClaimsIdentity(claims);
ctx.Principal.AddIdentity(appIdentity);
}
else
{
**// Send back to Login Page (with error message, maybe?)**
}
}
return Task.CompletedTask;
},
};
});
Run Code Online (Sandbox Code Playgroud)
您可以覆盖默认处理并自行处理响应:
// Send back to Login Page (with error message, maybe?)
ctx.HandleResponse();
ctx.Response.Redirect("/path/to/login");
Run Code Online (Sandbox Code Playgroud)
对 的调用HandleResponse表明我们要自己处理响应,以下调用将设置重定向。有几种发送错误消息的方法。一种方法是向登录 URL 提供查询字符串参数。
| 归档时间: |
|
| 查看次数: |
1532 次 |
| 最近记录: |