【发布时间】:2018-12-03 16:33:32
【问题描述】:
我正在使用 Finbuckle.Multitenant 和 IdentityServer4 开发多租户 ASP.NET MVC 应用程序(使用他们教程中的标准类和控制器)。我的应用程序使用租户的路由策略 (https://host/tenant1/controller/action),并且我为每个租户使用单独的 cookie(名为 auth.tenant1、auth.tenant2...等的 cookie)除非我为 auth cookie 指定自定义路径,否则一切正常。如果它们都具有 Path=/ 一切正常。但是,当我将 Path=/tenant1 设置为名为 auth.tenant1 的 cookie 并为所有其他租户设置相同的模式时,我在通过同意屏幕后会有一个循环重定向。当我在同意屏幕上单击“是”时,我从客户端的 IdentityServer 中间件收到了 302 质询重定向。它会将我重定向回同意屏幕。在每个“是”之后,我都会返回同意。但是,它仅在身份验证过程中发生。如果我打开新标签并前往https://host/tenant1,我将不会被重定向并且会成功通过身份验证。 谷歌搜索了几天的答案,但没有找到任何解决方案。请帮帮我!
这是我客户的 Startup.cs:
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Clear();
services.AddMultiTenant().WithInMemoryStore(Configuration.GetSection("MultiTenant:InMemoryStore"))
.WithRouteStrategy(MapRoutes)
.WithRemoteAuthentication()
.WithPerTenantOptions<AuthenticationOptions>((options, tenantContext) =>
{
// Allow each tenant to have a different default challenge scheme.
if (tenantContext.Items.TryGetValue("ChallengeScheme", out object challengeScheme))
{
options.DefaultChallengeScheme = (string)challengeScheme;
}
})
.WithPerTenantOptions<CookieAuthenticationOptions>((options, tenantContext) =>
{
options.Cookie.Name += tenantContext.Identifier;
options.Cookie.Path = "/" + tenantContext.Identifier;
options.LoginPath = "/" + tenantContext.Identifier + "/Home/Login";
});
services.AddAuthentication(options =>
{
options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = "oidc";
})
.AddCookie(CookieAuthenticationDefaults.AuthenticationScheme, o =>
{
o.Cookie.Name = "auth.";
})
.AddOpenIdConnect("oidc", options =>
{
options.SignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.Authority = "https://localhost:5000";
options.RequireHttpsMetadata = true;
options.ClientId = "mvc";
options.SaveTokens = true;
options.ClientSecret = "secret";
//Hybrid protocols (OpenId + OAuth)
options.ResponseType = OpenIdConnectResponseType.CodeIdToken;
options.GetClaimsFromUserInfoEndpoint = true;
//ask to allow access to testApi
options.Scope.Add("testApi");
//allows requesting refresh tokens for long lived API access
options.Scope.Add("offline_access");
options.Events = new OpenIdConnectEvents()
{
OnRedirectToIdentityProvider = ctx =>
{
var tenant = ctx.HttpContext.GetMultiTenantContext()?.TenantInfo?.Identifier;
ctx.ProtocolMessage.AcrValues = $"tenant:{tenant}";
return Task.FromResult(0);
}
};
});
}
private void MapRoutes(IRouteBuilder router)
{
router.MapRoute("Default", "{__tenant__=tenant1}/{controller=Home}/{action=Index}/{id?}");
}
这是我在 IdentityServer 端的客户端配置 (appsettings.json):
{
"clientId": "mvc",
"clientName": "MVC Client",
"allowedGrantTypes": [ "hybrid", "client_credentials" ],
"clientSecrets": [
{ "value": "K7gNU3sdo+OL0wNhqoVWhr3g6s1xYv72ol/pe/Unols=" } // Sha256("secret")
],
"redirectUris": [ "https://localhost:5002/signin-oidc" ],
"postLogoutRedirectUris": [ "https://localhost:5002/signout-callback-oidc" ],
"allowedScopes": [ "openid", "profile", "testApi" ],
"allowOfflineAccess": true
},
我的 IdentityServer4 配置:
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
var identityServerBuilder = services.AddIdentityServer()
.AddDeveloperSigningCredential();
if (_config.GetSection("AppSettings:UseDummyAuthentication").Get<bool>())
{
identityServerBuilder
.AddInMemoryIdentityResources(_config.GetSection("IdentityResources"))
.AddInMemoryApiResources(_config.GetSection("ApiResources"))
.AddInMemoryClients(_config.GetSection("Clients"))
.AddTestUsers(_config.GetSection("TestUsers"));
}
services.AddAuthentication();
}
请帮帮我,伙计们!如何使 Path=/tenant1 方案在同意屏幕上没有重定向的情况下工作???
【问题讨论】:
标签: asp.net .net authentication identityserver4 openid-connect