Former-commit-id: a4adcbac5443655ce869307da0a2ed0fb59b09b7
TangShanKaiPing
wanggang 6 years ago
parent 619f2cd5cb
commit dd79d5e6f8

@ -24,7 +24,7 @@ namespace Infrastructure.Extensions
public static void SignIn(this HttpContext httpContext, string userName, bool rememberMe, IConfiguration cfg) public static void SignIn(this HttpContext httpContext, string userName, bool rememberMe, IConfiguration cfg)
{ {
var token = httpContext.GetToken(userName, rememberMe, cfg, DateTime.Now.AddYears(1)); var token = httpContext.GetToken(userName, cfg, DateTime.Now.AddYears(1));
var cookieOptions = new CookieOptions var cookieOptions = new CookieOptions
{ {
HttpOnly = true HttpOnly = true
@ -37,7 +37,7 @@ namespace Infrastructure.Extensions
httpContext.Response.Cookies.Append("jwt", token, cookieOptions); 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, IConfiguration cfg, DateTime expires)
{ {
var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(cfg["jwt:key"])); var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(cfg["jwt:key"]));
var creds = new SigningCredentials(key, SecurityAlgorithms.HmacSha256); var creds = new SigningCredentials(key, SecurityAlgorithms.HmacSha256);

@ -99,13 +99,13 @@ namespace UserCenter.Controllers
{ {
return Ok(new return Ok(new
{ {
AccessToken = Request.HttpContext.GetToken(model.UserName, false, _cfg, DateTime.Now.AddHours(_cfg.GetValue<double>("AccessTokenHours", 0.5))), AccessToken = Request.HttpContext.GetToken(model.UserName, _cfg, DateTime.Now.AddHours(_cfg.GetValue<double>("AccessTokenHours", 0.5))),
RefreshToken = Request.HttpContext.GetToken(model.UserName, false, _cfg, DateTime.Now.AddHours(_cfg.GetValue<double>("AccessTokenHours", 720))), RefreshToken = Request.HttpContext.GetToken(model.UserName, _cfg, DateTime.Now.AddHours(_cfg.GetValue<double>("AccessTokenHours", 720))),
}); });
} }
else else
{ {
return BadRequest(ModelState); return Unauthorized(ModelState);
} }
} }
catch (Exception ex) catch (Exception ex)
@ -124,13 +124,13 @@ namespace UserCenter.Controllers
if (DateTime.UtcNow > token.ValidTo) if (DateTime.UtcNow > token.ValidTo)
{ {
ModelState.AddModelError("", "已过期"); ModelState.AddModelError("", "已过期");
return BadRequest(ModelState); return Unauthorized(ModelState);
} }
var userName = token.Claims.FirstOrDefault(o => o.Type == ClaimTypes.Name).Value; var userName = token.Claims.FirstOrDefault(o => o.Type == ClaimTypes.Name).Value;
return Ok(new return Ok(new
{ {
AccessToken = Request.HttpContext.GetToken(userName, false, _cfg, DateTime.Now.AddHours(_cfg.GetValue<double>("AccessTokenHours", 0.5))), AccessToken = Request.HttpContext.GetToken(userName, _cfg, DateTime.Now.AddHours(_cfg.GetValue<double>("AccessTokenHours", 0.5))),
RefreshToken = Request.HttpContext.GetToken(userName, false, _cfg, DateTime.Now.AddHours(_cfg.GetValue<double>("AccessTokenHours", 720))), RefreshToken = Request.HttpContext.GetToken(userName, _cfg, DateTime.Now.AddHours(_cfg.GetValue<double>("AccessTokenHours", 720))),
}); });
} }
catch (Exception ex) catch (Exception ex)

Loading…
Cancel
Save