【发布时间】:2020-04-20 16:30:19
【问题描述】:
我正在尝试向我的身份服务器添加一个子目录,以便可以将其与 nginx 一起使用。
请注意,这是一个带有 UI 的身份服务器,请参阅 (quickstart ui)
在仔细阅读身份服务器的github 问题后,我设法找到了实际添加子目录的代码。
这是我的配置:
public void Configure(IApplicationBuilder app)
{
if (Environment.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.Map("/auth", app =>
{
app.UseRouting();
app.UseStaticFiles();
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
endpoints.MapRazorPages();
});
app.UseIdentityServer();
});
}
但是,当我导航到 http://xxx:8888/auth/account/login 并尝试登录并接收身份 cookie 时,URL 保持不变,并且我看到一个空白屏幕并且没有 cookie。应该发生的是,我应该被重定向回主页并登录特定用户。
这似乎只有在我添加子目录时才会发生。
请注意,众所周知的端点在使用/auth 获取password 或resource owner 的访问令牌时工作正常。
这是我的配置服务,这里有什么遗漏吗?:
public void ConfigureServices(IServiceCollection services)
{
string connectionString = Configuration.GetConnectionString("AzureConnection");
var migrationsAssembly = typeof(Startup).GetTypeInfo().Assembly.GetName().Name;
services.AddCors(options =>
{
options.AddPolicy("CorsPolicy",
builder => builder.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader());
});
services.AddControllersWithViews().AddRazorRuntimeCompilation();
services.AddRazorPages()
.AddRazorPagesOptions(options =>
{
options.Conventions.AuthorizeAreaFolder("Identity", "/Account/Manage");
});
services.AddDbContext<IdentityDbContext>(options => options.UseSqlServer(connectionString, sql => sql.MigrationsAssembly(migrationsAssembly)));
services.AddDbContext<ConfigurationDbContext>(options => options.UseSqlServer(connectionString, sql => sql.MigrationsAssembly(migrationsAssembly)));
services.AddIdentity<ApplicationUser, IdentityRole>(options =>
{
options.SignIn.RequireConfirmedEmail = true;
})
.AddEntityFrameworkStores<IdentityDbContext>()
.AddDefaultTokenProviders();
services.AddAuthentication()
.AddOpenIdConnect("azuread", "Azure AD", options => Configuration.Bind("AzureAd", options));
services.Configure<OpenIdConnectOptions>("azuread", options =>
{
options.GetClaimsFromUserInfoEndpoint = true;
options.SaveTokens = true;
options.Scope.Add("openid");
options.Scope.Add("profile");
options.Scope.Add("email");
options.Events = new OpenIdConnectEvents()
{
OnRedirectToIdentityProviderForSignOut = context =>
{
context.HandleResponse();
context.Response.Redirect("/Account/Logout");
return Task.FromResult(0);
}
};
});
var builder = services.AddIdentityServer(options =>
{
options.IssuerUri = "http://xxx:8888";
options.PublicOrigin = "http://xxx:8888";
options.Events.RaiseErrorEvents = true;
options.Events.RaiseInformationEvents = true;
options.Events.RaiseFailureEvents = true;
options.Events.RaiseSuccessEvents = true;
options.UserInteraction.LoginUrl = "/Account/Login";
options.UserInteraction.LogoutUrl = "/Account/Logout";
options.Authentication = new IdentityServer4.Configuration.AuthenticationOptions()
{
CookieLifetime = TimeSpan.FromHours(10), // ID server cookie timeout set to 10 hours
CookieSlidingExpiration = true
};
})
.AddConfigurationStore(options =>
{
options.ConfigureDbContext = b => b.UseSqlServer(connectionString, sql => sql.MigrationsAssembly(migrationsAssembly));
})
.AddOperationalStore(options =>
{
options.ConfigureDbContext = b => b.UseSqlServer(connectionString, sql => sql.MigrationsAssembly(migrationsAssembly));
options.EnableTokenCleanup = true;
})
.AddAspNetIdentity<ApplicationUser>();
}
这实际上可以根据请求在具有公共 URL 的 VM 上进行测试。
【问题讨论】:
标签: asp.net-core identityserver4