Blazor jwt from client to server

blazor jwt token

Question

I already have the token in local storage and ready to send to the web api where the controller or the method has en Authorize attribute this es the Blazor client, How do I send the token ?

        var token = Storage["token"];
     await http.GetJsonAsync<string[]>("/api/authorizedController");

And how do I recive the token on the api ? Does it happens automatically or do I have to do somehting ?

    [Authorize]
[Route("api/[controller]")]

Popular Answer

Mate, you also need code on the server to validate the bearer token in your request header on each request.

Try this:

[Route("api/[controller]")]
[Authorize]
public class AutorizedController: Controller

  public void ConfigureServices(IServiceCollection services)
    {
       services.AddIdentityCore<IdentityUser>()
  .AddEntityFrameworkStores<StoreContext>();

    services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
  .AddJwtBearer(cfg =>
  {
    cfg.TokenValidationParameters = new TokenValidationParameters()
    {
      ValidateIssuer = true,
      ValidIssuer = _config["Security:Tokens:Issuer"],
      ValidateAudience = true,
      ValidAudience = _config["Security:Tokens:Audience"],
      ValidateIssuerSigningKey = true,
      IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_config["Security:Tokens:Key"])),

    };
  });

      services.AddDbContext<StoreContext>();
      services.AddMvc();
    }


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why