asp.net-web-api - ASP.NET - ADFS 身份验证挂钩
问题描述
我有一个针对 ADFS 服务器进行身份验证的 ASP.NET Web API。Authentication 启动类定义如下:
public void ConfigureAuth(IAppBuilder app)
{
app.UseCookieAuthentication(
new CookieAuthenticationOptions
{
AuthenticationType = WsFederationAuthenticationDefaults.AuthenticationType
});
app.UseWsFederationAuthentication(
new WsFederationAuthenticationOptions
{
MetadataAddress = ConfigurationManager.AppSettings["ADFSMetadata"],
Wtrealm = ConfigurationManager.AppSettings["Wtrealm"]
});
app.SetDefaultSignInAsAuthenticationType(WsFederationAuthenticationDefaults.AuthenticationType);
}
我想要的是,当 ADFS 成功验证用户身份并返回令牌时,如果在 ADFS 返回的声明中找到的电子邮件在数据库中不存在,则应在我的 SQL 数据库中创建用户记录。
是否有某种方法可以在身份验证后直接拦截响应以实现上述任务?
解决方案
我找到了解决方案。该类WsFederationAuthenticationOptions
具有Notification
可用于挂钩身份验证成功和失败响应的属性。
例如
public void ConfigureAuth(IAppBuilder app)
{
app.UseCookieAuthentication(
new CookieAuthenticationOptions
{
AuthenticationType = WsFederationAuthenticationDefaults.AuthenticationType
});
app.UseWsFederationAuthentication(
new WsFederationAuthenticationOptions
{
MetadataAddress = ConfigurationManager.AppSettings["ADFSMetadata"],
Wtrealm = ConfigurationManager.AppSettings["Wtrealm"],
Notifications = new WsFederationAuthenticationNotifications
{
AuthenticationFailed = context =>
{
context.HandleResponse();
context.Response.Redirect("/Error?message=" + context.Exception.Message);
return Task.FromResult(0);
},
SecurityTokenReceived = context =>
{
// Get the token
var token = context.ProtocolMessage.GetToken();
return Task.FromResult(0);
}
}
});
app.SetDefaultSignInAsAuthenticationType(WsFederationAuthenticationDefaults.AuthenticationType);
}
推荐阅读
- php - 使用phpSpreadSheet读写excel文件后,图形消失
- java - 带有 React 的 Struts Web 应用程序
- css - 当一个类已经存在时如何实现一个 sr-only 类
- json - 如何解析雪花中的json?
- javascript - 如何在没有未捕获类型错误的情况下访问此嵌套数组?
- reactjs - 使用 mongoDb 将 Next.js 应用程序部署到 Heroku 时出错
- node.js - TypeError: schema[parameter].validateAsync 运行 API 调用时?
- apache - 如何使用 Puppet 的 Apache 模块执行 Location/ProxyPass?
- python-3.x - Python - 比 if else 更好的方法
- javascript - 尽管有条件将当前道具与先前道具进行比较,但在 componentDidUpdate(prevProps) 中的无限 api 调用