Created
October 24, 2017 07:02
-
-
Save ozgurrgul/166d0c8c3b725b68a6d6d44d3a0218f1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.ComponentModel.DataAnnotations; | |
using System.IdentityModel.Tokens.Jwt; | |
using System.Linq; | |
using System.Security.Claims; | |
using System.Text; | |
using System.Threading.Tasks; | |
using Microsoft.AspNetCore.Identity; | |
using Microsoft.AspNetCore.Mvc; | |
using Microsoft.Extensions.Configuration; | |
using Microsoft.IdentityModel.Tokens; | |
namespace WebApiJwt.Controllers | |
{ | |
[Route("[controller]/[action]")] | |
public class AccountController : Controller | |
{ | |
private readonly SignInManager<IdentityUser> _signInManager; | |
private readonly UserManager<IdentityUser> _userManager; | |
private readonly IConfiguration _configuration; | |
public AccountController( | |
UserManager<IdentityUser> userManager, | |
SignInManager<IdentityUser> signInManager, | |
IConfiguration configuration | |
) | |
{ | |
_userManager = userManager; | |
_signInManager = signInManager; | |
_configuration = configuration; | |
} | |
[HttpPost] | |
public async Task<object> Login([FromBody] LoginDto model) | |
{ | |
var result = await _signInManager.PasswordSignInAsync(model.Email, model.Password, false, false); | |
if (result.Succeeded) | |
{ | |
var appUser = _userManager.Users.SingleOrDefault(r => r.Email == model.Email); | |
return await GenerateJwtToken(model.Email, appUser); | |
} | |
throw new ApplicationException("INVALID_LOGIN_ATTEMPT"); | |
} | |
[HttpPost] | |
public async Task<object> Register([FromBody] RegisterDto model) | |
{ | |
var user = new IdentityUser | |
{ | |
UserName = model.Email, | |
Email = model.Email | |
}; | |
var result = await _userManager.CreateAsync(user, model.Password); | |
if (result.Succeeded) | |
{ | |
await _signInManager.SignInAsync(user, false); | |
return await GenerateJwtToken(model.Email, user); | |
} | |
throw new ApplicationException("UNKNOWN_ERROR"); | |
} | |
private async Task<object> GenerateJwtToken(string email, IdentityUser user) | |
{ | |
var claims = new List<Claim> | |
{ | |
new Claim(JwtRegisteredClaimNames.Sub, email), | |
new Claim(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString()), | |
new Claim(ClaimTypes.NameIdentifier, user.Id) | |
}; | |
var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_configuration["JwtKey"])); | |
var creds = new SigningCredentials(key, SecurityAlgorithms.HmacSha256); | |
var expires = DateTime.Now.AddDays(Convert.ToDouble(_configuration["JwtExpireDays"])); | |
var token = new JwtSecurityToken( | |
_configuration["JwtIssuer"], | |
_configuration["JwtIssuer"], | |
claims, | |
expires: expires, | |
signingCredentials: creds | |
); | |
return new JwtSecurityTokenHandler().WriteToken(token); | |
} | |
public class LoginDto | |
{ | |
[Required] | |
public string Email { get; set; } | |
[Required] | |
public string Password { get; set; } | |
} | |
public class RegisterDto | |
{ | |
[Required] | |
public string Email { get; set; } | |
[Required] | |
[StringLength(100, ErrorMessage = "PASSWORD_MIN_LENGTH", MinimumLength = 6)] | |
public string Password { get; set; } | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment