【问题标题】:Implementing Swagger Authorization with Identity Server 4 asp.net core 2.2使用 Identity Server 4 asp.net core 2.2 实现 Swagger 授权
【发布时间】:2019-06-02 18:30:03
【问题描述】:

尝试使用身份服务器 4 实现 Swagger 授权。出现错误但不知道我在哪里做错了。

身份服务器设置

public IEnumerable<Client> GetClients()
    {
        var client = new List<Client>
        {
            new Client
            {
                 ClientId = ConstantValue.ClientId,
                ClientName = ConstantValue.ClientName,
                AllowedGrantTypes = GrantTypes.Implicit,
                AllowAccessTokensViaBrowser = true,
                RequireConsent = false,
                RedirectUris =           { string.Format("{0}/{1}", Configuration["IdentityServerUrls:ClientUrl"], "assets/oidc-login-redirect.html"), string.Format("{0}/{1}", Configuration["IdentityServerUrls:ClientUrl"], "assets/silent-redirect.html") },
                PostLogoutRedirectUris = { string.Format("{0}?{1}", Configuration["IdentityServerUrls:ClientUrl"] , "postLogout=true") },
                AllowedCorsOrigins =     { Configuration["IdentityServerUrls: ClientUrl"] },

                AllowedScopes =
                {
                    IdentityServerConstants.StandardScopes.OpenId,
                    IdentityServerConstants.StandardScopes.Profile,
                    ConstantValue.ClientDashApi
                },
                IdentityTokenLifetime=120,
                AccessTokenLifetime=120
            },
            new Client
            {
                ClientId = "swaggerui",
                ClientName = "Swagger UI",
                AllowedGrantTypes = GrantTypes.Implicit,
                AllowAccessTokensViaBrowser = true,

                RedirectUris =
                {
                    string.Format("{0}/{1}", Configuration["IdentityServerUrls:ClientApiUrl"], "swagger/oauth2-redirect.html"),
                    string.Format("{0}/{1}", Configuration["IdentityServerUrls:ClientApiUrl"], "swagger/o2c.html")
                },
                AllowedCorsOrigins =     { Configuration["IdentityServerUrls: ClientApiUrl"] },

                AllowedScopes = {ConstantValue.ClientDashApi},
            },
        };
        return client;
    }

API 代码

services.AddSwaggerGen(options =>
            {
                options.AddSecurityDefinition("oauth2", new OAuth2Scheme
                {
                    Type = "oauth2",
                    Flow = "implicit",
                    AuthorizationUrl = "http://localhost:44305/connect/authorize",
                    TokenUrl = "http://localhost:44305/connect/token",
                    Scopes = new Dictionary<string, string>()
                    {
                        { "mero-rental-client-api", "mero-rental-client-api" }

                    }
                });
            });


app.UseSwagger();
            app.UseSwaggerUI(options =>
            {
                options.SwaggerEndpoint("/swagger/v1/swagger.json", "Mero Rental API V1");
                options.OAuthClientId("swaggerui");
                options.OAuthAppName("Swagger UI");
            });

这是重定向网址

http://localhost:44305/Account/Login?ReturnUrl=%2Fconnect%2Fauthorize%2Fcallback%3Fresponse_type%3Dtoken%26client_id%3Dswaggerui%26redirect_uri%3Dhttp%253A%252F%252Flocalhost%253A44333%252Fswagger%252Foauth2-redirect.html%26scope%3Dmero-rental-client-api%26state%3DV2VkIEphbiAwOSAyMDE5IDE0OjUzOjMzIEdNVCsxMTAwIChBdXN0cmFsaWFuIEVhc3Rlcm4gRGF5bGlnaHQgVGltZSk%253D

这是我在控制台中遇到的错误。 在浏览器上。

参考

https://www.scottbrady91.com/Identity-Server/ASPNET-Core-Swagger-UI-Authorization-using-IdentityServer4

https://dave.vanherten.ca/2017/03/swagger-identityserver4-part2/

【问题讨论】:

  • 请求颁发令牌以访问 Web api 失败,因为未经授权的客户端。检查 api 项目中的客户端详细信息是否正确。
  • 我现在可以登录了。但是登录后页面不会重定向到swagger api页面

标签: asp.net-mvc asp.net-core oauth-2.0 swagger identityserver4


【解决方案1】:

您使用RedirectUris = { string.Format("{0}/{1}", Configuration["IdentityServerUrls:ClientUrl"], "swagger/o2c.html") }Client 配置RedirectUris,这将生成http://localhost:4200/swagger/o2c.html。但是对于您的请求,它会发送带有http://localhost:44333/swagger/oauth2-redirect.html 的requesturl。

尝试检查您的客户端配置并将其更改为http://localhost:4200/swagger/o2c.html

app.UseOpenIdConnectAuthentication(new OpenIdConnectAuthenticationOptions
    {            
        RedirectUri = "http://localhost:4200/swagger/o2c.html",
    });

【讨论】:

  • 我可以登录了。但是登录后页面不会重定向。请告诉我。
  • @SanJaisy 你之前的问题解决了吗?在我的记忆中,您最初的问题与redirecturl 有关。
  • 我已经解决了无效的 url 问题。现在我可以成功登录身份服务器了。但无法重定向回招摇的 UI。得到如上的错误
  • @SanJaisy 如果它解决了您的问题,请接受答案,或者与我们分享您的解决方案并为您的新问题重新发布新线程。请避免编辑您的原始帖子,这会将建议标记为混淆。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-10-17
  • 1970-01-01
  • 1970-01-01
  • 2021-01-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多