pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

  • <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="pvvvbv7" 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 令牌授權可選

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

              <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="3hnnvpt" id='cf5MV'><tfoot id='cf5MV'></tfoot><dl id='cf5MV'><fieldset id='cf5MV'></fieldset></dl></div>
                • 本文介紹了如何在控制器方法中使 JWT 令牌授權可選的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                  問題描述

                  限時送ChatGPT賬號..

                  我在 ASP.NET Core 2 Web 應用程序中使用 JWT 令牌

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

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

                  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"]))
                                  };
                  
                              });
                  

                  當用戶登錄時

                     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);
                  

                  我確保在經過身份驗證后,各種 API 調用都會傳遞 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
                  {
                  

                  這允許我使用以下代碼捕獲登錄用戶以進行各種 API 調用

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

                   var loggedInUser = User.Identity.Name;
                  

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

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

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

                  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
                  

                  如果授權成功,您將擁有一個經過身份驗證的 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.

                  這篇關于如何在控制器方法中使 JWT 令牌授權可選的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

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

                  相關文檔推薦

                  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(從函數調用按鈕 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="h7jpfth" id='tI5sd'><tfoot id='tI5sd'></tfoot><dl id='tI5sd'><fieldset id='tI5sd'></fieldset></dl></div>
                            主站蜘蛛池模板: 企业VI设计_LOGO设计公司_品牌商标设计_【北京美研】 | 江西自考网-江西自学考试网 | 胶原检测试剂盒,弹性蛋白检测试剂盒,类克ELISA试剂盒,阿达木单抗ELISA试剂盒-北京群晓科苑生物技术有限公司 | 山东限矩型液力偶合器_液力耦合器易熔塞厂家-淄博市汇川源机械厂 | 聚氨酯复合板保温板厂家_廊坊华宇创新科技有限公司 | 档案密集柜_手动密集柜_智能密集柜_内蒙古档案密集柜-盛隆柜业内蒙古密集柜直销中心 | 信阳市建筑勘察设计研究院有限公司| 物流公司电话|附近物流公司电话上门取货 | 耳模扫描仪-定制耳机设计软件-DLP打印机-asiga打印机-fitshape「飞特西普」 | 常州翔天实验仪器厂-恒温振荡器-台式恒温振荡器-微量血液离心机 恒温恒湿箱(药品/保健品/食品/半导体/细菌)-兰贝石(北京)科技有限公司 | 不锈钢水箱生产厂家_消防水箱生产厂家-河南联固供水设备有限公司 | 分光色差仪,测色仪,反透射灯箱,爱色丽分光光度仪,美能达色差仪维修_苏州欣美和仪器有限公司 | 北京律师事务所_房屋拆迁律师_24小时免费法律咨询_云合专业律师网 | _网名词典_网名大全_qq网名_情侣网名_个性网名 | 湖南档案密集架,智能,物证,移动,价格-湖南档案密集架厂家 | 云南外加剂,云南速凝剂,云南外加剂代加工-普洱澜湄新材料科技有限公司 | 注塑机-压铸机-塑料注塑机-卧式注塑机-高速注塑机-单缸注塑机厂家-广东联升精密智能装备科技有限公司 | 花纹铝板,合金铝卷板,阴极铝板-济南恒诚铝业有限公司 | 讲师宝经纪-专业培训机构师资供应商_培训机构找讲师、培训师、讲师经纪就上讲师宝经纪 | AGV无人叉车_激光叉车AGV_仓储AGV小车_AGV无人搬运车-南昌IKV机器人有限公司[官网] | 自动部分收集器,进口无油隔膜真空泵,SPME固相微萃取头-上海楚定分析仪器有限公司 | 北京翻译公司_同传翻译_字幕翻译_合同翻译_英语陪同翻译_影视翻译_翻译盖章-译铭信息 | SEO网站优化,关键词排名优化,苏州网站推广-江苏森歌网络 | 网站建设-高端品牌网站设计制作一站式定制_杭州APP/微信小程序开发运营-鼎易科技 | 质检报告_CE认证_FCC认证_SRRC认证_PSE认证_第三方检测机构-深圳市环测威检测技术有限公司 | 武汉EPS线条_EPS装饰线条_EPS构件_湖北博欧EPS线条厂家 | 皮带机_移动皮带机_大倾角皮带机_皮带机厂家 - 新乡市国盛机械设备有限公司 | ETFE膜结构_PTFE膜结构_空间钢结构_膜结构_张拉膜_浙江萬豪空间结构集团有限公司 | 合肥白癜风医院_合肥治疗白癜风医院_合肥看白癜风医院哪家好_合肥华研白癜风医院 | 哲力实业_专注汽车涂料汽车漆研发生产_汽车漆|修补油漆品牌厂家 长沙一级消防工程公司_智能化弱电_机电安装_亮化工程专业施工承包_湖南公共安全工程有限公司 | 智能风向风速仪,风速告警仪,数字温湿仪,综合气象仪(气象五要素)-上海风云气象仪器有限公司 | 上海盐水喷雾试验机_两厢式冷热冲击试验箱-巨怡环试 | 广州云仓代发-昊哥云仓专业电商仓储托管外包代发货服务 | 无锡网站建设_企业网站定制-网站制作公司-阿凡达网络 | 罗茨真空机组,立式无油往复真空泵,2BV水环真空泵-力侨真空科技 | 河南卓美创业科技有限公司-河南卓美防雷公司-防雷接地-防雷工程-重庆避雷针-避雷器-防雷检测-避雷带-避雷针-避雷塔、机房防雷、古建筑防雷等-山西防雷公司 | MVE振动电机_MVE震动电机_MVE卧式振打电机-河南新乡德诚生产厂家 | 魔方网-培训咨询服务平台 | 翰墨AI智能写作助手官网_人工智能问答在线AI写作免费一键生成 | 家乐事净水器官网-净水器厂家「官方」 | led冷热冲击试验箱_LED高低温冲击试验箱_老化试验箱-爱佩百科 |