代码之家  ›  专栏  ›  技术社区  ›  Mathieu

Identityserver4 with ComponentSpace SAML 2在请求期间获取自定义参数

  •  0
  • Mathieu  · 技术社区  · 8 年前

    对于SAML实现,我使用商业产品componentspacesaml2 For ASP.Net内核。我使用基于中间件的配置。

    我可以控制客户机,因此我还可以传递额外的acr\u值(我认为可以用来传递自定义数据),但是我仍然不知道如何在OnAuthnRequestCreated事件中获取它们,如下面的代码所示。

    services.AddSaml(Configuration.GetSection("SAML"));
    
    services.AddAuthentication()
                .AddWsFederation("adfs", options =>
                {
                    options.SignInScheme = IdentityServerConstants.ExternalCookieAuthenticationScheme;
                    //...rest of config (SSO is working)
                })
                .AddSaml("saml", options =>
                {
                    options.SignInScheme = IdentityServerConstants.ExternalCookieAuthenticationScheme;
                    //...rest of config (SSO is working)
    
                    options.OnAuthnRequestCreated = request =>
                    {                      
                        //Here I would need to know from which client the request is coming (either by client name or url or acr_values or whatever)
                        //to be able to perform conditional logic. I've checked on the request object itself but the info is not in there
    
                        return request;
                    };
                });
    
    2 回复  |  直到 8 年前
        1
  •  1
  •   ComponentSpace    8 年前

    app.Use((context, next) =>
    {
        var samlServiceProvider =
            context.RequestServices.GetRequiredService<ISamlServiceProvider>();
    
        samlServiceProvider.OnAuthnRequestCreated += authnRequest =>
        {
            // Update authn request as required.
    
            return authnRequest;
        };
    
        return next();
    });
    
        2
  •  0
  •   Mathieu    7 年前

    再次感谢你给我指出了正确的方向。

    //build and intermediate service provider so we can get already configured services further down this method
    var sp = services.BuildServiceProvider();
    
    services.AddAuthentication()
                .AddSaml("SamlIdp", options =>
                {
                    options.SignInScheme = IdentityServerConstants.ExternalCookieAuthenticationScheme;
    
                    options.OnAuthnRequestCreated = request =>
                    {
                        var httpContextAccessor = sp.GetService<IHttpContextAccessor>();
                        var queryStringValues = HttpUtility.ParseQueryString(httpContextAccessor.HttpContext.Request.QueryString.Value);
    
                        var interactionService = sp.GetService<IIdentityServerInteractionService>();
                        var authContext = interactionService.GetAuthorizationContextAsync(queryStringValues["ReturnUrl"]).Result;
    
                        //authContext now contains client info and other useful stuff to help build further logic to customize the request
    
                        return request;
                    };
                });
    
    推荐文章