【发布时间】:2020-03-19 10:15:14
【问题描述】:
我正在开发一个使用 OWIN 身份验证系统(Microsoft.Owin v4.1.0 通过 NuGet)的 MVC5 网站。我根据数据库验证用户的登录凭据,然后使用 OWIN IAuthenticationManager 登录。以下是相关代码:
using Microsoft.Owin.Security;
public class AuthManager : IAuthManager
{
private readonly IAuthenticationManager AuthenticationManager;
public AuthManager()
{
this.AuthenticationManager = HttpContext.Current.GetOwinContext().Authentication;
}
public void SignIn(AppUserState appUserState, bool rememberMe)
{
AuthenticationProperties authenticationProperties = new AuthenticationProperties()
{
AllowRefresh = true,
IsPersistent = rememberMe,
ExpiresUtc = DateTime.UtcNow.AddDays(14),
IssuedUtc = DateTime.UtcNow
};
List<Claim> claims = new List<Claim>();
claims.Add(new Claim(ClaimTypes.NameIdentifier, appUserState.EmailAddress));
claims.Add(new Claim(ClaimTypes.Name, appUserState.Name));
claims.Add(new Claim("userState", appUserState.ToString()));
ClaimsIdentity identity = new ClaimsIdentity(claims, DefaultAuthenticationTypes.ApplicationCookie);
this.AuthenticationManager.SignIn(authenticationProperties, identity);
}
}
登录过程有效,但持久性无效。尽管 IsPersistent 被设置为 true,但在大约 30-60 分钟后会话总是结束。当我进入调试器并检查authenticationProperties 对象时,我可以看到IsPersistent 确实是真的。
我在网上读到,这是决定您的应用程序是否应该让您的用户保持登录状态的标志,即使用户没有积极使用该网站。但是用户的会话总是结束。
我认识到的一件事是AuthenticationProperties 类内部有一个字典对象:
当我打开字典时,我可以看到有一个标记为.persistent 的键,它有一个空白值。我尝试将此值设置为True(以复制.refresh 键的行为)-但这根本没有效果。
我认为上述问题与问题无关,但我认为我应该分享我已经进行的调查。
如何防止用户自动注销?
【问题讨论】:
标签: c# asp.net-mvc authentication asp.net-mvc-5 owin