© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•4y ago•
6 replies
M B V R K

❔ JWT with Identity issue

Hi friends,
I'm working on a simple
ASP.NET Core 7
ASP.NET Core 7
project, with
Identity
Identity
and
EF Core
EF Core
,
JWT
JWT
and
Angular
Angular
.

I have this
Controller
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);
    }
}
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources
Was this page helpful?

Similar Threads

Recent Announcements

Similar Threads

✅ Refresh Token + JWT + Identity
C#CC# / help
2y ago
.Net Identity JWT has weird claim
C#CC# / help
4y ago
✅ jwt audience and issuer
C#CC# / help
3y ago
How can i use ASP.NET CORE Identity with JWT
C#CC# / help
3y ago