JWT on .NET Core 2.0

后端 未结 6 1930
忘掉有多难
忘掉有多难 2020-11-28 18:27

I\'ve been on quite an adventure to get JWT working on DotNet core 2.0 (now reaching final release today). There is a ton of documentation, but all the sample code

6条回答
  •  失恋的感觉
    2020-11-28 19:16

    Here is a solution for you.

    In your startup.cs, firstly, config it as services:

      services.AddAuthentication().AddJwtBearer(cfg =>
            {
                cfg.RequireHttpsMetadata = false;
                cfg.SaveToken = true;
                cfg.TokenValidationParameters = new TokenValidationParameters()
                {
                    IssuerSigningKey = "somethong",
                    ValidAudience = "something",
                    :
                };
            });
    

    second, call this services in config

              app.UseAuthentication();
    

    now you can use it in your controller by add attribute

              [Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
              [HttpGet]
              public IActionResult GetUserInfo()
              {
    

    For full details source code that use angular as Frond-end see here

提交回复
热议问题