Search code examples
c#asp.net-mvcswaggerswagger-uiswagger-net

In Swagger.Net UI, how can I remove the padlock icon from “Anonymous” methods?


I found a similar thread here but that is for .Net Core. I have the same issue with Swagger.Net API(.Net Framework).

I am using Swagger.Net API version 8.3.35.101 with .Net framework 4.6.1 and I am getting a lock icon for each and every method in the Swagger UI. Some of the API's in my application doesn't need authentication, therefore I want to remove the padlock icon from such API's.

enter image description here

The padlock icon which needs to be removed can be seen in the above image highlighted in red. I believe hiding it for Anonymous methods can be achieved by implementing IOperationFilter but couldn't find any sample code to achieve it with .Net framework.


Solution

  • Here is the answer from what we discussed in the comments:

    The issues is the incorrect type on the config of ApiKey here is how it should be:

    c.ApiKey("apiKey", "header", "API Key Authentication", typeof(KeyAuthorizeAttribute));
    c.ApiKey("appId", "header", "APP ID Authentication", typeof(KeyAuthorizeAttribute));
    

    That code on github:
    https://github.com/heldersepu/nhc-noaa/blob/master/nhc-noaa/App_Start/SwaggerConfig.cs#L75

    And a live sample:
    http://nhc-noaa.azurewebsites.net/swagger/ui/index?docExpansion=list&filter=#/

    The padlock icon is only on one of them enter image description here