<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 id='cf5MV'><tfoot id='cf5MV'></tfoot><dl id='cf5MV'><fieldset id='cf5MV'></fieldset></dl></div>
      1. 如何在控制器方法中使 JWT 令牌授权可选

        时间:2023-06-01

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

              <tbody id='4VSPm'></tbody>

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

                • 本文介绍了如何在控制器方法中使 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模板网!

                  上一篇:ASP .NET CORE 2.2 JWT &amp;声明网站身份认证 下一篇:ASP.NET Core 5.0 JWT 身份验证总是抛出 HTTP 401 代码

                  相关文章

                  最新文章

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

                    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 id='tI5sd'><tfoot id='tI5sd'></tfoot><dl id='tI5sd'><fieldset id='tI5sd'></fieldset></dl></div>