通过SwashbuckleSwagger从身份验证传递第二个值

问题描述

我已设置Swashbuckle进行身份验证(并且正在运行):

ConfigureServices

services.AddSwaggerGen(c =>
{
    c.SwaggerDoc("v1",new OpenApiInfo { Title = "WebAPI Authentication Test Service",Version = "v1" });
    
    c.AddSecurityDeFinition("oauth2",new OpenApiSecurityScheme
    {
        Type = SecuritySchemeType.OAuth2,Flows = new OpenApiOAuthFlows
        {
            AuthorizationCode = new OpenApiOAuthFlow
            {
                AuthorizationUrl = new Uri("https://MyIdp/authorize"),Scopes = new Dictionary<string,string>
                {
                    {"offline_access","Sends the ID Token"},{"openid","Generic open Id"},{"profile","profile"}
                },TokenUrl = new Uri("https://MyIdp/token")
            }
        }
    });

    var openApiSecurityRequirement = new OpenApiSecurityRequirement();
    var openApiSecurityScheme = new OpenApiSecurityScheme
    {
        Reference = new OpenApiReference {Type = ReferenceType.SecurityScheme,Id = "oauth2"}
    };
    openApiSecurityRequirement.Add(openApiSecurityScheme,new string[] { });

    c.AddSecurityRequirement(openApiSecurityRequirement);
});

配置

app.UseSwagger(options =>
{
    options.RouteTemplate = "swagger/{documentName}/swagger.json";
    options.PreSerializefilters.Add((swaggerDoc,httpReq) =>
    {
        swaggerDoc.Servers = new List<OpenApiServer>
        {
            new OpenApiServer {Url = $"https://{apiGatewayHost}/TestService/v2"},new OpenApiServer {Url = $"{httpReq.Scheme}://{httpReq.Host.Value}"}
        };            
    });
});
app.UseSwaggerUI(options =>
{
    options.SwaggerEndpoint("/swagger/v1/swagger.json","WebAPI Authentication Test Service");
    options.RoutePrefix = string.Empty;
    options.OAuthAppName("Swagger Authentication");
    options.OAuthClientId("<Client Id Here>");
    options.OAuthClientSecret("<Client Secret Here>");
    options.OAuthUsePkce();
});

执行此操作时,它会将访问令牌作为承载令牌添加到所有请求中(就像我希望的那样)。

但是我还想添加从身份验证返回的ID令牌,作为称为UserJwt的HttpHeader。

我认为我可以设法将ID令牌而不是访问令牌放入。 但我俩都需要。

我还可以设法将所有旧标头添加到我的招摇调用中(使用操作过滤器。)

但是如何在操作过滤器中访问ID令牌(从身份验证调用获取),以便我可以将ID令牌作为标头包含在内?

更新:
看来OperationFilter在身份验证发生之前就已运行。因此,这不是添加标头的可行方法

不确定是否还有其他过滤器或其他挂钩可以使用。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)