久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

  • <legend id='r5z6o'><style id='r5z6o'><dir id='r5z6o'><q id='r5z6o'></q></dir></style></legend>

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

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

      <tfoot id='r5z6o'></tfoot>
      • <bdo id='r5z6o'></bdo><ul id='r5z6o'></ul>

      1. .Net Core JWT 身份驗證與自定義 API 密鑰中間件

        .Net Core JWT Authentication with custom API Key Middleware(.Net Core JWT 身份驗證與自定義 API 密鑰中間件)

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

          <bdo id='c7NrZ'></bdo><ul id='c7NrZ'></ul>
            <i id='c7NrZ'><tr id='c7NrZ'><dt id='c7NrZ'><q id='c7NrZ'><span id='c7NrZ'><b id='c7NrZ'><form id='c7NrZ'><ins id='c7NrZ'></ins><ul id='c7NrZ'></ul><sub id='c7NrZ'></sub></form><legend id='c7NrZ'></legend><bdo id='c7NrZ'><pre id='c7NrZ'><center id='c7NrZ'></center></pre></bdo></b><th id='c7NrZ'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='c7NrZ'><tfoot id='c7NrZ'></tfoot><dl id='c7NrZ'><fieldset id='c7NrZ'></fieldset></dl></div>
              <tbody id='c7NrZ'></tbody>
          • <legend id='c7NrZ'><style id='c7NrZ'><dir id='c7NrZ'><q id='c7NrZ'></q></dir></style></legend>

            <tfoot id='c7NrZ'></tfoot>

                  本文介紹了.Net Core JWT 身份驗證與自定義 API 密鑰中間件的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                  問題描述

                  限時送ChatGPT賬號..

                  我有一個使用 JWT 令牌授權(quán)用戶的 .Net Core 2.0 應用程序.這一切都很好,但我想要某種 API 密鑰機制來允許其他應用程序集成,但我似乎無法讓它與當前的身份驗證一起使用.

                  I have a .Net Core 2.0 application that uses JWT tokens to authorize the user. This all works fine but I want to have some sort of API Key mechanism to allow other applications to integrate but I cannot seem to get this to work with the current authentication.

                  代碼:

                  Startup.cs

                  public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory,
                              IMemoryCache cache, IServiceProvider serviceProvider)
                  {
                      app.UseAuthentication();
                  
                      ApiKeyMiddlewear(app, serviceProvider);
                  
                      app.UseMvc(routes =>
                      { 
                          routes.MapRoute(
                                 name: "default",
                                  template: "{controller=Home}/{action=Index}/{id?}");
                  
                              routes.MapSpaFallbackRoute(
                                  name: "spa-fallback",
                                  defaults: new { controller = "Home", action = "Index" });
                          });
                      }
                  }
                  
                  
                      private static void ApiKeyMiddlewear(IApplicationBuilder app, IServiceProvider serviceProvider)
                      {
                          app.Use(async (context, next) =>
                          {
                              if (context.Request.Path.StartsWithSegments(new PathString("/api")))
                              {
                                  // Let's check if this is an API Call
                                  if (context.Request.Headers["ApiKey"].Any())
                                  {
                                      // validate the supplied API key
                                      // Validate it
                                      var headerKey = context.Request.Headers["ApiKey"].FirstOrDefault();
                                      var settingsProvider = serviceProvider.GetService<ISettingsService<OmbiSettings>>();
                                      var ombiSettings = settingsProvider.GetSettings();
                                      var valid = ombiSettings.ApiKey.Equals(headerKey, StringComparison.CurrentCultureIgnoreCase);
                                      if (!valid)
                                      {
                                          context.Response.StatusCode = (int) HttpStatusCode.Unauthorized;
                                          await context.Response.WriteAsync("Invalid API Key");
                                      }
                                      else
                                      {
                                          var identity = new GenericIdentity("API");
                                          identity.AddClaim(new System.Security.Claims.Claim("Origin", "Api"));
                                          identity.AddClaim(new System.Security.Claims.Claim("role", "Admin"));
                  
                                          var principal = new GenericPrincipal(identity, new[] {"ApiUser"});
                                          context.User = principal;
                                          await next();
                                      }
                                  }
                                  else
                                  {
                                      await next();
                                  }
                              }
                              else
                              {
                                  await next();
                              }
                          });
                      }
                  }
                  

                  所以在上面的代碼中,您可以看到我正在攔截提供名為 ApiKey 的標頭的 HTTP 請求,然后將其驗證為我存儲的內(nèi)容.這部分一切正常,但是當使用 Authorize 屬性調(diào)用 API 方法時,這不起作用,我得到以下錯誤日志:

                  So in the code above you can see that I am intercepting the HTTP requests that provide a header called ApiKey and then validate it to what I have stored. This part all works but when calling an API Method with the Authorize attribute this does not work and I get the following error logs:

                  2017-09-19 08:15:17.280 +01:00 [Information] Request starting HTTP/1.1 POST http://localhost:52038/api/v1/Identity/ application/json 372
                  2017-09-19 08:15:21.967 +01:00 [Information] Authorization failed for user: "API".
                  2017-09-19 08:15:21.976 +01:00 [Information] Authorization failed for the request at filter '"Microsoft.AspNetCore.Mvc.Authorization.AuthorizeFilter"'.
                  2017-09-19 08:15:21.981 +01:00 [Information] Executing ForbidResult with authentication schemes ([]).
                  2017-09-19 08:15:21.991 +01:00 [Information] AuthenticationScheme: "Bearer" was forbidden.
                  2017-09-19 08:15:21.996 +01:00 [Information] Executed action "Ombi.Controllers.IdentityController.CreateUser (Ombi)" in 38.8268ms
                  2017-09-19 08:15:22.004 +01:00 [Information] Request finished in 4723.032ms 403 
                  

                  現(xiàn)在我猜這與僅提供 ApiKey 標頭而不是帶有正確 JWT 令牌的 Authorization 標頭的請求有關(guān).

                  Now I'm guessing this is to do with the request only supplying a ApiKey header and not a Authorization header with a correct JWT token.

                  我怎樣才能只提供 ApiKey 標頭,當沒有 ApiKey 標頭時回退到需要 JWT 令牌?

                  How am I able to only supply a ApiKey header and when there is no ApiKey header then fallback to requiring a JWT token?

                  推薦答案

                  應用 Claim("role", "Admin")GenericPrincipal 不會有任何影響,因為GenericPrincipal 與角色聲明無關(guān).所以如果你想為 GenericPrincipal 應用管理員角色,你需要在構(gòu)造函數(shù)參數(shù)中添加它:

                  Applying Claim("role", "Admin") to GenericPrincipal will not affect anything, because GenericPrincipal have nothing to do with Role Claims. So if you want to apply admin role to GenericPrincipal, you need to add it in constructor parameter:

                   var principal = new GenericPrincipal(identity, new[] {"Admin","ApiUser"});
                   context.User = principal;
                  

                  這篇關(guān)于.Net Core JWT 身份驗證與自定義 API 密鑰中間件的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

                  【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

                  相關(guān)文檔推薦

                  What are good algorithms for vehicle license plate detection?(車牌檢測有哪些好的算法?)
                  onClick event for Image in Unity(Unity中圖像的onClick事件)
                  Running Total C#(運行總 C#)
                  Deleting a directory when clicked on a hyperlink with JAvascript.ASP.NET C#(單擊帶有 JAvascript.ASP.NET C# 的超鏈接時刪除目錄)
                  asp.net listview highlight row on click(asp.net listview 在單擊時突出顯示行)
                  Calling A Button OnClick from a function(從函數(shù)調(diào)用按鈕 OnClick)
                    <tfoot id='vSVfl'></tfoot>

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

                      <legend id='vSVfl'><style id='vSVfl'><dir id='vSVfl'><q id='vSVfl'></q></dir></style></legend>
                      • <bdo id='vSVfl'></bdo><ul id='vSVfl'></ul>

                            <tbody id='vSVfl'></tbody>

                          1. <i id='vSVfl'><tr id='vSVfl'><dt id='vSVfl'><q id='vSVfl'><span id='vSVfl'><b id='vSVfl'><form id='vSVfl'><ins id='vSVfl'></ins><ul id='vSVfl'></ul><sub id='vSVfl'></sub></form><legend id='vSVfl'></legend><bdo id='vSVfl'><pre id='vSVfl'><center id='vSVfl'></center></pre></bdo></b><th id='vSVfl'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='vSVfl'><tfoot id='vSVfl'></tfoot><dl id='vSVfl'><fieldset id='vSVfl'></fieldset></dl></div>
                            主站蜘蛛池模板: 久久久999成人 | 欧美在线a| 日韩欧美国产一区二区 | 欧美性生活一区二区三区 | 欧美性jizz18性欧美 | 黄a免费网络 | 久久久精选 | 黄色片亚洲 | 国产成人福利视频在线观看 | 日本午夜视频 | 亚洲精品国产综合区久久久久久久 | 综合久久久久 | 91精品综合久久久久久五月天 | 欧美电影免费观看 | 色免费在线视频 | 欧美视频在线播放 | 中文字幕电影在线观看 | 国产最好的av国产大片 | 国产精品99久久久久久久久久久久 | av黄在线观看 | 欧美久久一区二区 | 亚洲国产精品久久人人爱 | 欧美一级www片免费观看 | 天天干天天色 | 少妇特黄a一区二区三区88av | 欧美性生活一区二区三区 | 欧美一级片在线观看 | 午夜国产羞羞视频免费网站 | 日本一区二区影视 | 在线色网站 | 欧美久久久电影 | 国产一区二区久久久 | 欧美色欧美亚洲另类七区 | 日韩欧美在线免费观看 | 日本成人在线网址 | 欧美大片一区二区 | 亚洲精品乱码久久久久久久久久 | 高清黄色毛片 | 精品国产一区二区三区久久久四川 | 国产免费让你躁在线视频 | av男人天堂影院 |