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

  • <i id='EfxpM'><tr id='EfxpM'><dt id='EfxpM'><q id='EfxpM'><span id='EfxpM'><b id='EfxpM'><form id='EfxpM'><ins id='EfxpM'></ins><ul id='EfxpM'></ul><sub id='EfxpM'></sub></form><legend id='EfxpM'></legend><bdo id='EfxpM'><pre id='EfxpM'><center id='EfxpM'></center></pre></bdo></b><th id='EfxpM'></th></span></q></dt></tr></i><div id='EfxpM'><tfoot id='EfxpM'></tfoot><dl id='EfxpM'><fieldset id='EfxpM'></fieldset></dl></div>
      <bdo id='EfxpM'></bdo><ul id='EfxpM'></ul>
  • <legend id='EfxpM'><style id='EfxpM'><dir id='EfxpM'><q id='EfxpM'></q></dir></style></legend>
      1. <tfoot id='EfxpM'></tfoot>
      2. JWT 令牌错误 401 Unauthorized in .net core 3.1

        时间:2023-06-03

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

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

              <tbody id='k0WPE'></tbody>

                  <i id='k0WPE'><tr id='k0WPE'><dt id='k0WPE'><q id='k0WPE'><span id='k0WPE'><b id='k0WPE'><form id='k0WPE'><ins id='k0WPE'></ins><ul id='k0WPE'></ul><sub id='k0WPE'></sub></form><legend id='k0WPE'></legend><bdo id='k0WPE'><pre id='k0WPE'><center id='k0WPE'></center></pre></bdo></b><th id='k0WPE'></th></span></q></dt></tr></i><div id='k0WPE'><tfoot id='k0WPE'></tfoot><dl id='k0WPE'><fieldset id='k0WPE'></fieldset></dl></div>
                1. 本文介绍了JWT 令牌错误 401 Unauthorized in .net core 3.1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..

                  我正在学习 DDD 和 JWT,所以我考虑在我的应用程序中同时使用这两种方法.问题是这样开始的.当我使用用户名和密码进行请求时,api返回令牌,但是当我把它放在邮递员的标题状态401中时.

                  I am learning DDD and JWT so I thought about using both in my application. The problem starts like this. When I do a resquest with username and password, the api returns the token, but when I put it in the postman's header status 401.

                  我试过把 http 和 https 放在一起.

                  I've tried to put http and https.

                  LoginController.cs

                  LoginController.cs

                  using System;
                  using Microsoft.AspNetCore.Authorization;
                  using Microsoft.AspNetCore.Mvc;
                  using Grp.Domain.Entities;
                  using Grp.Service.Services;
                  
                  namespace Grp.Api.Controllers
                  {
                      [Route("api/[controller]")]
                      [ApiController]
                      public class LoginController : ControllerBase
                      {
                          private readonly LoginService _loginService;
                          private readonly RepresentanteService _representanteService;
                          public LoginController(LoginService loginService,
                              RepresentanteService representanteService)
                          {
                              _loginService = loginService;
                              _representanteService = representanteService;
                          }
                  
                          // POST: api/Login
                          [HttpPost]
                          [AllowAnonymous]
                          public ActionResult<dynamic> Authenticate([FromBody]Representante representante)
                          {
                              try
                              {
                                  representante.Senha = _representanteService.CriptografarSenha(representante.Senha);
                                  var usuarioValido = _loginService.UsuarioValido(representante);
                  
                                  if (!usuarioValido)
                                      return BadRequest(new { message = "Usuário ou senha inválidos" });
                  
                  
                                  var token = TokenService.GenerateToken(representante);
                                  representante.Senha = "";
                  
                                  return new
                                  {
                                      representante,
                                      token
                                  };
                              }
                              catch (Exception ex)
                              {
                                  return BadRequest(ex);
                              }
                          }
                      }
                  }
                  

                  ClientesController.cs

                  ClientesController.cs

                  using System;
                  using Microsoft.AspNetCore.Authorization;
                  using Microsoft.AspNetCore.Mvc;
                  using Grp.Domain.Entities;
                  using Grp.Service.Services;
                  using Grp.Service.Validators;
                  
                  namespace OpersanEM.Api.Controllers
                  {
                      [Authorize]
                      [Route("api/[controller]")]
                      [ApiController]
                      public class ClientesController : ControllerBase
                      {
                          private readonly BaseService<Cliente> _service;
                          public ClientesController(BaseService<Cliente> service)
                          {
                              _service = service;
                          }
                          // GET: api/Clientes
                          [HttpGet]
                          public IActionResult Get()
                          {
                              try
                              {
                                  return new ObjectResult(_service.Get());
                              }
                              catch (Exception ex)
                              {
                                  return BadRequest(ex.Message);
                              }
                          }
                  
                          // GET: api/Clientes/5
                          [HttpGet("{id}")]
                          public IActionResult Get(int id)
                          {
                              try
                              {
                                  return new ObjectResult(_service.Get(id));
                              }
                              catch (ArgumentException ex)
                              {
                                  return NotFound(ex);
                              }
                              catch (Exception ex)
                              {
                                  return BadRequest(ex.Message);
                              }
                          }
                  
                          // POST: api/Clientes
                          [HttpPost]
                          public IActionResult Post([FromBody] Cliente item)
                          {
                              try
                              {
                                  _service.Post<ClienteValidator>(item);
                  
                                  return new ObjectResult(item.Id);
                              }
                              catch (ArgumentNullException ex)
                              {
                                  return NotFound(ex);
                              }
                              catch (Exception ex)
                              {
                                  return BadRequest(ex.Message);
                              }
                          }
                  
                          // PUT: api/Clientes/5
                          [HttpPut("{id}")]
                          public IActionResult Put([FromBody] Cliente item)
                          {
                              try
                              {
                                  _service.Put<ClienteValidator>(item);
                  
                                  return new ObjectResult(item);
                              }
                              catch (ArgumentNullException ex)
                              {
                                  return NotFound(ex);
                              }
                              catch (Exception ex)
                              {
                                  return BadRequest(ex.Message);
                              }
                          }
                  
                          // DELETE: api/ApiWithActions/5
                          [HttpDelete("{id}")]
                          public IActionResult Delete(int id)
                          {
                              try
                              {
                                  _service.Delete(id);
                  
                                  return new NoContentResult();
                              }
                              catch (ArgumentException ex)
                              {
                                  return NotFound(ex);
                              }
                              catch (Exception ex)
                              {
                                  return BadRequest(ex.Message);
                              }
                          }
                      }
                  }
                  

                  Startup.cs

                  using Microsoft.AspNetCore.Authentication.JwtBearer;
                  using Microsoft.AspNetCore.Builder;
                  using Microsoft.AspNetCore.Hosting;
                  using Microsoft.AspNetCore.Mvc;
                  using Microsoft.Extensions.Configuration;
                  using Microsoft.Extensions.DependencyInjection;
                  using Microsoft.Extensions.Hosting;
                  using Microsoft.IdentityModel.Tokens;
                  using Grp.Domain.Entities;
                  using Grp.Infra.CrossCutting;
                  using Grp.Infra.Data.Context;
                  using Grp.Infra.Data.Repository;
                  using Grp.Service.Services;
                  using System.Text;
                  
                  namespace Grp.Api
                  {
                      public class Startup
                      {
                          public Startup(IConfiguration configuration)
                          {
                              Configuration = configuration;
                          }
                  
                          public IConfiguration Configuration { get; }
                  
                          // This method gets called by the runtime. Use this method to add services to the container.
                          public void ConfigureServices(IServiceCollection services)
                          {
                              services.AddCors();
                              services.AddControllers();
                  
                              var key = Encoding.ASCII.GetBytes(Settings.Secret);
                              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(key),
                                      ValidateIssuer = false,
                                      ValidateAudience = false
                                  };
                              });
                  
                              services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_3_0);
                              services.AddScoped<SqlContext, SqlContext>();
                  
                              services.AddScoped<BaseRepository<Cliente>>();
                              services.AddScoped<BaseRepository<Representante>>();
                  
                              services.AddScoped<BaseService<Cliente>>();
                              services.AddScoped<BaseService<Representante>>();
                              services.AddScoped<RepresentanteService>();
                              services.AddScoped<LoginService>();
                  
                              services.AddScoped<StringCipher>();
                  
                  
                          }
                  
                          // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
                          public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
                          {
                              if (env.IsDevelopment())
                              {
                                  app.UseDeveloperExceptionPage();
                              }
                  
                              app.UseStaticFiles();
                              app.UseRouting();
                  
                              app.UseCors(x => x
                              .AllowAnyOrigin()
                              .AllowAnyMethod()
                              .AllowAnyHeader());
                  
                              app.UseAuthentication();
                              app.UseAuthorization();
                  
                  
                              app.UseEndpoints(endpoints =>
                              {
                                  endpoints.MapControllerRoute("default", "{controller=Home}/{action=Index}");
                              });
                          }
                      }
                  }
                  

                  TokenService.cs

                  TokenService.cs

                  using Microsoft.IdentityModel.Tokens;
                  using Grp.Domain.Entities;
                  using System;
                  using System.IdentityModel.Tokens.Jwt;
                  using System.Security.Claims;
                  using System.Text;
                  
                  namespace Grp.Service.Services
                  {
                      public static class TokenService
                      {
                          public static string GenerateToken(Representante representante)
                          {
                              var tokenHandler = new JwtSecurityTokenHandler();
                              var key = Encoding.ASCII.GetBytes(Settings.Secret);
                              var tokenDescriptor = new SecurityTokenDescriptor
                              {
                                  Subject = new ClaimsIdentity(new Claim[]
                                  {
                                      new Claim(ClaimTypes.Name, representante.Nome)
                                  }),
                                  Expires = DateTime.UtcNow.AddDays(2),
                                  SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(key), SecurityAlgorithms.HmacSha256Signature)
                              };
                              var token = tokenHandler.CreateToken(tokenDescriptor);
                              return tokenHandler.WriteToken(token);
                          }
                      }
                  }
                  

                  推荐答案

                  你找到解决方案了吗?我在同一问题上滚动了一段时间.最后我解决了它添加[Authorize(AuthenticationSchemes=JwtBearerDefaults.AuthenticationScheme)]在我的控制器的装饰器中.

                  Did you find a solution? I was rolling around the same problem for a while. Finally I Solved it adding [Authorize(AuthenticationSchemes=JwtBearerDefaults.AuthenticationScheme)] in the decorator of my controller.

                  如果您不想在每个控制器中设置方案,请在 Startup.cs ConfigureServices() 中配置它

                  If you don't want to set the scheme in every controller then configure it in the Startup.cs ConfigureServices()

                          services.AddAuthorization(options =>
                          {
                              var defaultAuthorizationPolicyBuilder = new AuthorizationPolicyBuilder(
                                  JwtBearerDefaults.AuthenticationScheme);
                  
                              defaultAuthorizationPolicyBuilder =
                                  defaultAuthorizationPolicyBuilder.RequireAuthenticatedUser();
                  
                              options.DefaultPolicy = defaultAuthorizationPolicyBuilder.Build();
                          });
                  

                  这篇关于JWT 令牌错误 401 Unauthorized in .net core 3.1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:验证 JWT 签名时出现 SecurityTokenSignatureKeyNotFoundE 下一篇:Jwt Bearer 和依赖注入

                  相关文章

                  最新文章

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

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

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

                  1. <tfoot id='OuSep'></tfoot>

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