Asp.net core Identityserver4,运行和实现问题
我想创建一个具有实体框架核心的集中asp.net核心API项目,用于成员身份管理,如登录、注册等。另外,我想创建另一个asp.net核心项目,并使用集中项目作为成员身份,如google.com。 经过大量的搜索,我明白应该使用IdentityServer4。我阅读了这篇文档并从Github获得了样本,但不清楚,我感到困惑。 谁能一步一步地解释清楚?Asp.net core Identityserver4,运行和实现问题,asp.net-core,entity-framework-core,identityserver4,Asp.net Core,Entity Framework Core,Identityserver4,我想创建一个具有实体框架核心的集中asp.net核心API项目,用于成员身份管理,如登录、注册等。另外,我想创建另一个asp.net核心项目,并使用集中项目作为成员身份,如google.com。 经过大量的搜索,我明白应该使用IdentityServer4。我阅读了这篇文档并从Github获得了样本,但不清楚,我感到困惑。 谁能一步一步地解释清楚? 谢谢IdentityServer4具有简单的MeadleWare,可在Asp.Net Core中使用 public void ConfigureSe
谢谢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();
...
}
哪份文件?这里:你到底对什么感到困惑?你遇到了什么问题?什么东西没有发挥应有的作用?你有例外吗?如果是,这些是什么?我们需要更多的信息来给你一个答案。我不知道是什么情况。我只需要两个项目,其中一个是登录/注册api,另一个加入它登录或注册。第二个项目的配置是什么?你有我要的完整样品吗?谢谢。该代码应用于登录/注册项目?如何在其他项目和服务的登录/注册项目中使用此identityserver4?
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
}
};
}
}
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();
...
}