• <legend id='S8k8Z'><style id='S8k8Z'><dir id='S8k8Z'><q id='S8k8Z'></q></dir></style></legend>

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

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

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

        在 ASP.NET Core 的 Swagger 中使用 JWT(授权:Bearer)

        时间:2023-06-03

              • <bdo id='5ZY6r'></bdo><ul id='5ZY6r'></ul>
                  <tbody id='5ZY6r'></tbody>

                <tfoot id='5ZY6r'></tfoot>
                <i id='5ZY6r'><tr id='5ZY6r'><dt id='5ZY6r'><q id='5ZY6r'><span id='5ZY6r'><b id='5ZY6r'><form id='5ZY6r'><ins id='5ZY6r'></ins><ul id='5ZY6r'></ul><sub id='5ZY6r'></sub></form><legend id='5ZY6r'></legend><bdo id='5ZY6r'><pre id='5ZY6r'><center id='5ZY6r'></center></pre></bdo></b><th id='5ZY6r'></th></span></q></dt></tr></i><div id='5ZY6r'><tfoot id='5ZY6r'></tfoot><dl id='5ZY6r'><fieldset id='5ZY6r'></fieldset></dl></div>
              • <small id='5ZY6r'></small><noframes id='5ZY6r'>

                <legend id='5ZY6r'><style id='5ZY6r'><dir id='5ZY6r'><q id='5ZY6r'></q></dir></style></legend>
                • 本文介绍了在 ASP.NET Core 的 Swagger 中使用 JWT(授权:Bearer)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..

                  我正在 ASP.NET Core 1.0 中创建一个 REST api.我使用 Swagger 进行测试,但现在我为某些路由添加了 JWT 授权.(使用 UseJwtBearerAuthentication)

                  I'm creating a REST api in ASP.NET Core 1.0. I was using Swagger to test but now I added JWT authorization for some routes. (with UseJwtBearerAuthentication)

                  是否可以修改 Swagger 请求的标头,以便可以测试具有 [Authorize] 属性的路由?

                  Is it possible to modify the header of the Swagger requests so the routes with the [Authorize] attribute can be tested?

                  推荐答案

                  我遇到了同样的问题,并在这篇博文中找到了一个可行的解决方案:http://blog.sluijsveld.com/28/01/2016/CustomSwaggerUIField

                  I struggled with the same problem and found a working solution in this blogpost: http://blog.sluijsveld.com/28/01/2016/CustomSwaggerUIField

                  归结为在您的配置选项中添加它

                  It comes down to adding this in your configurationoptions

                  services.ConfigureSwaggerGen(options =>
                  {
                     options.OperationFilter<AuthorizationHeaderParameterOperationFilter>();
                  });
                  

                  以及操作过滤器的代码

                  public class AuthorizationHeaderParameterOperationFilter : IOperationFilter
                  {
                     public void Apply(Operation operation, OperationFilterContext context)
                     {
                        var filterPipeline = context.ApiDescription.ActionDescriptor.FilterDescriptors;
                        var isAuthorized = filterPipeline.Select(filterInfo => filterInfo.Filter).Any(filter => filter is AuthorizeFilter);
                        var allowAnonymous = filterPipeline.Select(filterInfo => filterInfo.Filter).Any(filter => filter is IAllowAnonymousFilter);
                  
                        if (isAuthorized && !allowAnonymous)
                        {
                            if (operation.Parameters == null)
                               operation.Parameters = new List<IParameter>();
                  
                            operation.Parameters.Add(new NonBodyParameter
                            {                    
                               Name = "Authorization",
                               In = "header",
                               Description = "access token",
                               Required = true,
                               Type = "string"
                           });
                        }
                     }
                  }
                  

                  然后你会在你的 swagger 中看到一个额外的 Authorization TextBox,你可以在其中以Bearer {jwttoken}"格式添加你的令牌,并且你应该在你的 swagger 请求中获得授权.

                  Then you will see an extra Authorization TextBox in your swagger where you can add your token in the format 'Bearer {jwttoken}' and you should be authorized in your swagger requests.

                  这篇关于在 ASP.NET Core 的 Swagger 中使用 JWT(授权:Bearer)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:Identity Server 4:向访问令牌添加声明 下一篇:如何使用我自己的 rsa 私钥使用 rs256 算法对字节

                  相关文章

                  最新文章

                  <tfoot id='6zPfI'></tfoot>

                  • <bdo id='6zPfI'></bdo><ul id='6zPfI'></ul>

                • <legend id='6zPfI'><style id='6zPfI'><dir id='6zPfI'><q id='6zPfI'></q></dir></style></legend>
                • <small id='6zPfI'></small><noframes id='6zPfI'>

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