相关疑难解决方法(0)

使用授权标头(Bearer)设置Swagger(ASP.NET Core)

我有一个Web API(ASP.NET核心),我正在尝试调整swagger来进行调用.这些调用必须包含Authorization标头,我正在使用Bearer身份验证.来自Postman等第三方应用的电话也没问题.但我遇到了设置swagger标头的问题(由于某种原因我没有收到标头).这就是现在的样子:

  "host": "localhost:50352",
  "basePath": "/" ,
  "schemes": [
    "http",
    "https"
  ],
 "securityDefinitions":  {
    "Bearer": {
      "name": "Authorization",
      "in": "header",
      "type": "apiKey",
      "description": "HTTP/HTTPS Bearer"
    }
  },
  "paths": { 
    "/v1/{subAccountId}/test1": {
      "post": {
        "tags": [
          "auth"
        ],
        "operationId": "op1",
        "consumes": ["application/json", "application/html"],
        "produces": ["application/json", "application/html"],
        "parameters": [
          {
            "name": "subAccountId",
            "in": "path",
            "required": true,
            "type": "string"
          }
        ],
        "security":[{
          "Bearer": []
        }],
        "responses": {
          "204": {
            "description": "No Content"
          },
          "400": {
            "description": "BadRequest",
            "schema": {
              "$ref": "#/definitions/ErrorResponse"
            }
          }, …
Run Code Online (Sandbox Code Playgroud)

c# swagger-ui asp.net-core

45
推荐指数
7
解决办法
4万
查看次数

在浏览器刷新或计算机重新启动后永久存储 Swagger UI 令牌

有没有什么方法可以在本地计算机或开发环境中将令牌永久硬编码到 Swagger UI 中?

我们正在测试 Net Core API,并通过 Angular 8 运行它们:开发、重建、编写代码、每天测试 Swagger API 超过 100 次。想办法,把token存储在下面,这样就不用一直Reentering了。随着时间的推移,可能会非常麻烦、耗时。

也许我们可以从开发人员桌面的外部文件中读取令牌。即使计算机重新启动后,令牌仍会保留,开发人员无需重新输入令牌。也许在 appsettings.json 或任何文件中?

或者无论如何,使用 Net Core Visual Studio 环境注入代码,不会在源代码管理中公开令牌?

答案应该运行从 Angular 环境运行的所有 Swagger UI 和 API,

仅在 QA 和生产中需要输入令牌

在此输入图像描述

使用 Angular 和 Net Core 2.0 C#,

c# swagger .net-core asp.net-core angular

9
推荐指数
2
解决办法
6327
查看次数

在 Asp.Net Core 中使用 Swagger 未在请求中发送授权承载令牌

我正在使用 Swagger 在 Asp.Net Core 应用程序中测试我的 API。我通过输入这样的令牌来发送请求Authorization: Bearer {token}。但授权标头未在请求中发送。

Asp.Net Core 版本 3.1 和 Swashbuckle.AspNetCore 5.4.1

Startup.cs代码:

public class Startup
{
    private const string _apiVersion = "v1";
    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.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
            .AddJwtBearer(options =>
            {
                options.TokenValidationParameters = new TokenValidationParameters
                {
                    ClockSkew = TimeSpan.FromMinutes(0),
                    ValidateIssuer = true, …
Run Code Online (Sandbox Code Playgroud)

c# swagger swagger-ui swashbuckle asp.net-core

8
推荐指数
1
解决办法
6653
查看次数