【发布时间】:2020-12-10 13:25:02
【问题描述】:
我有一个包含 ASP.NET Core 3.1 和 Razor 页面的项目。该应用程序非常简单:一个用于保护静态文件的欢迎页面。当我想将我的小型网站与 Identity Server 4 集成时,我的问题就开始了。
通常,在我的其他 .NET Core 项目中,我在 Startup.cs 中添加了:
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddRazorPages(options => {
options.Conventions.AuthorizePage("/Login");
});
services.Configure<IdentityServerConfiguration>(Configuration.GetSection("IdentityServerConfiguration"));
services.AddDistributedMemoryCache();
services.AddSession(options =>
{
options.Cookie.Name = ".cello.Session";
options.IdleTimeout = TimeSpan.FromHours(12);
});
services.AddAuthentication(options =>
{
options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = "oidc";
})
.AddCookie(options =>
{
options.ExpireTimeSpan = TimeSpan.FromMinutes(30);
options.Cookie.Name = "cello.dashboard";
})
.AddOpenIdConnect("oidc", options =>
{
IdentityServerConfiguration idsrv = Configuration.GetSection("IdentityServerConfiguration").Get<IdentityServerConfiguration>();
options.Authority = idsrv.Url;
options.ClientId = idsrv.ClientId;
options.ClientSecret = idsrv.ClientSecret;
#if 调试 options.RequireHttpsMetadata = false; #别的 options.RequireHttpsMetadata = true; #endif
options.ResponseType = "code";
options.Scope.Clear();
options.Scope.Add("openid");
options.Scope.Add("profile");
options.Scope.Add("email");
options.Scope.Add("roles");
options.Scope.Add("offline_access");
options.ClaimActions.MapJsonKey("role", "role", "role");
options.GetClaimsFromUserInfoEndpoint = true;
options.SaveTokens = true;
options.SignedOutRedirectUri = "/";
options.TokenValidationParameters = new TokenValidationParameters
{
NameClaimType = JwtClaimTypes.Name,
RoleClaimType = JwtClaimTypes.Role,
};
});
}
当我尝试添加 Microsoft.AspNetCore.Authentication.OpenIdConnect 时出现不兼容错误。
我必须使用这个库的 3.1.9 版本,而不是最新版本。
有什么想法吗?
【问题讨论】:
标签: c# asp.net identityserver4