C
C#2y ago
M B V R K

❔ JWT with Identity issue

Hi friends, I'm working on a simple ASP.NET Core 7 project, with Identity and EF Core, JWT and Angular. I have this Controller :
[ApiController]
[Route("api/[controller]")]
public class StudentController : ControllerBase
{
readonly AppDbContext _dbContext;
readonly UserManager<IdentityUser> _userManager;
readonly SignInManager<IdentityUser> _signInManager;

public StudentController(AppDbContext context, UserManager<IdentityUser> userManager, SignInManager<IdentityUser> signInManager)
{
_dbContext = context;
_userManager = userManager;
_signInManager = signInManager;
}

[HttpPost]
public async Task<IActionResult> Login([FromBody] LoginModel model)
{
var result = await _signInManager.PasswordSignInAsync(model.UserName, model.Password, false, false);
if (!result.Succeeded)
{
return BadRequest();
}

var user = await _userManager.FindByNameAsync(model.UserName);
var claims = new[]
{
new Claim(JwtRegisteredClaimNames.Sub, user.UserName),
new Claim(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString())
};

var token = new JwtSecurityToken(
issuer: "https://localhost:7183",
audience: "https://localhost:7183",
claims: claims,
expires: DateTime.UtcNow.AddMinutes(30),
signingCredentials: new SigningCredentials(new SymmetricSecurityKey(Encoding.UTF8.GetBytes("superSecretKey@345")), SecurityAlgorithms.HmacSha256)
);

return Ok(new
{
token = new JwtSecurityTokenHandler().WriteToken(token),
expiration = DateTime.Now.AddMinutes(30),
userName = user.UserName
});
}

[Authorize]
[HttpGet("students")]
public IActionResult GetStudents()
{
var students = _dbContext.Students.ToList();
return Ok(students);
}
}
[ApiController]
[Route("api/[controller]")]
public class StudentController : ControllerBase
{
readonly AppDbContext _dbContext;
readonly UserManager<IdentityUser> _userManager;
readonly SignInManager<IdentityUser> _signInManager;

public StudentController(AppDbContext context, UserManager<IdentityUser> userManager, SignInManager<IdentityUser> signInManager)
{
_dbContext = context;
_userManager = userManager;
_signInManager = signInManager;
}

[HttpPost]
public async Task<IActionResult> Login([FromBody] LoginModel model)
{
var result = await _signInManager.PasswordSignInAsync(model.UserName, model.Password, false, false);
if (!result.Succeeded)
{
return BadRequest();
}

var user = await _userManager.FindByNameAsync(model.UserName);
var claims = new[]
{
new Claim(JwtRegisteredClaimNames.Sub, user.UserName),
new Claim(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString())
};

var token = new JwtSecurityToken(
issuer: "https://localhost:7183",
audience: "https://localhost:7183",
claims: claims,
expires: DateTime.UtcNow.AddMinutes(30),
signingCredentials: new SigningCredentials(new SymmetricSecurityKey(Encoding.UTF8.GetBytes("superSecretKey@345")), SecurityAlgorithms.HmacSha256)
);

return Ok(new
{
token = new JwtSecurityTokenHandler().WriteToken(token),
expiration = DateTime.Now.AddMinutes(30),
userName = user.UserName
});
}

[Authorize]
[HttpGet("students")]
public IActionResult GetStudents()
{
var students = _dbContext.Students.ToList();
return Ok(students);
}
}
6 Replies
M B V R K
M B V R KOP2y ago
After logged in a new JWT will be saved on the Local Storage for the client, but When I try to access to the GetStudents Action, I get 401 UnAuthorized error
M B V R K
M B V R KOP2y ago
M B V R K
M B V R KOP2y ago
M B V R K
M B V R KOP2y ago
M B V R K
M B V R KOP2y ago
I hope someone to provide a help, since I'm a noob into JWT
Accord
Accord2y ago
Looks like nothing has happened here. I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server