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
[HttpPost] | |
public async Task<IActionResult> CreateUserAsync([FromBody] UserCredentialsResource userCredentials) | |
{ | |
if (!ModelState.IsValid) | |
{ | |
return BadRequest(ModelState); | |
} | |
var user = _mapper.Map<UserCredentialsResource, User>(userCredentials); |
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
public class PasswordHasher : IPasswordHasher | |
{ | |
public string HashPassword(string password) | |
{ | |
byte[] salt; | |
byte[] buffer2; | |
if (string.IsNullOrEmpty(password)) | |
{ | |
throw new ArgumentNullException("password"); | |
} |
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
[Route("/api/login")] | |
[HttpPost] | |
public async Task<IActionResult> LoginAsync([FromBody] UserCredentialsResource userCredentials) | |
{ | |
if (!ModelState.IsValid) | |
{ | |
return BadRequest(ModelState); | |
} | |
var response = await _authenticationService.CreateAccessTokenAsync(userCredentials.Email, userCredentials.Password); |
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
public async Task<TokenResponse> CreateAccessTokenAsync(string email, string password) | |
{ | |
var user = await _userService.FindByEmailAsync(email); | |
if (user == null || !_passwordHasher.PasswordMatches(password, user.Password)) | |
{ | |
return new TokenResponse(false, "Invalid credentials.", null); | |
} | |
var token = _tokenHandler.CreateAccessToken(user); |
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
"TokenOptions": { | |
"Audience": "SampleAudience", | |
"Issuer": "JWPAPI", | |
"AccessTokenExpiration": 30, | |
"RefreshTokenExpiration": 60 | |
}, |
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
public AccessToken CreateAccessToken(User user) | |
{ | |
var refreshToken = BuildRefreshToken(user); | |
var accessToken = BuildAccessToken(user, refreshToken); | |
_refreshTokens.Add(refreshToken); | |
return accessToken; | |
} |
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
private RefreshToken BuildRefreshToken(User user) | |
{ | |
var refreshToken = new RefreshToken | |
( | |
token : _passwordHaser.HashPassword(Guid.NewGuid().ToString()), | |
expiration : DateTime.UtcNow.AddSeconds(_tokenOptions.RefreshTokenExpiration).Ticks | |
); | |
return refreshToken; | |
} |
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
private AccessToken BuildAccessToken(User user, RefreshToken refreshToken) | |
{ | |
var accessTokenExpiration = DateTime.UtcNow.AddSeconds(_tokenOptions.AccessTokenExpiration); | |
var securityToken = new JwtSecurityToken | |
( | |
issuer : _tokenOptions.Issuer, | |
audience : _tokenOptions.Audience, | |
claims : GetClaims(user), | |
expires : accessTokenExpiration, |
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
private IEnumerable<Claim> GetClaims(User user) | |
{ | |
var claims = new List<Claim> | |
{ | |
new Claim(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString()), | |
new Claim(JwtRegisteredClaimNames.Sub, user.Email) | |
}; | |
foreach (var userRole in user.UserRoles) | |
{ |
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
public class SigningConfigurations | |
{ | |
public SecurityKey Key { get; } | |
public SigningCredentials SigningCredentials { get; } | |
public SigningConfigurations() | |
{ | |
using(var provider = new RSACryptoServiceProvider(2048)) | |
{ | |
Key = new RsaSecurityKey(provider.ExportParameters(true)); |
OlderNewer