© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•16mo ago•
1 reply
Yasu

Why is my JWT token not getting authorized and not getting entering the [Authorize] method?

[HttpPost]
public IActionResult Login(LogInAndSignUp user)
{
    var logUser = context.LogInAndSignUps.Where(x => x.Email == user.Email && x.Password == user.Password).FirstOrDefault();
    if (logUser != null)
    {
        // Generate JWT token
        var token = GenerateJwtToken(logUser);

        // Set the token in a cookie
        var cookieOptions = new CookieOptions
        {
            HttpOnly = true,  // Prevents JavaScript from accessing the cookie
            Expires = DateTime.Now.AddMinutes(30) // Set expiration time for the token
        };
        Response.Cookies.Append("UserToken", token, cookieOptions);

        // Redirect to the dashboard
        return RedirectToAction("Dashboard");
    }
    else
    {
        ViewBag.Message = "Login Failed";
    }
    return View();
}

[Authorize]
public IActionResult Dashboard()
{
    Console.WriteLine("Dashboard action called.");
    var userEmail = HttpContext.Session.GetString("UserSession");
    //var userEmail = User.FindFirstValue(JwtRegisteredClaimNames.Sub); // Extract email from token
    if (userEmail != null)
    {
        var user = context.LogInAndSignUps.Where(x => x.Email == userEmail).FirstOrDefault();
        if (user != null)
        {
            return View(user);
        }
    }
    return RedirectToAction("Login");
}
[HttpPost]
public IActionResult Login(LogInAndSignUp user)
{
    var logUser = context.LogInAndSignUps.Where(x => x.Email == user.Email && x.Password == user.Password).FirstOrDefault();
    if (logUser != null)
    {
        // Generate JWT token
        var token = GenerateJwtToken(logUser);

        // Set the token in a cookie
        var cookieOptions = new CookieOptions
        {
            HttpOnly = true,  // Prevents JavaScript from accessing the cookie
            Expires = DateTime.Now.AddMinutes(30) // Set expiration time for the token
        };
        Response.Cookies.Append("UserToken", token, cookieOptions);

        // Redirect to the dashboard
        return RedirectToAction("Dashboard");
    }
    else
    {
        ViewBag.Message = "Login Failed";
    }
    return View();
}

[Authorize]
public IActionResult Dashboard()
{
    Console.WriteLine("Dashboard action called.");
    var userEmail = HttpContext.Session.GetString("UserSession");
    //var userEmail = User.FindFirstValue(JwtRegisteredClaimNames.Sub); // Extract email from token
    if (userEmail != null)
    {
        var user = context.LogInAndSignUps.Where(x => x.Email == userEmail).FirstOrDefault();
        if (user != null)
        {
            return View(user);
        }
    }
    return RedirectToAction("Login");
}
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

✅ jwt token
C#CC# / help
14mo ago
❔ JWT TOKEN
C#CC# / help
4y ago
[Authorize] Always Returns 401 Even With Valid-Looking JWT Token
C#CC# / help
3mo ago
Why is the generic delegate does not accept my method?
C#CC# / help
4y ago