public async Task <ActionResult <string> > Login([FromBody] LoginDTO model) { var appUser = await _userManager.FindByEmailAsync(model.Email); if (appUser == null) { // user is not found, return 403 _logger.LogInformation("User not found."); return(StatusCode(403)); } // do not log user in, just check that the password is ok var result = await _signInManager.CheckPasswordSignInAsync(appUser, model.Password, false); if (result.Succeeded) { // create claims based user var claimsPrincipal = await _signInManager.CreateUserPrincipalAsync(appUser); // get the Json Web Token var jwt = JwtHelper.GenerateJwt( claimsPrincipal.Claims, _configuration["JWT:Key"], _configuration["JWT:Issuer"], int.Parse(_configuration["JWT:ExpireDays"])); _logger.LogInformation("Token generated for user"); return(Ok(new { token = jwt })); } return(StatusCode(403)); }
public async Task <IActionResult> SignIn( [FromBody] SignRequestModel model, [FromServices] WorkshopDataContext context, [FromServices] ILogger <AuthController> logger) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var user = await context.Users.FirstOrDefaultAsync(x => x.Email == model.Email); if (user == null) { logger.LogInformation($"User '{model.Email}' - Email not found"); return(BadRequest(new ProblemDetails { Status = 400, Title = "You're not registered, please create an account" })); } if (PasswordHash.HashPassword(model.Password, user.PasswordSalt) != user.Password) { logger.LogInformation($"User '{user.Id}' - Password mismatch"); return(BadRequest(new ProblemDetails { Status = 400, Title = "Invalid email or password" })); } return(Ok(JwtHelper.GenerateJwt(user))); }
public async Task <ActionResult <string> > Register([FromBody] Register model) { if (ModelState.IsValid) { var appUser = new AppUser { UserName = model.Email, Email = model.Email, FirstName = model.FirstName, LastName = model.LastName }; var result = await _userManager.CreateAsync(appUser, model.Password); if (result.Succeeded) { _logger.LogInformation("New user created."); // create claims based user var claimsPrincipal = await _signInManager.CreateUserPrincipalAsync(appUser); // get the Json Web Token var jwt = JwtHelper.GenerateJwt( claimsPrincipal.Claims, _configuration["JWT:Key"], _configuration["JWT:Issuer"], int.Parse(_configuration["JWT:ExpireDays"])); _logger.LogInformation("Token generated for user"); return(Ok(new { token = jwt })); } return(StatusCode(406)); //406 Not Acceptable } return(StatusCode(400)); //400 Bad Request }
public async Task <ActionResult <string> > Login([FromBody] LoginRequest model) { var appUser = await _userManager.FindByEmailAsync(model.Email); if (appUser == null) { _logger.LogInformation("User not found."); return(StatusCode(403)); } var result = await _signInManager.CheckPasswordSignInAsync(appUser, model.Password, false); if (result.Succeeded) { var claimsPrincipal = await _signInManager.CreateUserPrincipalAsync(appUser); var jwt = JwtHelper.GenerateJwt( claimsPrincipal.Claims, _configuration["JWT:Key"], _configuration["JWT:Issuer"], int.Parse(_configuration["JWT:ExpireDays"])); _logger.LogInformation("Token generated for user"); return(Ok(new { token = jwt })); } return(StatusCode(403)); }
public IActionResult Index() { var token = JwtHelper.GenerateJwt(); var isValid = JwtHelper.ValidateJwt(token); return(Content(token)); }
public async Task <ActionResult <string> > Register([FromBody] RegisterDTO model) { if (ModelState.IsValid) { var appUser = new AppUser { UserName = model.Email, Email = model.Email, FirstName = model.FirstName, LastName = model.LastName }; var result = await _userManager.CreateAsync(appUser, model.Password); if (result.Succeeded) { _logger.LogInformation("New user created."); /* * var code = await _userManager.GenerateEmailConfirmationTokenAsync(appUser); * * var callbackUrl = Url.Page( * "/Account/ConfirmEmail", * pageHandler: null, * values: new { userId = appUser.Id, code = code }, * protocol: Request.Scheme); * * await _emailSender.SendEmailAsync(model.Email, "Confirm your email", * $"Please confirm your account by <a href='{HtmlEncoder.Default.Encode(callbackUrl)}'>clicking here</a>."); */ // create claims based user var claimsPrincipal = await _signInManager.CreateUserPrincipalAsync(appUser); // get the Json Web Token var jwt = JwtHelper.GenerateJwt( claimsPrincipal.Claims, _configuration["JWT:Key"], _configuration["JWT:Issuer"], int.Parse(_configuration["JWT:ExpireDays"])); _logger.LogInformation("Token generated for user"); return(Ok(new { token = jwt })); } return(StatusCode(406)); //406 Not Acceptable } return(StatusCode(400)); //400 Bad Request }
public async Task <ActionResult <string> > Register([FromBody] RegisterDTO model) { if (ModelState.IsValid) { var appUser = new AppUser { UserName = model.Email, Email = model.Email, FirstName = model.FirstName, LastName = model.LastName }; var result = await _userManager.CreateAsync(appUser, model.Password); if (result.Succeeded) { _logger.LogInformation("New user created."); // create claims based user var claimsPrincipal = await _signInManager.CreateUserPrincipalAsync(appUser); var claims = new List <Claim> { new Claim("UserID", appUser.Id.ToString()), new Claim("FirstName", appUser.FirstName), new Claim("LastName", appUser.LastName) }; var appIdentity = new ClaimsIdentity(claims); claimsPrincipal.AddIdentity(appIdentity); // get the Json Web Token var jwt = JwtHelper.GenerateJwt( claimsPrincipal.Claims, Environment.GetEnvironmentVariable("JWT_ENCRYPTION_KEY"), _configuration["JWT:Issuer"], int.Parse(_configuration["JWT:ExpireDays"])); _logger.LogInformation("Token generated for user"); return(Ok(new { token = jwt })); } return(StatusCode(406)); //406 Not Acceptable } return(StatusCode(400)); //400 Bad Request }
public async Task <IActionResult> Post([FromBody] CredentialsViewModel credentials) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var identity = await GetClaimsIdentity(credentials.UserName, credentials.Password); if (identity == null) { //TODO: dodělat errors to model state //return BadRequest(Errors.AddErrorToModelState("login_failure", "Invalid username or password.", ModelState)); } var jwt = await JwtHelper.GenerateJwt(identity, _jwtFactory, credentials.UserName, _jwtOptions, new JsonSerializerSettings { Formatting = Formatting.Indented }); return(new OkObjectResult(jwt)); }
public async Task <ActionResult <string> > Login([FromBody] LoginDTO model) { var appUser = await _userManager.FindByEmailAsync(model.Email); if (appUser == null) { // user is not found, return 403 _logger.LogInformation("User not found."); return(StatusCode(403)); } // do not log user in, just check that the password is ok var result = await _signInManager.CheckPasswordSignInAsync(appUser, model.Password, false); if (result.Succeeded) { // create claims based user var claimsPrincipal = await _signInManager.CreateUserPrincipalAsync(appUser); var claims = new List <Claim> { new Claim("UserID", appUser.Id.ToString()), new Claim("FirstName", appUser.FirstName), new Claim("LastName", appUser.LastName) }; var appIdentity = new ClaimsIdentity(claims); claimsPrincipal.AddIdentity(appIdentity); // get the Json Web Token var jwt = JwtHelper.GenerateJwt( claimsPrincipal.Claims, Environment.GetEnvironmentVariable("JWT_ENCRYPTION_KEY"), _configuration["JWT:Issuer"], int.Parse(_configuration["JWT:ExpireDays"])); _logger.LogInformation("Token generated for user"); return(Ok(new { token = jwt })); } return(StatusCode(403)); }
public async Task <ActionResult <string> > Register([FromBody] RegisterRequest model) { var appUser = new AppUser { UserName = model.Email, Email = model.Email }; var result = await _userManager.CreateAsync(appUser, model.Password); if (result.Succeeded) { _logger.LogInformation("New user created."); var claimsPrincipal = await _signInManager.CreateUserPrincipalAsync(appUser); var jwt = JwtHelper.GenerateJwt( claimsPrincipal.Claims, _configuration["JWT:Key"], _configuration["JWT:Issuer"], int.Parse(_configuration["JWT:ExpireDays"])); _logger.LogInformation("Token generated for user"); return(Ok(new { token = jwt })); } return(StatusCode(406)); }