【问题标题】:Identity Server 4 Swagger Authentication身份服务器 4 Swagger 身份验证
【发布时间】:2016-10-02 18:02:48
【问题描述】:

我目前在使用 swagger 授权对身份服务器 4 的 api 调用时遇到问题。
我的招摇依赖是使用 swashbuckle 版本 -beta 身份服务器中的客户端对象 4 看起来像

new Client
{
    ClientId="swagger",
    Enabled = true,
    ClientName="Swagger",
    AllowedGrantTypes = GrantTypes.Implicit,
    ClientSecrets = new List<Secret>
    {
        new Secret("secret".Sha256())
    },
    AllowedScopes = new List<string>
    {
        "apil"
    },
    RedirectUris = new List<string>
    {
        "http://localhost:15138/swagger/ui/popup.html"
    },
    AllowedCorsOrigins = new List<string>
    {
        "http://localhost:15138",
        "http://localhost:15138"
    },
    AllowAccessTokensViaBrowser = true,
    AllowAccessToAllScopes= true
}

客户端对象是身份服务器 4 模型 在身份验证的配置方法中我有这个

app.UseIdentityServer();
JwtSecurityTokenHandler.DefaultInboundClaimTypeMap = new Dictionary<string, string>();
app.UseIdentityServerAuthentication(new IdentityServerAuthenticationOptions
{
    Authority = "http://localhost:15138/",
    ScopeName = "apil",
    RequireHttpsMetadata = false,

});

使用提琴手我的获取请求看起来像这样

获取 /connect/authorize?state=9321480892748389&nonce=5279296493808222&client_id=swagger&redirect_uri=http%3A%2F%2Flocalhost%3A15138%2Fswagger%2Fui%2Fpopup.html&response_type=id_token%20token&scope=apil HTTP/1.1

所有必要的参数都在那里,客户端有相应的客户端ID,但我得到的响应是重定向到错误页面,并显示无效请求的消息。我期待一个登录页面传递凭据或类似的东西来获得授权,我想知道我做错了什么导致这种情况发生。

【问题讨论】:

    标签: c# swagger .net-core identityserver4 swashbuckle


    【解决方案1】:

    我遇到了同样的问题,它与一些不同的事情有关。

    1. Swagger 需要配置安全定义。

    2. IdentityServerAuthentication AutomaticAuthenticate 需要为真。

    3. 需要在 Startup.cs 中配置 Swagger 的客户端 ID 和客户端名称。

    见下文:

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddSwaggerGen(c => {
            c.SwaggerDoc("v1", new Info
            {
                Version = "v1",
                Title = "my api title",
                Description = "my api desc",
                TermsOfService = "None",
                Contact = new Contact { Name = "contact" }
            });
    
            var filePath = Path.Combine(PlatformServices.Default.Application.ApplicationBasePath, "api.xml");
            c.IncludeXmlComments(filePath);
    
            c.AddSecurityDefinition("oauth2", new OAuth2Scheme
            {
                Type = "oauth2",
                Flow = "implicit",
                AuthorizationUrl = "https://url",
                Scopes = new Dictionary<string, string>
                {
                    { "api-name", "my api" }
                }
            });
        });
    }
    
    public void Configure(IApplicationBuilder app, ILoggerFactory    loggerFactory)
    {
        loggerFactory.AddConsole(Configuration.GetSection("Logging"));
        loggerFactory.AddDebug();
    
        app.UseIdentity();
    
        app.UseIdentityServerAuthentication(new IdentityServerAuthenticationOptions
        {
            Authority = "https://url",
            RequireHttpsMetadata = true,
            ApiName = "api-name",
            ApiSecret = "api-secret",
            AllowedScopes = { "api-name", "openid", "email", "profile" },
            ClaimsIssuer = "https://url",
            AutomaticAuthenticate = true,
        });
    
        app.UseStaticFiles();
        app.UseMvc();
    
        // Enable middleware to serve generated Swagger as a JSON endpoint
        app.UseSwagger();
    
        // Enable middleware to serve swagger-ui assets (HTML, JS, CSS etc.)
        app.UseSwaggerUi(c =>
        {
            c.ConfigureOAuth2("swagger-name", "swagger-secret", "swagger-realm", "Swagger");
        });
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-08-25
      • 1970-01-01
      • 1970-01-01
      • 2020-10-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多