C# 如何设置asp.net身份Cookie过期时间
我使用Asp.Net身份控制我的应用程序的授权。现在,我需要这样做:如果用户在30分钟内没有操作,跳转到登录页面,当他登录时没有选中“isPersistent”复选框。 而且,如果他选中了“isPersistent”复选框,则将cookie的过期日期设置为14天。 我尝试通过如下方式更改Startup.Auth.cs来实现此目的:C# 如何设置asp.net身份Cookie过期时间,c#,asp.net,cookies,asp.net-identity,C#,Asp.net,Cookies,Asp.net Identity,我使用Asp.Net身份控制我的应用程序的授权。现在,我需要这样做:如果用户在30分钟内没有操作,跳转到登录页面,当他登录时没有选中“isPersistent”复选框。 而且,如果他选中了“isPersistent”复选框,则将cookie的过期日期设置为14天。 我尝试通过如下方式更改Startup.Auth.cs来实现此目的: public void ConfigureAuth(IAppBuilder app) { app.UseCookieAuthentication(new Co
public void ConfigureAuth(IAppBuilder app)
{
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
LoginPath = new PathString("/Account/Login"),
SlidingExpiration = true,
CookieName = WebHelpers.ConstStrings.AUTHCOOKIESNAME
});
}
private async Task SignInAsync(User user, bool isPersistent)
{
AuthenticationManager.SignOut(DefaultAuthenticationTypes.ExternalCookie);
var identity = await UserManager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie);
if (isPersistent)
{
AuthenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = isPersistent }, identity);
}
else
{
AuthenticationManager.SignIn(new AuthenticationProperties() { ExpiresUtc = new DateTimeOffset(DateTime.UtcNow.AddMinutes(30)) }, identity);
}
}
签名代码如下:
public void ConfigureAuth(IAppBuilder app)
{
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
LoginPath = new PathString("/Account/Login"),
SlidingExpiration = true,
CookieName = WebHelpers.ConstStrings.AUTHCOOKIESNAME
});
}
private async Task SignInAsync(User user, bool isPersistent)
{
AuthenticationManager.SignOut(DefaultAuthenticationTypes.ExternalCookie);
var identity = await UserManager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie);
if (isPersistent)
{
AuthenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = isPersistent }, identity);
}
else
{
AuthenticationManager.SignIn(new AuthenticationProperties() { ExpiresUtc = new DateTimeOffset(DateTime.UtcNow.AddMinutes(30)) }, identity);
}
}
但我发现,当用户没有选择isPersistent复选框时,cookies的过期日期已经是“会话”,而不是当前时间加30分钟
使用代码(如after)时的cookies状态,因此“记住我”复选框无法工作。:(
使用此
public void ConfigureAuth(IAppBuilder app)
{
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
ExpireTimeSpan = TimeSpan.FromHours(1),
});
}
如果
AuthenticationProperties
的IsPersistent
属性设置为false,则cookie过期时间设置为Session
如果选中了复选框“记住我”,则AuthenticationManager.SignIn(新的AuthenticationProperties{IsPersistent=true},userIdentity);
将创建一个cookie,其过期时间等于在Startup.cs
中设置的ExpireTimeSpan
(默认为14天)
如果未选中复选框“记住我”,则必须使用AuthenticationManager.SignIn(新的AuthenticationProperties{IsPersistent=true,ExpiresUtc=DateTimeOffset.UtcNow.AddMinutes(30)},userIdentity);
。再次将IsPersistent
设置为true,但现在我们给了ExpiresUtc一个值,这样它就不会使用fromCookieAuthenticationOptions
fromStartup.cs
public override async Task SignInAsync(ApplicationUser user, bool isPersistent, bool rememberBrowser)
{
var userIdentity = await CreateUserIdentityAsync(user).WithCurrentCulture();
// Clear any partial cookies from external or two factor partial sign ins
AuthenticationManager.SignOut(DefaultAuthenticationTypes.ExternalCookie, DefaultAuthenticationTypes.TwoFactorCookie);
if (rememberBrowser)
{
var rememberBrowserIdentity = AuthenticationManager.CreateTwoFactorRememberBrowserIdentity(ConvertIdToString(user.Id));
AuthenticationManager.SignIn(new AuthenticationProperties { IsPersistent = isPersistent }, userIdentity, rememberBrowserIdentity);
}
else
{
//AuthenticationManager.SignIn(new AuthenticationProperties { IsPersistent = isPersistent }, userIdentity);
if (isPersistent)
{
AuthenticationManager.SignIn(new AuthenticationProperties { IsPersistent = true }, userIdentity);
}
else
{
AuthenticationManager.SignIn(new AuthenticationProperties { IsPersistent = true, ExpiresUtc = DateTimeOffset.UtcNow.AddMinutes(30) }, userIdentity);
}
}
}
我也有同样的问题,这段代码对我有效(在Startup.cs文件中)
services.Configure(选项=>
{
options.Cookies.applicationcokie.ExpireTimeSpan=TimeSpan.FromDays(3650);
});
这将为持久性cookie增加大约10年的时间
NB:如果您想要更短的到期时间,您可以使用
TimeSpan.FromMinutes(1);
1分钟或TimeSpan.FromSeconds(30);
30秒等。为了实现您在ASP.NET Core 3.1中描述的功能,我以以下方式在启动中配置身份验证:
services.ConfigureApplicationCookie(o =>
{
...
o.ExpireTimeSpan = TimeSpan.FromMinutes(30);
o.SlidingExpiration = true;
...
o.Events.OnSigningIn = ctx =>
{
if (ctx.Properties.IsPersistent)
{
var issued = ctx.Properties.IssuedUtc ?? DateTimeOffset.UtcNow;
ctx.Properties.ExpiresUtc = issued.AddDays(14);
}
return Task.FromResult(0);
};
});
使用OnSigningIn
回调,我明确地将到期日期设置为now+14天,如果“isPersistent”选中复选框。谢谢,但我发现如果我设置此属性,“记住我”将无法工作。这对我不起作用。它仍然默认为14天。@NabeelZafar感谢您的帮助,但我在下面发布了对我有效的答案。很好,我使用了您的代码并实现了上述功能,谢谢。此外,我从要升级到3.0.1的2.1.0版本。这是用于哪个版本的ASP.NET标识?我在默认ASP.NET MVC 5模板上使用2.2.2,要登录,我有以下行var result=wait-signmanager.PasswordSignInAsync(model.Email,model.Password,ispersist:false,shoulldlockout:false);
没有过期UTC
或任何其他设置方法。还想知道如何在较新版本的ASP.NET Identity中实现这一点…@phxism:请参阅下面的我的答案。如果将过期时间设置为晚于,则某些浏览器可能会比预期的更早过期Cookie。工作得很好!使用auth事件比覆盖I身份登录操作。