是否可以大摇大摆地更改 api_key 的名称?

Is it possible to change the name of api_key in swagger?

我正在尝试构建 asp.net mvc web api 并且还尝试在第一个 time.In 中使用 swagger 我所有的请求都有一个名为 [=16 的参数=] 所以使用 swagger 我在 swagger 页面的右上角输入一个键并在测试该方法之前按下探索并且我还取消注释 swagger.config 文件中的代码,如下所示。

 c.ApiKey("auth")
 .Description("API Key Authentication")
 .Name("auth")
 .In("query");

但是还是swagger把参数名取成了'api_key'。如何把'api_key'改成'auth' swagger呢?

我不知道如何直接更改 api_key,但您可以使用 UI js 文件以您喜欢的方式完全操作它。我已经回答了类似的问题here

1) 添加js文件:

c.InjectJavaScript(thisAssembly, "SwashbuckleCustomAuth.CustomContent.basic-auth.js"); 

2) 这是将连接到令牌服务器的 JS 示例,通过 username/password 获取 API 密钥,然后将其添加为 header。

$('#explore').off();

$('#explore').click(function () {
   var key = $('#input_apiKey')[0].value;
   var credentials = key.split(':'); //username:password expected

$.ajax({
    url: "yourAuthEndpoint",
    type: "post",
    contenttype: 'x-www-form-urlencoded',
    data: "grant_type=password&username=" + credentials[0] + "&password=" + credentials[1],
    success: function (response) {
        var bearerToken = 'Bearer ' + response.access_token;

        window.swaggerUi.api.clientAuthorizations.add('Authorization', new SwaggerClient.ApiKeyAuthorization('Authorization', bearerToken, 'header'));
        window.swaggerUi.api.clientAuthorizations.remove("api_key");
        alert("Login successfull");
       },
       error: function (xhr, ajaxoptions, thrownerror) {
        alert("Login failed!");
       }
    });
});

所以我想你也可以使用更简单的js方式来更改参数的名称。

这不是简单的名称更改,但如果无法通过简单的方法(当然仍然可能),您可以使用该方法。

I found the solution here at the end

解决方案是 index.html

中以下功能的更改
function addApiKeyAuthorization() {
        var key = encodeURIComponent($('#input_apiKey')[0].value);
        if (key && key.trim() != "") {
            var apiKeyAuth = new SwaggerClient.ApiKeyAuthorization("NAME OF PARAMETER", key, "query");
            window.swaggerUi.api.clientAuthorizations.add("api_key", apiKeyAuth);
            log("added key " + key);
        }
    }

为了使用 swagger:2.0 的人的利益。 这应该在 swagger.json 文件中设置。

{
  "swagger": "2.0",
  "info": {
    "description": "API Documentation",
    "version": "1.0.2",
    "title": "my super service"
  },
  "basePath": "/my-super-services/api",
  "host": "example.com",
  "schemes": [
    "https",
    "http"
  ],
  "securityDefinitions": {
    "UserSecurity": {
      "type": "apiKey",
      "in": "header",       <----- set this to header or cookie or query
      "name": "X-Api-Key"   <----- you custom key goes here
    }
  },