恢复cookie登录中的自动登录功能

Former-commit-id: 80f888219c1b0b920ab923fd1d0ac245ce61e652
TangShanKaiPing
wanggang 6 years ago
parent 9e8c161029
commit 619f2cd5cb

@ -22,11 +22,19 @@ namespace Infrastructure.Extensions
httpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, claimsPrincipal, new AuthenticationProperties { IsPersistent = rememberMe }); httpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, claimsPrincipal, new AuthenticationProperties { IsPersistent = rememberMe });
} }
public static void SignIn(this HttpContext httpContext, string userName, bool rememberMe, IConfiguration cfg, DateTime expires) public static void SignIn(this HttpContext httpContext, string userName, bool rememberMe, IConfiguration cfg)
{ {
var token = httpContext.GetToken(userName, rememberMe, cfg, expires); var token = httpContext.GetToken(userName, rememberMe, cfg, DateTime.Now.AddYears(1));
var cookieOptions = new CookieOptions
{
HttpOnly = true
};
if (rememberMe)
{
cookieOptions.Expires = DateTimeOffset.Now.AddYears(1);
}
httpContext.Response.Cookies.Delete("jwt"); httpContext.Response.Cookies.Delete("jwt");
httpContext.Response.Cookies.Append("jwt", token); httpContext.Response.Cookies.Append("jwt", token, cookieOptions);
} }
public static string GetToken(this HttpContext httpContext, string userName, bool rememberMe, IConfiguration cfg, DateTime expires) public static string GetToken(this HttpContext httpContext, string userName, bool rememberMe, IConfiguration cfg, DateTime expires)

@ -205,7 +205,7 @@ namespace UserCenter.Controllers
} }
else else
{ {
HttpContext.SignIn(model.UserName, model.RememberMe, _cfg, DateTime.Now.AddDays(1)); HttpContext.SignIn(model.UserName, model.RememberMe, _cfg);
if (string.IsNullOrEmpty(returnUrl)) if (string.IsNullOrEmpty(returnUrl))
{ {
returnUrl = Url.Action("Index", "Home"); returnUrl = Url.Action("Index", "Home");

Loading…
Cancel
Save