public async Task <ActionResult <LoginResponseViewModel> > Post([FromBody] SecondFactorRequestViewModel model) { if (string.IsNullOrWhiteSpace(model.Username) || string.IsNullOrWhiteSpace(model.Password) || string.IsNullOrWhiteSpace(model.SecondFactorValue)) { return(new UnauthorizedResult()); } MemberModel member = await _userManager.FindByNameAsync(model.Username); if (member != null) { Microsoft.AspNetCore.Identity.SignInResult result = await _signInManager.CheckPasswordSignInAsync(member, model.Password, false); if (result.Succeeded) { if (_memberServices.ValidateTwoFactorCodeAsync(member, model.SecondFactorValue)) { IList <string> roles = await _userManager.GetRolesAsync(member); string role = ""; if (roles.Contains("Admin")) { role = "Admin"; } else if (roles.Contains("Member")) { role = "Member"; } JwtSecurityTokenHandler handler = new JwtSecurityTokenHandler(); SymmetricSecurityKey securityKey = new SymmetricSecurityKey(Encoding.ASCII.GetBytes(_configuration["JwtKey"])); SecurityTokenDescriptor tokenDescriptor = new SecurityTokenDescriptor { Subject = new ClaimsIdentity(new Claim[] { new Claim(ClaimTypes.Name, member.Id.ToString()), new Claim(ClaimTypes.Role, role) }), Expires = DateTime.UtcNow.AddDays(7), SigningCredentials = new SigningCredentials(securityKey, SecurityAlgorithms.HmacSha256Signature), }; SecurityToken securityToken = handler.CreateToken(tokenDescriptor); LoginResponseViewModel responseModel = new LoginResponseViewModel(); responseModel.Token = handler.WriteToken(securityToken); return(new OkObjectResult(responseModel)); } } } return(new UnauthorizedResult()); }