public IActionResult Post([FromBody] LoginCreateDto loginCreateDto) { if (loginCreateDto == null) { return(BadRequest()); } var login = Mapper.Map <Login>(loginCreateDto); var user = Mapper.Map <User>(loginCreateDto); //if (!_loginRepository.CompanyExists(login.Company)) //{ // user.Department = Default_Department; // user.Position = Default_Position; //} _loginRepository.AddLogin(login); _unitOfWorkCompany.ChangeDatabase(login.Company); _userRepository.AddUser(user); var userinfo = Mapper.Map <UserInfoDto>(loginCreateDto); if (_unitOfWorkEntrance.Save() & _unitOfWorkCompany.Save()) { return(CreatedAtRoute("GetUser", new { username = user.UserName }, userinfo)); } else { return(StatusCode(500, "将用户信息存入数据库时出错")); } }
public static string GenerateToken(LoginCreateDto login) { var jti = login.UserName + DateTimeOffset.Now.ToUnixTimeSeconds().ToString(); var claims = new Claim[] { // new Claim(ClaimTypes.Name, username), new Claim(JwtRegisteredClaimNames.Sub, login.UserName), new Claim(ClaimTypes.Role, login.Type), new Claim(JwtRegisteredClaimNames.Iss, "Core Web Api"), // new Claim(JwtRegisteredClaimNames.Aud, "Web Application"), // new Claim(JwtRegisteredClaimNames.Iat, new DateTimeOffset(DateTime.Now).ToUnixTimeSeconds().ToString()), new Claim(JwtRegisteredClaimNames.Nbf, DateTimeOffset.Now.ToUnixTimeSeconds().ToString()), // new Claim(JwtRegisteredClaimNames.Exp, new DateTimeOffset(DateTime.Now.AddDays(1)).ToUnixTimeSeconds().ToString()), new Claim(JwtRegisteredClaimNames.Exp, DateTimeOffset.Now.AddMinutes(1).ToUnixTimeSeconds().ToString()), new Claim(JwtRegisteredClaimNames.Jti, jti), new Claim("company", login.Company), new Claim("department", login.Department), new Claim("position", login.Position) }; var token = new JwtSecurityToken( new JwtHeader(new SigningCredentials( new SymmetricSecurityKey(Encoding.UTF8.GetBytes("the secret that needs to be at least 16 characeters long for HmacSha256")), SecurityAlgorithms.HmacSha256)), new JwtPayload(claims)); return(new JwtSecurityTokenHandler().WriteToken(token)); }
public async Task Invoke(HttpContext context, IUnitOfWork <CompanyContext> unitOfWork, ILoginRepository loginRepository, IUserRepository userRepository) { string auth = "Authorization"; string logincookie = "Login"; if (context.Request.Headers.ContainsKey(auth)) { var str = context.Request.Headers[auth]; var token = context.Request.Headers[auth].ToString().Replace("Bearer ", ""); if (Authenticate(token) == EXPIRED) { _logger.LogDebug("Token is Expired."); string cookie = context.Request.Cookies[logincookie]; if (cookie != null) { LoginDto loginDto = JsonConvert.DeserializeObject <LoginDto>(cookie); loginDto.UserName = System.Web.HttpUtility.UrlDecode(loginDto.UserName); loginDto.Password = System.Web.HttpUtility.UrlDecode(loginDto.Password); Login login = Mapper.Map <Login>(loginDto); if (loginRepository.VerifyLogin(login)) { Login logininfo = loginRepository.GetLoginInfo(login.UserName); unitOfWork.ChangeDatabase(logininfo.Company); User userinfo = userRepository.GetUserInfo(loginDto.UserName); LoginCreateDto tokeninfo = Mapper.Map <Login, LoginCreateDto>(logininfo); Mapper.Map(userinfo, tokeninfo); string newtoken = TokenOperator.GenerateToken(tokeninfo); context.Request.Headers[auth] = "Bearer " + newtoken; //context.Response.Headers.Add("NewToken", newtoken); context.Response.OnStarting(() => { context.Response.Headers["NewToken"] = newtoken; return(Task.CompletedTask); }); _logger.LogDebug("Token is Reissued for " + loginDto.UserName); } } } } await _next.Invoke(context); }
public async Task <IActionResult> Login(LoginCreateDto user) { try { var result = await _manager.UserServices.Login(user.UserName, user.Password); return(Ok(result)); } catch (BadHttpRequestException) { return(BadRequest()); } catch (Exception ex) { return(StatusCode(StatusCodes.Status500InternalServerError, new { Status = "Error", Message = ex.Message })); } }