我正在使用OWIN WsFederation对用户进行身份验证,我想获取一个声明并执行额外检查以查看此唯一用户ID是否位于数据库中以供访问。如果不是,我想将用户重定向到显示消息的视图。我订阅了一个通知" SecurityTokenValidated",在此通知中,我将获取声明并检查用户是否存在。根据我的理解,在创建cookie之后调用SecurityTokenValidated,所以这可能为时已晚。
如何将用户重定向到一个视图,让他们知道他们无法访问?
app.UseWsFederationAuthentication(
new WsFederationAuthenticationOptions
{
Wtrealm = AppSettings.IdpRealm,
MetadataAddress = AppSettings.IdpMetadata,
Notifications = new WsFederationAuthenticationNotifications
{
// check and create additional claims
SecurityTokenValidated = notification =>
{
// identity object to access claims from IDP
var identity = notification.AuthenticationTicket.Identity;
// get claim and check database
return Task.FromResult<object>(null);
}
}
});
答案 0 :(得分:1)
您可以抛出异常来阻止身份验证流程。像这样的东西
throw new System.IdentityModel.Tokens.SecurityTokenValidationException();
在异常处理程序中,向用户添加友好消息。