Identityserver4,运行和实现的问题

问题描述 投票:-1回答:1

我想用Entity-FrameWork核心创建一个集中的asp.net核心API项目,用于会员管理,如登录,注册等。另外我想创建另一个asp.net核心项目,并使用集中项目为google.com等会员。经过大量的搜索,我明白应该使用IdentityServer4。我阅读了这份文件并从Github那里得到了样本,但目前还不清楚,我很困惑。谁可以一步一步清楚解释?谢谢

asp.net-core entity-framework-core identityserver4
1个回答
0
投票

IdentityServer4具有简单的MeadleWare,可用于Asp.Net Core

public void ConfigureServices(IServiceCollection services){
   ...
   var cert = new X509Certificate2("/Cert/cert.pfx", "123456");

   services.AddIdentityServer()
                .AddInMemoryApiResources(Config.GetApisResources())
                .AddSigningCredential(cert)
                .AddInMemoryClients(Config.GetClients())
                .Services.AddTransient<IResourceOwnerPasswordValidator, ResourceOwnerPasswordValidator>();
   ...
}

public void Configure(ILoggerFactory loggerFactory, IApplicationBuilder app, IHostingEnvironment env){
   ...
   app.UseIdentityServer();
   ...
}

并创建一个配置文件:

public class Config
    {
        public static IEnumerable<ApiResource> GetApisResources()
        {
            return new[]
            {
                // simple API with a single scope (in this case the scope name is the same as the api name)
                new ApiResource("api1"),
            };
        }


        public static IEnumerable<Client> GetClients()
        {
            return new List<Client>
        {
            new Client
            {
                ClientId = "spa",
                AllowedGrantTypes = GrantTypes.ResourceOwnerPasswordAndClientCredentials,
                //IdentityTokenLifetime=10,

                AllowOfflineAccess=true,
                RefreshTokenExpiration = TokenExpiration.Absolute,
                AbsoluteRefreshTokenLifetime = 999999,
                RefreshTokenUsage=TokenUsage.ReUse,
                AccessTokenType=AccessTokenType.Jwt,

                ClientSecrets =
                {
                    new Secret("secret".Sha256())
                },

                AllowedScopes =
                {
                    "api1",
                    IdentityServerConstants.StandardScopes.OfflineAccess
                },
                AccessTokenLifetime=36000
            }
        };
        }
    }

然后在resourceServer中使用Bellow MW:

public void ConfigureServices(IServiceCollection services){
   ...
   services.AddAuthentication(o =>
            {
                o.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
                o.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
            }).AddJwtBearer(o =>
            {
                o.Authority = "http://localhost:5000";
                o.Audience = "self";
                o.RequireHttpsMetadata = false;
                o.TokenValidationParameters = new TokenValidationParameters
                {
                    ValidateIssuerSigningKey = false,
                    ValidateIssuer = false,
                    ValidateAudience = false,
                    ValidateLifetime = true,
                    RequireExpirationTime = true,
                    ClockSkew = TimeSpan.Zero
                };

                o.Events = new JwtBearerEvents()
                {
                    OnAuthenticationFailed = c =>
                    {
                        c.NoResult();
                        c.Response.StatusCode = 401;
                        c.Response.ContentType = "text/plain";
                        return c.Response.WriteAsync(c.Exception.ToString());
                    },
                    OnTokenValidated = context =>
                    {
                        return Task.CompletedTask;
                    },
                    OnMessageReceived = context =>
                    {
                        return Task.CompletedTask;
                    },
                    OnChallenge = context =>
                    {
                        return Task.CompletedTask;
                    }
                };
            });
   ...
}

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
   {
      ...
      app.UseAuthentication();
      ...
   }
© www.soinside.com 2019 - 2024. All rights reserved.