Cookie expiry in ASP NET Core Authentication using Azure AD OpenIdConnect and custom middleware(使用Azure AD OpenIdConnect和自定义中间件的ASP网络核心身份验证中的Cookie过期)
问题描述
在通过OpenIdConnect身份验证模型使用Azure AD对我的.NET核心应用程序进行身份验证时,我当前正在努力设置Cookie/auth令牌上的超时。
正在ConfigureServices方法中通过以下方式设置登录方案:
services.AddAuthentication(options => options.SignInScheme = CookieAuthenticationDefaults.AuthenticationScheme);
然后我正在设置我的配置,如下所示:
app.UseCookieAuthentication(new CookieAuthenticationOptions()
{
CookieName = "MyCookie",
ExpireTimeSpan = TimeSpan.FromHours(2)
});
app.UseOpenIdConnectAuthentication(new OpenIdConnectOptions()
{
Authority = authorityUri.AbsoluteUri,
ClientId = azureOptions.ClientId,
ClientSecret = azureOptions.ClientSecret,
ResponseType = OpenIdConnectResponseTypes.CodeIdToken,
Events = new OpenIdConnectEvents()
{
OnAuthorizationCodeReceived = async context =>
{
await aAuthenticateMiddleware.OnAuthenticate(context, logger);
}
}
});
app.UseMiddleware<aAuthenticateMiddleware>();
请注意,我使用的不是内置标识(因为它对我们的目的不实用),而是使用自定义中间件。
在中间件层中,我正在检查用户是否经过身份验证,如果没有,则发出质询:
var authenticationProperties = new AuthenticationProperties() { RedirectUri = context.Request.Path.Value ?? "/" };
authenticationProperties.AllowRefresh = false;
authenticationProperties.IssuedUtc = DateTime.Now;
authenticationProperties.ExpiresUtc = DateTime.Now.AddHours(2);
await context.Authentication.ChallengeAsync(
authenticationManager.IdentityProvider.AuthenticationScheme,
authenticationProperties,
ChallengeBehavior.Automatic
);
这一切正常,并正确地对用户进行身份验证等。但是,这是在颁发15分钟过期的身份验证令牌(和Cookie),并忽略我已尝试的2小时过期设置。
我一直在参考来自GitHub的最新源代码示例,这些示例来自于ASPnet/安全存储库...但是,所有这些都没有提到任何有关覆盖发出的默认过期时间的内容。
https://github.com/aspnet/Security/tree/dev/samples/OpenIdConnect.AzureAdSample
我找到的大多数示例仍然引用旧的AspNet库,而不是AspNetCore库。
有些文章建议使用SignInAsync并将Persistent设置为True允许遵守ExpireTimeSpan,但是在调用它时会抛出一个"不受支持的异常"。可能不支持通过Azure AD登录Async?
有人对如何实现这一点有什么见解吗?
推荐答案
在UseOpenIdConnectAuthentication
集合UseTokenLifetime = false
这篇关于使用Azure AD OpenIdConnect和自定义中间件的ASP网络核心身份验证中的Cookie过期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:使用Azure AD OpenIdConnect和自定义中间件的ASP网络核心身份验证中的Cookie过期


基础教程推荐
- 从 VS 2017 .NET Core 项目的发布目录中排除文件 2022-01-01
- 首先创建代码,多对多,关联表中的附加字段 2022-01-01
- 全局 ASAX - 获取服务器名称 2022-01-01
- 错误“此流不支持搜索操作"在 C# 中 2022-01-01
- 是否可以在 asp classic 和 asp.net 之间共享会话状态 2022-01-01
- 在 VS2010 中的 Post Build 事件中将 bin 文件复制到物 2022-01-01
- 如何动态获取文本框中datagridview列的总和 2022-01-01
- JSON.NET 中基于属性的类型解析 2022-01-01
- 将事件 TextChanged 分配给表单中的所有文本框 2022-01-01
- 经典 Asp 中的 ResolveUrl/Url.Content 等效项 2022-01-01