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

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

      <bdo id='G9lMs'></bdo><ul id='G9lMs'></ul>

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

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

      1. <tfoot id='G9lMs'></tfoot>
      2. ASP.NET 核心 JWT 身份驗證總是拋出 401 未授權

        ASP.NET core JWT authentication always throwing 401 unauthorized(ASP.NET 核心 JWT 身份驗證總是拋出 401 未授權)

          <tfoot id='vACOq'></tfoot>
            <tbody id='vACOq'></tbody>
            <bdo id='vACOq'></bdo><ul id='vACOq'></ul>

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

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

            1. <legend id='vACOq'><style id='vACOq'><dir id='vACOq'><q id='vACOq'></q></dir></style></legend>
                • 本文介紹了ASP.NET 核心 JWT 身份驗證總是拋出 401 未授權的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                  問題描述

                  限時送ChatGPT賬號..

                  我正在嘗試盡可能簡單地在我的 asp.net 核心 webAPI 上實現(xiàn) JWT 身份驗證.我不知道我錯過了什么,但即使使用 proper 不記名令牌,它也總是返回 401.

                  I'm trying to implement JWT authentication on my asp.net core webAPI as simply as possible. I don't know what i'm missing but it's always returning 401 even with the proper bearer token.

                  這是我的 configureServices 代碼

                  here is my configureServices code

                  public void ConfigureServices(IServiceCollection services)
                          {
                              services.AddAuthentication(x =>
                              {
                                  x.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
                                  x.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
                  
                              }).AddJwtBearer(
                                 x =>
                                 {
                                     x.RequireHttpsMetadata = false;
                                     x.SaveToken = true;
                                     x.TokenValidationParameters = new TokenValidationParameters
                                     {
                                         ValidateIssuerSigningKey = true,
                                         IssuerSigningKey = new SymmetricSecurityKey(Encoding.ASCII.GetBytes("A_VERY_SECRET_SECURITY_KEY_FOR_JWT_AUTH")),
                                         ValidateAudience = false,
                                         ValidateIssuer = false,
                                     };
                                 }
                                  );
                              services.AddControllers();
                  
                              services.AddDbContext<dingdogdbContext>(options =>
                                      options.UseSqlServer(Configuration.GetConnectionString("dingdogdbContext")));
                          }
                  
                  

                  這就是我生成令牌的方式

                  and this is how I'm generating token

                          [AllowAnonymous]
                          [HttpPost("/Login")]
                          public ActionResult<User> Login(AuthModel auth)
                          {
                              var user = new User();
                              user.Email = auth.Email;
                              user.Password = auth.Password;
                              //var user = await _context.User.SingleOrDefaultAsync(u=> u.Email == auth.Email && u.Password==auth.Password);
                              //if(user==null) return NotFound("User not found with this creds");
                  
                              //starting token generation...
                              var tokenHandler = new JwtSecurityTokenHandler();
                              var seckey = new SymmetricSecurityKey(Encoding.ASCII.GetBytes("A_VERY_SECRET_SECURITY_KEY_FOR_JWT_AUTH"));
                              var signingCreds = new SigningCredentials(seckey, SecurityAlgorithms.HmacSha256Signature);
                              var token = tokenHandler.CreateToken(new SecurityTokenDescriptor
                              {
                                  Subject = new System.Security.Claims.ClaimsIdentity(new Claim[] { new Claim(ClaimTypes.Name, user.Id.ToString()) }),
                                  SigningCredentials = signingCreds,
                                  Expires = DateTime.UtcNow.AddDays(7),
                              });
                              user.Token = tokenHandler.WriteToken(token);
                              return user;
                          }
                  

                  我在 app.useRouting() 之后添加了 app.useAuthorization().當我向/Login 發(fā)送 POST 請求時,我得到了令牌.但是當我使用令牌查詢使用郵遞員的任何其他端點時(在郵遞員的授權/JWT 中添加令牌)每次都獲得 401 未經(jīng)授權.我還缺少什么嗎?

                  And I added app.useAuthorization() very after the app.useRouting(). when i'm sending POST request to /Login I'm getting the token. but when I'm using the token in for querying any other endpoint using postman(added the token in authorization/JWT in postman) getting 401 unauthorized every time. is there anything I'm missing still?

                  推薦答案

                  記住 UseAuthenticationUseRoutingUseAuthorization 中間件必須正確,以便 ASP 框架正確地將身份上下文注入 http 請求.

                  Keep in mind that the UseAuthentication, UseRouting and UseAuthorization middleware must in correct in order for the ASP framework properly inject the identity context to http request.

                  它應該看起來像這樣:(.NET Core 3.1)

                  It should look like this: (.NET Core 3.1)

                  相同的代碼適用于 .NET 5 &.NET 6

                  the same code applies to .NET 5 & .NET 6

                              app.UseAuthentication();
                              app.UseRouting();
                              app.UseAuthorization();
                              app.UseEndpoints(endpoints =>
                              {
                                  endpoints.MapControllers();
                              });
                  

                  這篇關于ASP.NET 核心 JWT 身份驗證總是拋出 401 未授權的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

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

                  相關文檔推薦

                  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)
                  ASP.net C# Gridview ButtonField onclick event(ASP.net C# Gridview ButtonField onclick 事件)
                  Adding OnClick event to ASP.NET control(將 OnClick 事件添加到 ASP.NET 控件)
                  Multiple submit Button click problem?(多個提交按鈕點擊問題?)
                  <legend id='McVAC'><style id='McVAC'><dir id='McVAC'><q id='McVAC'></q></dir></style></legend>
                    <bdo id='McVAC'></bdo><ul id='McVAC'></ul>
                    • <tfoot id='McVAC'></tfoot>

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

                          <tbody id='McVAC'></tbody>

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

                          • 主站蜘蛛池模板: 液氮罐_液氮容器_自增压液氮罐_杜瓦瓶_班德液氮罐厂家 | 氢氧化钙设备, 氢氧化钙生产线-淄博惠琛工贸有限公司 | 金属清洗剂,防锈油,切削液,磨削液-青岛朗力防锈材料有限公司 | 砂磨机_立式纳米砂磨机_实验室砂磨机-广州儒佳化工设备厂家 | 永嘉县奥阳陶瓷阀门有限公司 | 山东锐智科电检测仪器有限公司_超声波测厚仪,涂层测厚仪,里氏硬度计,电火花检漏仪,地下管线探测仪 | 东莞市天进机械有限公司-钉箱机-粘箱机-糊箱机-打钉机认准东莞天进机械-厂家直供更放心! | 广州食堂承包_广州团餐配送_广州堂食餐饮服务公司 - 旺记餐饮 | 12cr1mov无缝钢管切割-15crmog无缝钢管切割-40cr无缝钢管切割-42crmo无缝钢管切割-Q345B无缝钢管切割-45#无缝钢管切割 - 聊城宽达钢管有限公司 | 石膏基自流平砂浆厂家-高强石膏基保温隔声自流平-轻质抹灰石膏粉砂浆批发-永康市汇利建设有限公司 | 耐磨陶瓷,耐磨陶瓷管道_厂家-淄博拓创陶瓷科技 | 七维官网-水性工业漆_轨道交通涂料_钢结构漆| 丹佛斯压力传感器,WISE温度传感器,WISE压力开关,丹佛斯温度开关-上海力笙工业设备有限公司 | 危废处理系统,水泥厂DCS集散控制系统,石灰窑设备自动化控制系统-淄博正展工控设备 | 在线浊度仪_悬浮物污泥浓度计_超声波泥位计_污泥界面仪_泥水界面仪-无锡蓝拓仪表科技有限公司 | 环保袋,无纺布袋,无纺布打孔袋,保温袋,环保袋定制,环保袋厂家,环雅包装-十七年环保袋定制厂家 | 纯化水设备-纯水设备-超纯水设备-[大鹏水处理]纯水设备一站式服务商-东莞市大鹏水处理科技有限公司 | 上海三信|ph计|酸度计|电导率仪-艾科仪器 | 风电变桨伺服驱动器-风电偏航变桨系统-深圳众城卓越科技有限公司 | 压力变送器-上海武锐自动化设备有限公司 | 维泰克Veertek-锂电池微短路检测_锂电池腐蚀检测_锂电池漏液检测 | 山东成考网-山东成人高考网| 自清洗过滤器_全自动过滤器_全自动反冲洗过滤器_量子过滤器-滑漮滴 | 耐高温风管_耐高温软管_食品级软管_吸尘管_钢丝软管_卫生级软管_塑料波纹管-东莞市鑫翔宇软管有限公司 | 包塑丝_高铁绑丝_地暖绑丝_涂塑丝_塑料皮铁丝_河北创筹金属丝网制品有限公司 | 北京工业设计公司-产品外观设计-产品设计公司-千策良品工业设计 北京翻译公司-专业合同翻译-医学标书翻译收费标准-慕迪灵 | 月嫂_保姆_育婴_催乳_母婴护理_产后康复_养老护理-吉祥到家家政 硫酸亚铁-聚合硫酸铁-除氟除磷剂-复合碳源-污水处理药剂厂家—长隆科技 | 冲击式破碎机-冲击式制砂机-移动碎石机厂家_青州市富康机械有限公司 | 铝单板_铝窗花_铝单板厂家_氟碳包柱铝单板批发价格-佛山科阳金属 | 钢化玻璃膜|手机钢化膜|钢化膜厂家|手机保护膜-【东莞市大象电子科技有限公司】 | RTO换向阀_VOC高温阀门_加热炉切断阀_双偏心软密封蝶阀_煤气蝶阀_提升阀-湖北霍科德阀门有限公司 | 护腰带生产厂家_磁石_医用_热压护腰_登山护膝_背姿矫正带_保健护具_医疗护具-衡水港盛 | 涂层测厚仪_光泽度仪_uv能量计_紫外辐照计_太阳膜测试仪_透光率仪-林上科技 | Brotu | 关注AI,Web3.0,VR/AR,GPT,元宇宙区块链数字产业 | 重庆小面培训_重庆小面技术培训学习班哪家好【终身免费复学】 | 警用|治安|保安|不锈钢岗亭-售货亭价格-垃圾分类亭-移动厕所厂家-苏州灿宇建材 | 滚珠丝杆升降机_螺旋升降机_丝杠升降机-德迈传动 | 远程会诊系统-手术示教系统【林之硕】医院远程医疗平台 | 首页|专注深圳注册公司,代理记账报税,注册商标代理,工商变更,企业400电话等企业一站式服务-慧用心 | Q361F全焊接球阀,200X减压稳压阀,ZJHP气动单座调节阀-上海戎钛 | 骁龙云呼电销防封号系统-axb电销平台-外呼稳定『免费试用』 |