mmr*_*mmr 4 c# openid-connect .net-core asp.net-core identityserver4
Hej社区,
我被卡住了,我需要一些建议或指向解决方案的指针。我有一个相当简单的 Identity Server 4 设置:
我想在 10 分钟不活动后自动注销用户。在下面的示例中,我使用了 10 秒来加快测试速度。身份验证、重定向和用户强制注销按预期工作,就像使用下面的代码一样。但是,当用户空闲时间超过设定的 10 s 时,用户仍处于登录状态,不会重定向到 IDS 主机上的登录页面。
MVC 客户端使用 Hybrid Grant 设置为:
客户定义
var mvcClient = new Client
{
ClientId = "account-mvc",
ClientName = "Account MVC",
ClientUri = "https://localhost:5002",
AllowedGrantTypes = GrantTypes.Hybrid,
ClientSecrets = { new Secret("secret".Sha256()) },
EnableLocalLogin = true,
RequireConsent = false,
AllowOfflineAccess = false,
AccessTokenLifetime = 10, // 10 s by intention
IdentityTokenLifetime = 10, // 10 s by intention
RedirectUris = "https://localhost:5002/signin-oidc",
PostLogoutRedirectUris = "https://localhost:5002/signout-callback-oidc",
FrontChannelLogoutUri = "https://localhost:5002/signout-oidc",
AllowedScopes =
{
IdentityServerConstants.StandardScopes.OpenId,
IdentityServerConstants.StandardScopes.Profile,
IdentityServerConstants.StandardScopes.Email,
},
};
Run Code Online (Sandbox Code Playgroud)
身份服务器选项
services.AddIdentityServer(options =>
{
options.Authentication.CheckSessionCookieName = "auth-cookie";
options.Authentication.CookieLifetime = new System.TimeSpan(0, 0, 10);
options.Authentication.CookieSlidingExpiration = false;
options.Csp.Level = IdentityServer4.Models.CspLevel.Two;
options.Events.RaiseErrorEvents = true;
options.Events.RaiseInformationEvents = true;
options.Events.RaiseFailureEvents = true;
options.Events.RaiseSuccessEvents = true;
})
.Add... // Left out for brevity
Run Code Online (Sandbox Code Playgroud)
在 MVC 客户端的启动中,我添加:
MVC 客户端启动
services.AddAuthentication(options =>
{
options.DefaultScheme = "Cookies";
options.DefaultChallengeScheme = "oidc";
})
.AddCookie("Cookies", options =>
{
options.ExpireTimeSpan = TimeSpan.FromSeconds(10);
options.SlidingExpiration = false;
options.Cookie.Name = "mvc-cookie";
})
.AddOpenIdConnect("oidc", options =>
{
options.SignInScheme = "Cookies";
options.Authority = "https://localhost:5001/";
options.ClientId = "account-mvc";
options.ClientSecret = "secret";
options.ResponseType = "code id_token";
options.SaveTokens = true;
options.GetClaimsFromUserInfoEndpoint = true;
options.Scope.Add("openid");
options.Scope.Add("profile");
options.Scope.Add("email");
});
Run Code Online (Sandbox Code Playgroud)
并app.UseAuthentication()在Configure方法中添加。
题
一旦会话超时,如何确保用户在 Identity Server 上注销?任何提示和帮助表示赞赏!
经过更多调试后,我发现 MVC 客户端的 cookie 生存期按预期工作,并具有滑动到期时间。但是,一旦 cookie 过期,就会联系身份服务器 (IDS) 并刷新 cookie,因为会话在 IDS 上仍然处于活动状态/活动状态。
我想出了两个解决方案,我决定暂时使用解决方案 1,看看它是否最适合长期使用。
如果有人对安全性和最佳实践有意见或建议,请发表评论或发布其他解决方案。
Client 属性UserSsoLifetime(在 Identity Server 4 中可用v2.3)可用于设置用户必须重新进行身份验证才能使用客户端的最长时间。因此,对于问题的示例,唯一需要的添加是在Client Definition 中添加UserSsoLifetime = 10,例如,
客户定义
var mvcClient = new Client
{
ClientId = "account-mvc",
ClientName = "Account MVC",
ClientUri = "https://localhost:5002",
AllowedGrantTypes = GrantTypes.Hybrid,
ClientSecrets = { new Secret("secret".Sha256()) },
EnableLocalLogin = true,
RequireConsent = false,
AllowOfflineAccess = false,
UserSsoLifetime = 10, // <- HERE
AccessTokenLifetime = 10, // 10 s by intention
IdentityTokenLifetime = 10, // 10 s by intention
RedirectUris = "https://localhost:5002/signin-oidc",
PostLogoutRedirectUris = "https://localhost:5002/signout-callback-oidc",
FrontChannelLogoutUri = "https://localhost:5002/signout-oidc",
AllowedScopes =
{
IdentityServerConstants.StandardScopes.OpenId,
IdentityServerConstants.StandardScopes.Profile,
IdentityServerConstants.StandardScopes.Email,
},
};
Run Code Online (Sandbox Code Playgroud)
这将强制用户在 10 秒不活动后重新进行身份验证。
这个SO 问题解决了 OIDC 属性的问题,该属性可用于强制用户在会话过期后通过登录提示重新进行身份验证 - 请参阅@Scotty Brady 的回答。
因此,问题中表示的示例应如下所示。请注意,只有 MVC 客户端需要更改,即删除了 Cookie 生存期并添加了 OIDC 选项以强制重新验证并使用来自 IDS 的令牌生存期(每行标有// <- HERE)。这样,就使用了来自 IDS 的 cookie 设置(滑动寿命为 10 秒)。
MVC 客户端启动
services.AddAuthentication(options =>
{
options.DefaultScheme = "Cookies";
options.DefaultChallengeScheme = "oidc";
})
.AddCookie("Cookies") // <- HERE
.AddOpenIdConnect("oidc", options =>
{
options.SignInScheme = "Cookies";
options.Authority = "https://localhost:5001/";
options.ClientId = "account-mvc";
options.ClientSecret = "secret";
options.ResponseType = "code id_token";
options.UseTokenLifetime = true; // <- HERE
options.SaveTokens = true;
options.GetClaimsFromUserInfoEndpoint = true;
options.Events.OnRedirectToIdentityProvider = context => // <- HERE
{ // <- HERE
context.ProtocolMessage.Prompt = "login"; // <- HERE
return Task.CompletedTask; // <- HERE
}; // <- HERE
options.Scope.Add("openid");
options.Scope.Add("profile");
options.Scope.Add("email");
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6063 次 |
| 最近记录: |