public async Task InvokeAsync(HttpContext context) { Console.WriteLine("called"); try { var token = context.Request.Headers["Authorization"].FirstOrDefault()?.Split(" ").Last(); var key = Encoding.UTF8.GetBytes(_configuration.GetSection("Auth:Secret").Value); var tokenHandler = new JwtSecurityTokenHandler(); tokenHandler.ValidateToken(token, new TokenValidationParameters { ValidateIssuerSigningKey = true, IssuerSigningKey = new SymmetricSecurityKey(key), ValidateIssuer = false, ValidateAudience = false, ClockSkew = TimeSpan.Zero }, out var validatedToken); var jwtToken = validatedToken as JwtSecurityToken; var claim = jwtToken.Claims.FirstOrDefault(x => x.Type == "id"); if (claim != null) { int.TryParse(claim.Value.ToString(), out var id); context.Items["User"] = _dataService.GetUser(id); } } catch { } await _next(context); }
public IActionResult Register(User user) { if (_dataService.GetUser(user.UserName) != null) { return(BadRequest()); } int.TryParse(_configuration.GetSection("Auth:PasswordSize").Value, out int pwdSize); if (pwdSize == 0) { throw new ArgumentException("No password size"); } var salt = PasswordService.GenerateSalt(pwdSize); var pwd = PasswordService.HashPassword(user.Password, salt, pwdSize); _dataService.CreateUser(user.UserName, user.Name, user.Email, pwd, salt); return(CreatedAtRoute(null, new { user.UserName })); }
public IActionResult GetUser(string userName) { var user = (User)ds.GetUser(userName); Console.WriteLine(user); if (user == null) { return(NotFound()); } var dto = _mapper.Map <UserDto>(user); return(Ok(dto)); }