• <tfoot id='pBH9m'></tfoot>

  • <small id='pBH9m'></small><noframes id='pBH9m'>

      <bdo id='pBH9m'></bdo><ul id='pBH9m'></ul>
    1. <i id='pBH9m'><tr id='pBH9m'><dt id='pBH9m'><q id='pBH9m'><span id='pBH9m'><b id='pBH9m'><form id='pBH9m'><ins id='pBH9m'></ins><ul id='pBH9m'></ul><sub id='pBH9m'></sub></form><legend id='pBH9m'></legend><bdo id='pBH9m'><pre id='pBH9m'><center id='pBH9m'></center></pre></bdo></b><th id='pBH9m'></th></span></q></dt></tr></i><div id='pBH9m'><tfoot id='pBH9m'></tfoot><dl id='pBH9m'><fieldset id='pBH9m'></fieldset></dl></div>

    2. <legend id='pBH9m'><style id='pBH9m'><dir id='pBH9m'><q id='pBH9m'></q></dir></style></legend>

        ASP.NET Core WebAPI Cookie + JWT 身份验证

        时间:2023-06-03
        • <small id='JdZ3J'></small><noframes id='JdZ3J'>

        • <i id='JdZ3J'><tr id='JdZ3J'><dt id='JdZ3J'><q id='JdZ3J'><span id='JdZ3J'><b id='JdZ3J'><form id='JdZ3J'><ins id='JdZ3J'></ins><ul id='JdZ3J'></ul><sub id='JdZ3J'></sub></form><legend id='JdZ3J'></legend><bdo id='JdZ3J'><pre id='JdZ3J'><center id='JdZ3J'></center></pre></bdo></b><th id='JdZ3J'></th></span></q></dt></tr></i><div id='JdZ3J'><tfoot id='JdZ3J'></tfoot><dl id='JdZ3J'><fieldset id='JdZ3J'></fieldset></dl></div>

          <legend id='JdZ3J'><style id='JdZ3J'><dir id='JdZ3J'><q id='JdZ3J'></q></dir></style></legend>

              <tbody id='JdZ3J'></tbody>

              <tfoot id='JdZ3J'></tfoot>

                  <bdo id='JdZ3J'></bdo><ul id='JdZ3J'></ul>
                • 本文介绍了ASP.NET Core WebAPI Cookie + JWT 身份验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..

                  we have a SPA (Angular) with API backend (ASP.NET Core WebAPI):

                  SPA is listens on app.mydomain.com, API on app.mydomain.com/API

                  We use JWT for Authentication with built-in Microsoft.AspNetCore.Authentication.JwtBearer; I have a controller app.mydomain.com/API/auth/jwt/login which creates tokens. SPA saves them into local storage. All works perfect. After a security audit, we have been told to switch local storage for cookies.

                  The problem is, that API on app.mydomain.com/API is used by SPA but also by a mobile app and several customers server-2-server solutions.

                  So, we have to keep JWT as is, but add Cookies. I found several articles which combines Cookies and JWT on different controllers, but I need them work side-by-side on each controller.

                  If client sends cookies, authenticate via cookies. If client sends JWT bearer, authenticate via JWT.

                  Is this achievable via built-in ASP.NET authentication or DIY middleware?

                  Thanks!

                  解决方案

                  Okay, I have been trying achieving this for a while and i solved same issue of using jwt Authentication Tokens and Cookie Authentication with the following code.

                  API Service Provider UserController.cs

                  This Provide Different Services to the User with Both (Cookie and JWT Bearer)Authentication Schemes

                  [Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
                  [Authorize(AuthenticationSchemes = CookieAuthenticationDefaults.AuthenticationScheme)] 
                  [Route("[controller]")]
                  [ApiController]
                  public class UsersController : ControllerBase
                  { 
                      private readonly IUserServices_Api _services;
                      public UsersController(IUserServices_Api services)
                      {
                          this._services = services;
                      }
                       
                      [HttpGet]
                      public IEnumerable<User> Getall()
                      {
                          return _services.GetAll();
                      }
                  }
                  

                  My Startup.cs

                  public void ConfigureServices(IServiceCollection services)
                      {
                            
                          services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
                           
                          services.AddAuthentication(options => {
                              options.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
                              options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
                              options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
                              options.DefaultScheme = JwtBearerDefaults.AuthenticationScheme;
                          })
                              .AddCookie(options =>
                              {
                                  options.LoginPath = "/Account/Login";
                                  options.AccessDeniedPath = "/Home/Error";
                              })
                              .AddJwtBearer(options =>
                              {
                                  options.SaveToken = true;
                                  options.RequireHttpsMetadata = false;
                                  options.TokenValidationParameters = new Microsoft.IdentityModel.Tokens.TokenValidationParameters()
                                  {
                                      ValidateIssuer = true,
                                      ValidateAudience = true,
                                      ValidAudience = " you site link blah blah",
                                      ValidIssuer = "You Site link Blah  blah",
                                      IssuerSigningKey = new Microsoft.IdentityModel.Tokens.SymmetricSecurityKey(System.Text.Encoding.UTF8.GetBytes(sysController.GetSecurityKey()))
                                      ,
                                      ValidateLifetime = true,
                                      ClockSkew = TimeSpan.Zero
                                  };
                              });
                  
                      }
                  

                  And further if you want custom Authentication for a specific Controller then you have to specify the Authentitcation Type for the Authorization like:

                  [Authorize(AuthenticationSchemes = CookieAuthenticationDefaults.AuthenticationScheme)]
                  public IActionResult Index()
                  {
                      return View();    // This can only be Access when Cookie Authentication is Authorized.
                  }
                  
                  [Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
                  public IActionResult Index()
                  {
                      return View();    // And this one will be Access when JWT Bearer is Valid
                  }
                  

                  这篇关于ASP.NET Core WebAPI Cookie + JWT 身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:ASP.Net Identity 内置函数与 ASP.Net Core 中的自定义表 下一篇:来自文件的 .NET Core IssuerSigningKey 用于 JWT 不记名

                  相关文章

                  最新文章

                    <small id='spXsO'></small><noframes id='spXsO'>

                  1. <i id='spXsO'><tr id='spXsO'><dt id='spXsO'><q id='spXsO'><span id='spXsO'><b id='spXsO'><form id='spXsO'><ins id='spXsO'></ins><ul id='spXsO'></ul><sub id='spXsO'></sub></form><legend id='spXsO'></legend><bdo id='spXsO'><pre id='spXsO'><center id='spXsO'></center></pre></bdo></b><th id='spXsO'></th></span></q></dt></tr></i><div id='spXsO'><tfoot id='spXsO'></tfoot><dl id='spXsO'><fieldset id='spXsO'></fieldset></dl></div>

                      <bdo id='spXsO'></bdo><ul id='spXsO'></ul>
                    <legend id='spXsO'><style id='spXsO'><dir id='spXsO'><q id='spXsO'></q></dir></style></legend>
                      <tfoot id='spXsO'></tfoot>