【问题标题】:Add custom data to ADFS authentication将自定义数据添加到 ADFS 身份验证
【发布时间】:2017-05-04 10:18:46
【问题描述】:

我有很多应用程序,我正在将身份验证切换到 ADFS,我需要添加自定义数据,比如说成功登录后数据库中的一组角色。

场景解释: 每个应用程序在 DB 中都有自己的角色, 在发送授权请求后的用户身份验证期间,Application_AuthenticateRequest(object sender, EventArgs e) 将被调用,因此我可以像这样添加角色作为声明

 ((ClaimsIdentity)((ClaimsPrincipal)currentUser).Identity)
                    .AddClaim(new Claim(ClaimTypes.Role, "role1FromDataBase"));
                HttpContext.Current.User = currentUser;

但是Application_AuthenticateRequest() 方法会为每个请求调用,我不想每次都从数据库中请求角色。 所以,我需要在某处添加这些角色,然后才能调用它们。当然,当我处理基于 API 角色的授权时,Sessions 和 Cookies 并不是最佳实践。

应用程序在 Windows server 2012 上具有控制器和 API 以及我的 ADFS

我的 OWIN 启动是

app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);

        app.UseCookieAuthentication(new CookieAuthenticationOptions());
        app.UseWsFederationAuthentication(
            new WsFederationAuthenticationOptions
            {
                Wtrealm = realm,
                MetadataAddress = adfsMetadata,

                Notifications = new WsFederationAuthenticationNotifications()
                {

                    RedirectToIdentityProvider = context =>
                    {   

                        context.ProtocolMessage.Wreply = "https://localhost:44329/";
                        return Task.FromResult(0);
                    }
                },

            });


        app.UseStageMarker(PipelineStage.Authenticate);

我能做什么?

【问题讨论】:

    标签: c# asp.net ws-federation adfs3.0


    【解决方案1】:

    几个小时后我解决了这个问题 在Startup 类和public void Configuration(IAppBuilder app) 方法中 我们必须将带有角色的声明添加到WsFederationAuthenticationOptions 像这样

     app.UseWsFederationAuthentication(new WsFederationAuthenticationOptions
            {
                Wtrealm = realm,
                MetadataAddress = adfsMetadata,
    
                Notifications = new WsFederationAuthenticationNotifications()
                {
                    // this method will be invoked after login succes 
                    SecurityTokenValidated = notification =>
                    {
                        ClaimsIdentity identity = notification.AuthenticationTicket.Identity;
                        // here we can add claims and specify the type, in my case i want to add Role Claim
                        identity.AddClaim(new Claim(ClaimTypes.Role, "student"));
    
                        return Task.FromResult(0);
                    },
                    RedirectToIdentityProvider = context =>
                    {
    
                        context.ProtocolMessage.Wreply = "https://localhost:44329/";
                        return Task.FromResult(0);
                    }
                },
    
            });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-04-02
      • 2020-06-21
      • 1970-01-01
      • 2019-10-23
      • 2016-05-11
      • 2014-06-05
      • 2020-04-01
      相关资源
      最近更新 更多