【问题标题】:ASP.NET - ADFS authentication hookASP.NET - ADFS 身份验证挂钩
【发布时间】:2019-10-10 17:05:49
【问题描述】:

我有一个针对 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 数据库中创建用户记录已经。

有没有办法在认证后直接拦截响应以实现上述任务?

【问题讨论】:

    标签: asp.net-web-api wif adfs3.0


    【解决方案1】:

    我找到了解决办法。 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);
    
    }
    

    【讨论】:

      猜你喜欢
      • 2016-05-11
      • 1970-01-01
      • 1970-01-01
      • 2012-11-08
      • 2022-07-06
      • 2015-02-20
      • 1970-01-01
      • 1970-01-01
      • 2013-12-14
      相关资源
      最近更新 更多