Search code examples
authenticationasp.net-web-apiswaggeraccess-tokenswagger-ui

How to pass access token in url header of swagger ui using asp.net web api?


I have a swagger API in asp.net. I am using token based authentication in my project. I get the token in response by a post request in tokenendpointpath [/token].Now i need to pass that token automatically with every request curl Authorization header. I tried in many ways but its not working.I don't want the user to every time input that token when request. Please kindly help me to get rid of this issue.

//request for token

enter image description here

//request for resource

enter image description here

//Swagger.config class

public class SwaggerConfig
{

    public static void Register()
    {
        var thisAssembly = typeof(SwaggerConfig).Assembly;

        GlobalConfiguration.Configuration
            .EnableSwagger(c =>
                {
                    c.DocumentFilter<AuthTokenOperation>();
                    c.SingleApiVersion("v1", "API");
                    c.PrettyPrint();
                })
            .EnableSwaggerUi(c =>
                {

                    c.DocumentTitle("MY API");

                });
    }
}

Thanx in advance


Solution

  • Update to web api .net framework

    .EnableSwagger(c =>
    {
        c.DocumentFilter<AuthTokenOperation>();
        c.SingleApiVersion("v1", "API");
        c.PrettyPrint();
    
         c.ApiKey("apiKey")
            .Description("API Key for accessing secure APIs")
            .Name("Api-Key")
            .In("header");                  
    })