public IActionResult RegisterPassword([FromBody] UsernameAndPassword uap) { try { var user = _loginService.SignUpPhone(new UserInfo { Phone = uap.Phone, Password = uap.Password }); HttpContext.SignInAsync(JwtBearerDefaults.AuthenticationScheme, new ClaimsPrincipal()); return(Json(new SigninResult { Id = user.Id, Type = user.Type.ToString(), Name = user.Name, Jwt = new JwtSecurityTokenHandler().WriteToken(new JwtSecurityToken(_header, new JwtPayload( null, null, new[] { new Claim("id", user.Id.ToString()), new Claim("type", user.Type.ToString()), }, null, //token过期时间 DateTime.Now.AddDays(7) ))) })); } catch (PhoneAlreadyExistsException) { return(StatusCode(401, new { msg = "该手机号已被注册" })); } catch (PasswordErrorException) { return(StatusCode(401, new { msg = "用户名或密码错误" })); } catch (UserNotFoundException) { return(StatusCode(404, new { msg = "用户不存在" })); } }
public IActionResult SigninPassword([FromBody] UsernameAndPassword uap) { try { var user = _loginService.SignUpPhone(new User { Phone = uap.Phone, Password = uap.Password }); return(Json(new SigninResult { Exp = DateTime.UtcNow.AddDays(7) .Subtract(new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).Ticks / TimeSpan.TicksPerSecond, Id = user.Id, //Type = user.Type; Name = user.Name, Jwt = new JwtSecurityTokenHandler().WriteToken(new JwtSecurityToken(_header, new JwtPayload( null, null, new[] { new Claim("id", user.Id.ToString()), new Claim("type", ""), }, null, DateTime.Now.AddDays(7) ))) })); } catch (PasswordErrorException) { return(StatusCode(401, new { msg = "用户名或密码错误" })); } catch (UserNotFoundException) { return(StatusCode(404, new { msg = "用户不存在" })); } }
public IActionResult RegisterPassword([FromBody] UsernameAndPassword uap) => Json(new SigninResult());