Home > Back-end >  Asp.net core 6 mvc : Authorize controller methods with JWT token from external API
Asp.net core 6 mvc : Authorize controller methods with JWT token from external API

Time:03-16

I am building a .Net core 6 mvc website which will interact with an API built by an external party. Amongst other things, the user authentication is handled by the API. The API responds with a JWT bearer token once user is authenticated and I need to tie that in to my website to Authorize controller methods.

At this point I call the API and successfully receive the token as expected, however after days of struggling to get [Authorize] to work in the controllers with the token, I am completely lost and hoping for some guidance. After scrapping multiple iterations of code, this what I currently have.... (excuse the mess)

public async Task<TokenResponse> LoginAsync(string email, string password)
        {
            var userLogin = new UserLogin
            {
                Email = email,
                Password = password
            };

            string encoded = System.Convert.ToBase64String(Encoding.GetEncoding("ISO-8859-1").GetBytes(email   ":"   password));
            var client = new RestClient("api location");
            var request = new RestRequest();
            request.AddHeader("Content-Type", "application/json");
            request.AddHeader("Authorization", "Basic "   encoded);
            var response = await client.GetAsync(request);
            var result = JsonConvert.DeserializeObject<TokenResponse>(response.Content);

            return result;

        } 
 public async Task<IActionResult> LoginPostAsync(LoginViewModel viewModel)
        {
            var tokenResponse = await _userManagementService
                                .LoginAsync(viewModel.Email, viewModel.Password);
            if (!string.IsNullOrEmpty(tokenResponse.access_token))
            {
                
                var handler = new JwtSecurityTokenHandler();
                var jwtSecurityToken = handler.ReadJwtToken(tokenResponse.access_token);
                var jti = jwtSecurityToken.Claims.First(claim => claim.Type == "jti").Value;
                var account_type = jwtSecurityToken.Claims.First(claim => claim.Type == "account_type").Value;
                var userId = jwtSecurityToken.Claims.First(claim => claim.Type == "user_id").Value;
                var email = jwtSecurityToken.Claims.First(claim => claim.Type == "email").Value;
                var iss = jwtSecurityToken.Claims.First(claim => claim.Type == "iss").Value;
                string[] userRoles = { "admin", "candidate",};
                HttpContext context = new DefaultHttpContext();
                var accessToken = tokenResponse.access_token;

                //var userClaims = new List<Claim>()
                //     {
                //         new Claim("email", email),
                //         new Claim("account_type", account_type),
                //         new Claim("jti", jti),

                //      };

                //var userIdentity = new ClaimsIdentity(userClaims, "User Identity");
                //var userPrincipal = new ClaimsPrincipal(new[] { userIdentity });
                //context.SignInAsync(userPrincipal);
                //Response.Cookies.Append(
                //    Constants.XAccessToken,
                //    tokenResponse.access_token, new CookieOptions
                //    {
                //        Expires = DateTimeOffset.UtcNow.AddMinutes(1),
                //        HttpOnly = true,
                //        SameSite = SameSiteMode.Strict
                //    });
                //return new AuthenticateResponse(user, token);

                SetJWTCookie(accessToken);
                return RedirectToAction("index", "Home", new { area = "CandidateDashboard" });
            }
            return Unauthorized();

        }

Program.cs

builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)

.AddJwtBearer(config =>
{

    config.SaveToken = true;
    config.RequireHttpsMetadata = false;
    config.TokenValidationParameters = new TokenValidationParameters()
    {
        ValidateAudience = false,
        ValidateIssuer = true,
        ValidIssuer = "issue data",
        ValidateIssuerSigningKey = false,
    };
    config.Events = new JwtBearerEvents
    {
        OnMessageReceived = context =>
        {
            context.Token = context.Request.Cookies["Bearer"];
            return Task.CompletedTask;
        }
};
});
[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
        public IActionResult Index()
        {
            return View();
        }

This is the what I see in dev console. 
--bearer error="invalid_token", error_description="the signature key was not found" 

Payload from Bearer

{
  "iss": "data here",
  "exp": 1647323406,
  "nbf": 1647319746,
  "iat": 1647319806,
  "jti": "e8f297d3-blah blah",
  "account_type": "candidate",
  "user_id": "2342342342",
  "email": "[email protected]"
}

CodePudding user response:

good question - following as ive been struggling with something similar

CodePudding user response:

The core problem is that AddJwtBearer by default only trusts token issued by someone it trusts (the issuer) because it needs to verify the signature of the token. You of course want to verify it so a hacker doesn't send fake/forged tokens to your API.

So either you need to add that

    .AddJwtBearer(opt =>
    {
        opt.Authority = "https://issuer.com"

In this way, AddJwtBearer will download the public signing key automatically for you.

Or you need to add the public signing key manually to AddJwtBearer. see https://devblogs.microsoft.com/dotnet/jwt-validation-and-authorization-in-asp-net-core/

  • Related