Asp.Net Core webApi Swagger configuration description

Keywords: Web Development xml

outline
You can find the configuration information to enable Swagger Gen on the official website, which is also the beginning of our rapid development of Api
So we don't need to write API documents and tests. It's ideal to be full, but some of our Get interfaces can be used when using, but
We need to do authentication when we Post. Token is a good choice. So is it. I learn Swagger, but I'm not good at it
How to open identity authentication

Get to the point

I pasted all the codes I used: one by one:

Swagger registration:

        services.AddSwaggerGen(c =>
        {
            c.OperationFilter<HttpAuthHeaderFilter>();
            var security = new Dictionary<string, IEnumerable<string>> { { "Bearer", new string[] { } }, };
            c.AddSecurityRequirement(security);
            //Add a required global security information
            //, and the scheme name specified by AddSecurityDefinition method should be consistent,
            //This is Bearer.
            c.DescribeStringEnumsInCamelCase();
            c.DescribeAllParametersInCamelCase();
            c.DescribeAllEnumsAsStrings();
            c.SwaggerDoc("v1", new Swashbuckle.AspNetCore.Swagger.Info
            {
                Title = "Interface document",
                Version = "v1",
                Description = "YiSpace  LazyCoder is Make",
            });

            string[] files = Directory.GetFiles(AppContext.BaseDirectory, "*.xml");
            //  var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
            //  var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
            foreach (var item in files)
            {
                c.IncludeXmlComments(item, true);
            }
            //var xmlPath = Path.Combine(AppContext.BaseDirectory, "");
            //c.IncludeXmlComments(xmlPath);
            //var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
            //c.IncludeXmlComments(xmlPath);
            // c.IgnoreObsoleteActions();
            c.AddSecurityDefinition("Bearer", new ApiKeyScheme
            {
                Description = "Permission authentication(Data will be transferred in the request header) Parameter structure: \"Authorization: Bearer {token}\"",
                Name = "Authorization",//jwt default parameter name
                In = "header",//The default location of Authorization information in jwt (in the request header)
                Type = "apiKey"
            });//Setting of Authorization

        });

•  c.OperationFilter<HttpAuthHeaderFilter>(); It's just a custom one Paramater Filter.Users add information for Oh universal,For specific Filter For adding information

• var security = new Dictionary<string, IEnumerable<string>> { { "Bearer", new string[] { } }, };
                c.AddSecurityRequirement(security);
                //, and the scheme name specified by AddSecurityDefinition method should be consistent,
                //This is Bearer. That's why we saw it later, Authore.Lock. Because
//We are developing API, some must have certification, so I added the global registration
• These are the configuration of type conversion 
c.DescribeStringEnumsInCamelCase();
         c.DescribeAllParametersInCamelCase();
         c.DescribeAllEnumsAsStrings();
•   You don't have to talk about the middle one Asp.net Core It's on the official website
•  
 c.AddSecurityDefinition("Bearer", new ApiKeyScheme
                {
                    Description = "Permission authentication(Data will be transferred in the request header) Parameter structure: \"Authorization: Bearer {token}\"",
                    Name = "Authorization",//jwt default parameter name
                    In = "header",//The default location of Authorization information in jwt (in the request header)
                    Type = "apiKey"
                });//Setting of Authorization
//This is the core because with him there will be customizable UI. No one will see it first
• At the same time, I'm not alone api Add to Token switch. I don't know how to add 

//In this way, our things can be used, but it should be noted that JWT Bearer is used here. When it is submitted, it does not add Bearer to you, so Authorization: Bearer {token} needs to add Bearer itself

 //continue
//After looking at the code, I got some inspiration to realize the token authentication of a single Action, which we need to use. IOperationFilter

/// <summary>
///swagger adds AUTH option
/// </summary>
public class HttpAuthHeaderFilter : IOperationFilter
{        public void Apply(Operation operation, OperationFilterContext context)
    {            var HasAuth = context.ApiDescription.ActionDescriptor.FilterDescriptors.Any(t => t.Filter is Microsoft.AspNetCore.Mvc.Authorization.AuthorizeFilter);
        if (HasAuth)
        {
            //operation.Security =new  
            var security = new Dictionary<string, IEnumerable<string>> { { "Bearer", new string[] { } }, };
            var ls = new List<IDictionary<string, IEnumerable<string>>>();
            ls.Add(security);
            operation.Security = ls;               
        }
      }
}
   operation.Security = ls;          Is the information configuration of security authentication,I didn't find it. api doc ,

With him
We also need to:
c.AddSecurityRequirement(security);
Annotated, because he registered for global identity
I'm lazy and easy to understand the code here. I didn't prevent the configuration information list from being global, but there was no big problem. After all, I used it once

Posted by jainsy on Wed, 19 Feb 2020 10:21:01 -0800