Identity Server 4摇摇欲坠的OAuth身份验证重定向到摇摇欲坠的UI不起作用asp.net核心2.2

时间:2019-01-09 05:34:13

标签: c# asp.net asp.net-web-api asp.net-core swagger

试图在asp.net core 2.2中实现大胆的oAuth身份验证。能够登录到身份服务器,但不能重定向到大摇大摆的用户界面。

身份服务器配置

public IEnumerable<ApiResource> GetApiResources()
        {
            return new List<ApiResource>
            {
                new ApiResource( ConstantValue.ClientDashApi, ConstantValue.ClientApi)
            };
        }


        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 =           {
                        $"{Configuration["IdentityServerUrls:ClientUrl"]}/assets/oidc-login-redirect.html",
                        $"{Configuration["IdentityServerUrls:ClientUrl"]}/assets/silent-redirect.html"
                    },
                    PostLogoutRedirectUris = {$"{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 = ConstantValue.SwaggerClientId,
                    ClientName = ConstantValue.SwaggerClientName,
                    AllowedGrantTypes = GrantTypes.Implicit,
                    AllowAccessTokensViaBrowser = true,

                    RedirectUris =
                    {
                        $"{Configuration["IdentityServerUrls:ClientApiUrl"]}/swagger/oauth2-redirect.html",
                        $"{Configuration["IdentityServerUrls:ClientApiUrl"]}/swagger/o2c.html"
                    },
                    AllowedCorsOrigins =     { Configuration["IdentityServerUrls: ClientApiUrl"] },
                    PostLogoutRedirectUris = { $"{Configuration["IdentityServerUrls:ClientApiUrl"]}/swagger/"},
                    AllowedScopes = {ConstantValue.ClientDashApi},
                },
            };
            return client;
        }

        public IEnumerable<IdentityResource> GetIdentityResources()
        {
            return new List<IdentityResource>
            {
                new IdentityResources.OpenId(),
                new IdentityResources.Profile()
            };
        }

客户代码

services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
                .AddJwtBearer(options =>
                {
                    // base-address of your identityserver
                    options.Authority = Configuration["ConstantUrls:IdentityServerAuthority"];
                    options.RequireHttpsMetadata = false;
                    // name of the API resource
                    options.Audience = "mero-rental-client-api";
                });

services.AddSwaggerGen(options =>
            {
                options.SwaggerDoc("v1", new Info { Title = "Mero Rental Web API", Version = "v1" });
                options.AddSecurityDefinition("oauth2", new OAuth2Scheme
                {
                    Type = "oauth2",
                    Flow = "implicit",
                    AuthorizationUrl = $"{Configuration["ConstantUrls:IdentityServerAuthority"]}connect/authorize",
                    TokenUrl = $"{Configuration["ConstantUrls:IdentityServerAuthority"]}connect/token",
                    Scopes = new Dictionary<string, string>()
                    {
                        { ConstantValue.ClientDashApi, ConstantValue.ClientApi }

                    }
                });
                options.OperationFilter<AuthorizeCheckOperationFilter>(); // Required to use access token
            });



app.UseSwagger();
            // Enable middleware to serve swagger-ui (HTML, JS, CSS, etc.), 
            // specifying the Swagger JSON endpoint.
            app.UseSwaggerUI(options =>
            {
                options.SwaggerEndpoint("/swagger/v1/swagger.json", "Mero Rental API V1");
                options.OAuthClientId(ConstantValue.SwaggerClientId);
                options.OAuthAppName(ConstantValue.SwaggerClientName);
            });

这是控制台日志 console logs browser details debugging code

我能够成功登录到身份服务器。此后,页面不会重定向回大摇大摆的用户界面。错误显示在网页图像中。

这是我用来创建身份验证的参考

https://github.com/scottbrady91/IdentityServer4-Swagger-Integration/blob/master/src/Api.Swashbuckle/Startup.cs

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

0 个答案:

没有答案