This file contains hidden or 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
var jwtSettings = new JwtSettings(); | |
configuration.Bind(nameof(jwtSettings), jwtSettings); | |
services.AddSingleton(jwtSettings); | |
var tokenValidationParameters = new TokenValidationParameters | |
{ | |
ValidateIssuerSigningKey = true, | |
IssuerSigningKey = new SymmetricSecurityKey(Encoding.ASCII.GetBytes(jwtSettings.Secret)), | |
ValidateIssuer = false, | |
ValidateAudience = false, |
This file contains hidden or 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
var jwtSettings = new JwtSettings(); | |
configuration.Bind(nameof(jwtSettings), jwtSettings); | |
services.AddSingleton(jwtSettings); | |
var tokenValidationParameters = new TokenValidationParameters | |
{ | |
ValidateIssuerSigningKey = true, | |
IssuerSigningKey = new SymmetricSecurityKey(Encoding.ASCII.GetBytes(jwtSettings.Secret)), | |
ValidateIssuer = false, | |
ValidateAudience = false, |
This file contains hidden or 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
var tokenHandler = new JwtSecurityTokenHandler(); | |
var key = Encoding.ASCII.GetBytes(_jwtSettings.Secret); | |
var claims = new List<Claim> | |
{ | |
new Claim(JwtRegisteredClaimNames.Sub, user.Email), | |
new Claim(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString()), | |
new Claim(JwtRegisteredClaimNames.Email, user.Email), | |
new Claim("id", user.Id) | |
}; |
This file contains hidden or 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 async Task<AuthenticationResult> GenerateAuthenticationResultForUserAsync(IdentityUser user) | |
{ | |
var tokenHandler = new JwtSecurityTokenHandler(); | |
var key = Encoding.ASCII.GetBytes(_jwtSettings.Secret); | |
var claims = new List<Claim> | |
{ | |
new Claim(JwtRegisteredClaimNames.Sub, user.Email), | |
new Claim(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString()), | |
new Claim(JwtRegisteredClaimNames.Email, user.Email), |
This file contains hidden or 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<AuthenticationResult> LoginAsync(string email, string password) | |
{ | |
var user = await _userManager.FindByEmailAsync(email); | |
if (user == null) | |
{ | |
return new AuthenticationResult | |
{ | |
Errors = new[] {"User does not exist"} | |
}; |
This file contains hidden or 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
namespace Tweetbook.Extensions | |
{ | |
public static class GeneralExtensions | |
{ | |
public static string GetUserId(this HttpContext httpContext) | |
{ | |
if (httpContext.User == null) | |
{ | |
return string.Empty; | |
} |
This file contains hidden or 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<bool> UserOwnsPostAsync(Guid postId, string userId) | |
{ | |
var post = await _dataContext.Posts.AsNoTracking().SingleOrDefaultAsync(x => x.Id == postId); | |
if (post == null) | |
{ | |
return false; | |
} | |
if (post.UserId != userId) |
This file contains hidden or 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
var tokenValidationParameters = new TokenValidationParameters | |
{ | |
ValidateIssuerSigningKey = true, | |
IssuerSigningKey = new SymmetricSecurityKey(Encoding.ASCII.GetBytes(jwtSettings.Secret)), | |
ValidateIssuer = false, | |
ValidateAudience = false, | |
RequireExpirationTime = false, | |
ValidateLifetime = true | |
}; |
This file contains hidden or 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
var tokenValidationParameters = new TokenValidationParameters | |
{ | |
ValidateIssuerSigningKey = true, | |
IssuerSigningKey = new SymmetricSecurityKey(Encoding.ASCII.GetBytes(jwtSettings.Secret)), | |
ValidateIssuer = false, | |
ValidateAudience = false, | |
RequireExpirationTime = false, | |
ValidateLifetime = true | |
}; |
This file contains hidden or 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(ApiRoutes.Identity.Refresh)] | |
public async Task<IActionResult> Refresh([FromBody] RefreshTokenRequest request) | |
{ | |
var authResponse = await _identityService.RefreshTokenAsync(request.Token, request.RefreshToken); | |
if (!authResponse.Success) | |
{ | |
return BadRequest(new AuthFailedResponse | |
{ | |
Errors = authResponse.Errors |