0

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.

prem
  • 3,348
  • 1
  • 25
  • 57
  • No, it's not duplicate. I have already given that link in my question but that is for .Net core and I am using Swagger.Net which is for .Net framework. – prem Dec 23 '20 at 04:55
  • Can you provide some sample code, it works fine for me: http://nhc-noaa.azurewebsites.net/swagger/ui/index?docExpansion=list&filter=#/Videos – Helder Sepulveda Dec 28 '20 at 18:25
  • The code which I tried didn't work as that is for .Net Core and IOperationFilter is different in Swagger.Net; therefore, I was not even able to compile it. [Here](https://stackoverflow.com/a/60340913/3085520) is the link which I tried. Could you please share the sample code of the link you have provided in your comment? I think that should work for me. – prem Dec 29 '20 at 08:15
  • I meant a sample code reproducing your issue, a complete project would be ideal ... here is the code for my project: https://github.com/heldersepu/nhc-noaa there I set the auth using `ApiKey` https://github.com/heldersepu/nhc-noaa/blob/master/nhc-noaa/App_Start/SwaggerConfig.cs#L75 – Helder Sepulveda Dec 29 '20 at 14:03
  • I would love to get to the bottom of why are you getting the padlock icon on all ... can you create a project on GitHub reproducing your issue(s) and post a link here? – Helder Sepulveda Dec 30 '20 at 19:49
  • Apologies for the delayed response, I was on leave owing to new year holidays. I will check your project today and will certainly provide you a sample if that doesn't work for me. – prem Jan 04 '21 at 09:39
  • Again apologies for the delayed reply Helder. I was shifter to another project so couldn't get time to dig into this issue. I have seen your project and I am able to find out the root cause of this issue. The problem is that, I didn't pass the type of AuthorizationFilterAttribute while setting .ApiKey in SwaggerConfig file. Now I have changed my code to **c.ApiKey("ApiKey", "header", "API Key Authentication", typeof(API_Bearer_Session_Authorization_Filter));**. Without the typeof() statement, you will get the padlock icon on each and every api. Please post it as answer. Thanks for your help. – prem Jan 27 '21 at 10:25

1 Answers1

1

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

Helder Sepulveda
  • 15,500
  • 4
  • 29
  • 56