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

  • <legend id='4VSPm'><style id='4VSPm'><dir id='4VSPm'><q id='4VSPm'></q></dir></style></legend>

      1. <small id='4VSPm'></small><noframes id='4VSPm'>

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

      2. 如何在控制器方法中使 JWT 令牌授權(quán)可選

        How to make JWT token authorization optional in controller methods(如何在控制器方法中使 JWT 令牌授權(quán)可選)

              <tbody id='cf5MV'></tbody>

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

            • <bdo id='cf5MV'></bdo><ul id='cf5MV'></ul>

                  <legend id='cf5MV'><style id='cf5MV'><dir id='cf5MV'><q id='cf5MV'></q></dir></style></legend>
                  <tfoot id='cf5MV'></tfoot>

                  <i id='cf5MV'><tr id='cf5MV'><dt id='cf5MV'><q id='cf5MV'><span id='cf5MV'><b id='cf5MV'><form id='cf5MV'><ins id='cf5MV'></ins><ul id='cf5MV'></ul><sub id='cf5MV'></sub></form><legend id='cf5MV'></legend><bdo id='cf5MV'><pre id='cf5MV'><center id='cf5MV'></center></pre></bdo></b><th id='cf5MV'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='cf5MV'><tfoot id='cf5MV'></tfoot><dl id='cf5MV'><fieldset id='cf5MV'></fieldset></dl></div>
                • 本文介紹了如何在控制器方法中使 JWT 令牌授權(quán)可選的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

                  問題描述

                  限時送ChatGPT賬號..

                  我在 ASP.NET Core 2 Web 應(yīng)用程序中使用 JWT 令牌

                  I use JWT tokens in my ASP.NET Core 2 web application

                  如果 JWT 令牌失敗,我仍然希望調(diào)用控制器方法,但 ASP.NET 標(biāo)識 User 對象將為空.目前,如果身份驗證失敗,將不會輸入控制器方法,因此我無法在該方法中應(yīng)用邏輯來處理我想以訪客身份處理而不是阻止他們的未經(jīng)身份驗證的用戶.

                  If the JWT token fails I still want the controller method to be hit but the ASP.NET Identity User object will just be null. Currently the controller method won't get entered if authentication fails so I can't apply logic inside the method to deal with non authenticated users which I want to deal with as guests instead of blocking them.

                  我的代碼:

                  Startup.cs

                      public void ConfigureServices(IServiceCollection services)
                      {
                  
                          services.AddAuthentication()
                              .AddJwtBearer(cfg =>
                              {
                                  cfg.TokenValidationParameters = new TokenValidationParameters()
                                  {
                                      ValidIssuer = _config["Tokens:Issuer"],
                                      ValidAudience = _config["Tokens:Audience"],
                                      IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_config["Tokens:Key"]))
                                  };
                  
                              });
                  

                  當(dāng)用戶登錄時

                     private IActionResult CreateToken(ApplicationUser user, IList<string> roles)
                      {
                          var claims = new List<Claim>
                          {
                            new Claim(JwtRegisteredClaimNames.Sub, user.Id.ToString()),
                            new Claim(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString()),
                            new Claim(JwtRegisteredClaimNames.UniqueName, user.UserName)
                          };
                  
                  
                  
                          var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_config["Tokens:Key"]));
                  
                          var creds = new SigningCredentials(key, SecurityAlgorithms.HmacSha256);
                  
                          var token = new JwtSecurityToken(
                            _config["Tokens:Issuer"],
                            _config["Tokens:Audience"],
                            claims.ToArray(),
                           expires: DateTime.Now.AddMinutes(60),
                            signingCredentials: creds);
                  
                          var results = new
                          {
                              token = new JwtSecurityTokenHandler().WriteToken(token),
                              expiration = token.ValidTo,
                          };
                  
                          return Created("", results);
                  

                  我確保在經(jīng)過身份驗證后,各種 API 調(diào)用都會傳遞 JWT 令牌.

                  I make sure after authenticated the various API calls pass the JWT token.

                  在我的控制器中,我使用了 Authorize 屬性

                  In my Controller I make use of an Authorize attribute

                  [Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
                   public class MyController : Controller
                  {
                  

                  這允許我使用以下代碼捕獲登錄用戶以進(jìn)行各種 API 調(diào)用

                  This allows me to capture the logged in user with the following code for the various API calls

                   var loggedInUser = User.Identity.Name;
                  

                  如果我刪除了授權(quán)屬性,那么 User.Identity.Name 將始終為 null,即使用戶已通過身份驗證.

                  If I remove the authorize attribute then User.Identity.Name will always be null even if the user is authenticated.

                  如果令牌無效,如何使用授權(quán)屬性但仍允許輸入控制器方法?我想對要輸入的授權(quán)用戶和非授權(quán)用戶使用相同的方法.然后,我將使用 User.Identity.Name 來確定他們是否未經(jīng)身份驗證,并在他們的方法中包含來賓用戶邏輯.

                  How do I use the authorize attribute but still allow the controller method to be entered if the token is not valid? I want to use the same method for both Authorized and non Authorized users to be entered. I will then use the User.Identity.Name to determine if they are not authenticated and have guest user logic inside the method for them.

                  推薦答案

                  這實際上比你想象的要容易.您可以只使用 both AuthorizeAllowAnonymous,如下所示:

                  It's actually easier than you might expect. You can just use both Authorize and AllowAnonymous, like so:

                  [Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
                  [AllowAnonymous]
                  public class MyController : Controller
                  

                  如果授權(quán)成功,您將擁有一個經(jīng)過身份驗證的 User (User.Identity.IsAuthenticated = true),否則,您將擁有一個匿名 User.

                  If authorization was successful, you'll have an authenticated User (User.Identity.IsAuthenticated = true), otherwise, you will have an anonymous User.

                  這篇關(guān)于如何在控制器方法中使 JWT 令牌授權(quán)可選的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持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#(運(yùn)行總 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)

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

                            <tbody id='tI5sd'></tbody>

                          1. <small id='tI5sd'></small><noframes id='tI5sd'>

                            <tfoot id='tI5sd'></tfoot>

                            <i id='tI5sd'><tr id='tI5sd'><dt id='tI5sd'><q id='tI5sd'><span id='tI5sd'><b id='tI5sd'><form id='tI5sd'><ins id='tI5sd'></ins><ul id='tI5sd'></ul><sub id='tI5sd'></sub></form><legend id='tI5sd'></legend><bdo id='tI5sd'><pre id='tI5sd'><center id='tI5sd'></center></pre></bdo></b><th id='tI5sd'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='tI5sd'><tfoot id='tI5sd'></tfoot><dl id='tI5sd'><fieldset id='tI5sd'></fieldset></dl></div>
                            主站蜘蛛池模板: 国产精品精品久久久 | 亚洲精品欧美一区二区三区 | 鲁大师一区影视 | 夜夜操天天操 | 精品一区二区三区在线视频 | 天天操综合网 | 伊人久麻豆社区 | 岛国一区 | 午夜精品一区二区三区免费视频 | 欧美专区在线视频 | 欧美精品91爱爱 | 麻豆久久 | 一区二区三 | 久久精品男人的天堂 | 久久久久久91 | 日日夜夜精品免费视频 | 91精品国产综合久久久久 | 久久精品免费 | 亚洲一区二区成人 | 99久久99久久精品国产片果冰 | 精品久久久久久久久久久院品网 | 亚洲精品乱码久久久久久按摩观 | 欧美一级视频免费看 | 黄色在线免费观看视频网站 | 国内自拍视频在线观看 | 成人伊人网 | 国产一级淫片免费视频 | 午夜视频一区二区 | 国产精品国产a | av午夜激情 | 亚洲欧美综合 | 亚洲免费在线 | 超碰在线免费av | jizz视频 | www,黄色,com| 国产精品无码久久久久 | 亚洲精品乱码久久久久久蜜桃91 | 性一交一乱一透一a级 | 亚洲播放| 成人午夜影院 | 99久久婷婷国产综合精品首页 |