在签署用户时我正在设置IsPersistent,如何读取该值?
var identity = await UserManager.CreateIdentityAsync(appUser, DefaultAuthenticationTypes.ApplicationCookie);
HttpContext.GetOwinContext().Authentication.SignIn(new AuthenticationProperties() { IsPersistent = false }, identity);
Run Code Online (Sandbox Code Playgroud) 我们使用MVC 5.2和ASP.NET Identity框架进行身份验证,使用表单身份验证屏幕(用户和密码组合),并使用cookie保留身份.我在Identity框架的启动方法中配置了将身份验证cookie的到期时间设置为30天,当用户选择"记住我"(IsPersistent = true)时,这种方法很好.当IsPersistent = false(用户选择不选择"记住我")时,默认情况下会创建会话cookie.此会话cookie在Internet Explorer和FireFox中运行良好,当浏览器关闭时,cookie将丢失.在Chrome和Safari(也可能是其他浏览器)中,有一些选项可以确保会话cookie不会丢失,
我想要一个解决方法,以确保会话cookie不会永久保留,但会在一小时内丢弃.我可以通过检查帐户是否在X分钟/小时内没有活动来实现这一点,并且用户从未选择"记住我",然后如果超过该时间跨度,则用户身份被下一个请求"拒绝".
有没有人在他们的ASP.NET身份实现中创建了一个解决方案,以确保在X时间之后会话cookie在后端过期,或者是否有更好的方法来解决这个限制?也许可以使用CookieAuthenticationProvider的自定义实现来执行此操作,或者将过期日期/时间交给用户声明,可以在ValidateIdentity流程中的某处进行检查?
任何想法都会很棒.提前谢谢你,-Igor
编辑 - 更多信息:我的身份验证实现有2种基本"模式"(目前无法想到更好的词).我的实现是一个简单的用户名/密码表单,带有一个"记住我"的复选框.复选框值传递给AuthenticationProperties对象的IsPersistent属性,该属性将传递给AuthenticationManager :: SignIn方法.
用户想要"记住我",这会创建一个长期存在的cookie,使用滑动过期设置30天.这使用户能够在浏览器会话之间保持身份验证,并且由于不活动而没有超时,除了不访问该站点超过30天.我的owin启动方法中的设置反映了cookie的到期时间为30天(CookieAuthenticationOptions.ExpireTimeSpan设置为30天).无论浏览器平台如何,这都有效(据我所知).
用户不想"记住我",预期的行为应该是创建会话cookie,当浏览器关闭时,会话cookie将从浏览器中删除.这应该确保下次启动浏览器时未对用户进行身份验证.当像公共计算机一样使用共享设备时,这一点尤其重要.问题是并非所有浏览器都会删除会话cookie,如果启用了浏览器设置,有些人会故意将它们留下(Chrome就是一个很好的例子).会话cookie没有到期日期/时间,因此CookieAuthenticationOptions.ExpireTimeSpan在此处没有任何影响.这是我在寻找建议的地方,因为我相信我不能成为第一个遇到此问题的人.可能有一种方法可以使这种行为更安全,因为替代方法是什么也不做,并且可能在浏览器上留下会话cookie(永不过期!).
我有一个用于用户身份验证的ASP.NET MVC 5应用程序ASP.NET Identity 2.1.0.
过去一切都运行良好,但现在我发现持久的用户会话不再起作用了.我不知道什么样的变化打破了这一点,但它工作时,我实现了身份(转换从应用程序SimpleMembership),这是我的逻辑我的那一刻:
var result = await SignInManager.PasswordSignInAsync(model.UserName, model.Password,
model.RememberMe, shouldLockout: true);
Run Code Online (Sandbox Code Playgroud)
SignInManager是我的ApplicationSignInManager基础SignInManager<ApplicationUser, int>和model.RememberMe是true.
我的设置:
app.CreatePerOwinContext<ApplicationSignInManager>(ApplicationSignInManager.Create);
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
LoginPath = new PathString("/Account/Login"),
Provider = new CookieAuthenticationProvider
{
OnValidateIdentity = ApplicationCookieIdentityValidator.OnValidateIdentity(
validateInterval: TimeSpan.FromMinutes(0),
regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager))
}
});
app.UseTwoFactorSignInCookie(DefaultAuthenticationTypes.TwoFactorCookie, TimeSpan.FromMinutes(5));
app.UseTwoFactorRememberBrowserCookie(DefaultAuthenticationTypes.TwoFactorRememberBrowserCookie);
app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);
Run Code Online (Sandbox Code Playgroud)
除了持久化用户会话外,一切正常.我检查了我的服务器返回的cookie,并且.AspNet.ApplicationCookie总是返回"当前会话有效"而不是将来的某个日期.因此,当我关闭并重新打开浏览器时,我需要再次登录...
有没有人知道为什么这不起作用了?
PS:我已经覆盖SignInAsync了我,ApplicationSignInManager因为我在那里做了一些自定义逻辑,但我甚至检查了调试器并进行了以下调用:
await base.SignInAsync(user, isPersistent, rememberBrowser);
Run Code Online (Sandbox Code Playgroud)
isPersistent是的 …
c# asp.net-mvc asp.net-mvc-5 asp.net-identity asp.net-identity-2
我使用asp.net identity 2.0来管理用户登录.我正在关注Identity 2.0的示例,并且在整个浏览器关闭后无法使cookie保持不变.这种情况发生在所有浏览器上.
码:
账户管理员
public async Task<ActionResult> Login(LoginViewModel model, string returnUrl)
{
if (!ModelState.IsValid)
{
return View(model);
}
var result = await SignInHelper.PasswordSignIn(model.Email, model.Password, isPersistent: true, shouldLockout: true);
switch (result)
{
case SignInStatus.Success:
return RedirectToLocal(returnUrl);
case SignInStatus.LockedOut:
return View("Lockout");
case SignInStatus.Failure:
default:
ModelState.AddModelError("", "Invalid login attempt.");
return View(model);
}
}
Run Code Online (Sandbox Code Playgroud)
SignInHelper
public async Task<SignInStatus> PasswordSignIn(string userName, string password, bool isPersistent, bool shouldLockout)
{
var user = await UserManager.FindByNameAsync(userName);
if (user == null)
{
return SignInStatus.Failure;
}
if (await …Run Code Online (Sandbox Code Playgroud)