Skip to content

Commit

Permalink
Genrate JWT Token
Browse files Browse the repository at this point in the history
  • Loading branch information
YaraGh22-engs committed Jun 10, 2024
1 parent a905879 commit aeebdec
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
36 changes: 34 additions & 2 deletions MoviesApi/Controllers/AccountController.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Microsoft.IdentityModel.Tokens;
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;
using System.Text;

namespace MoviesApi.Controllers
{
Expand All @@ -9,9 +13,11 @@ namespace MoviesApi.Controllers
public class AccountController : ControllerBase
{
private readonly UserManager<AppUser> _userManager;
public AccountController(UserManager<AppUser> userManager)
private readonly IConfiguration configuration;
public AccountController(UserManager<AppUser> userManager, IConfiguration configuration)
{
_userManager = userManager;
this.configuration = configuration;
}
[HttpPost("Register")]
public async Task<IActionResult> RegisterNewUser(dtoNewUser dtouser)
Expand Down Expand Up @@ -50,7 +56,33 @@ public async Task<IActionResult> LogIn(dtoLogin dtolog)
{
if (await _userManager.CheckPasswordAsync(user, dtolog.password))
{
return Ok("Token");
var claims = new List<Claim>();
//claims.Add(new Claim("name", "value"));
claims.Add(new Claim(ClaimTypes.Name, user.UserName));

Check warning on line 61 in MoviesApi/Controllers/AccountController.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference argument for parameter 'value' in 'Claim.Claim(string type, string value)'.
claims.Add(new Claim(ClaimTypes.NameIdentifier, user.Id));
claims.Add(new Claim(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString()));
var roles = await _userManager.GetRolesAsync(user);
foreach (var role in roles)
{
claims.Add(new Claim(ClaimTypes.Role, role.ToString()));
}
//signingCredentials
var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(configuration["JWT:SecretKey"]));

Check warning on line 70 in MoviesApi/Controllers/AccountController.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference argument for parameter 's' in 'byte[] Encoding.GetBytes(string s)'.
var sc = new SigningCredentials(key, SecurityAlgorithms.HmacSha256);
var token = new JwtSecurityToken(
claims: claims,
issuer: configuration["JWT:Issuer"],
audience: configuration["JWT:Audience"],
expires: DateTime.Now.AddHours(1),
signingCredentials: sc
);
var _token = new
{
token = new JwtSecurityTokenHandler().WriteToken(token),
expiration = token.ValidTo,
};
return Ok(_token);

}
else
{
Expand Down
1 change: 1 addition & 0 deletions MoviesApi/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@
app.UseSwaggerUI();
}

app.UseAuthentication();
app.UseHttpsRedirection();


Expand Down

0 comments on commit aeebdec

Please sign in to comment.