68 lines
2.6 KiB
C#
68 lines
2.6 KiB
C#
using Mapster;
|
|
using MapsterMapper;
|
|
using Newtonsoft.Json.Converters;
|
|
using Newtonsoft.Json.Serialization;
|
|
using Newtonsoft.Json;
|
|
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
|
using Microsoft.IdentityModel.Tokens;
|
|
using System.Text;
|
|
using System.Reflection.Metadata;
|
|
using Microsoft.OpenApi.Models;
|
|
|
|
namespace WGShare.API.ServiceConfigs
|
|
{
|
|
public static class SwaggerServiceExtensions
|
|
{
|
|
/// <summary>
|
|
/// 添加认证和授权
|
|
/// </summary>
|
|
/// <param name="services">服务集合</param>
|
|
/// <returns></returns>
|
|
public static IServiceCollection AddSwagger(this IServiceCollection services)
|
|
{
|
|
services.AddSwaggerGen(w =>
|
|
{
|
|
w.SwaggerDoc("frontend", new OpenApiInfo { Title = "前端", Version = "frontend" });
|
|
w.SwaggerDoc("backend", new OpenApiInfo { Title = "后端", Version = "backend" });
|
|
w.SwaggerDoc("public", new OpenApiInfo { Title = "公共接口", Version = "public" });
|
|
w.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme
|
|
{
|
|
Description = "JWT Authorization header using the Bearer scheme. Example: \"Authorization: Bearer {token}\"",
|
|
Name = "Authorization",
|
|
In = ParameterLocation.Header,
|
|
Type = SecuritySchemeType.ApiKey,
|
|
Scheme = "Bearer"
|
|
});
|
|
|
|
w.AddSecurityRequirement(new OpenApiSecurityRequirement
|
|
{
|
|
{
|
|
new OpenApiSecurityScheme
|
|
{
|
|
Reference = new OpenApiReference
|
|
{
|
|
Type = ReferenceType.SecurityScheme,
|
|
Id = "Bearer"
|
|
}
|
|
},
|
|
new string[] {}
|
|
}
|
|
});
|
|
|
|
//string xmlPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "SwaggerGroup.xml");
|
|
//w.IncludeXmlComments(xmlPath, true);
|
|
});
|
|
|
|
|
|
return services;
|
|
//services.AddAuthorization(options =>
|
|
//{
|
|
// options.AddPolicy(Constant.Policy.FreePolicyName,
|
|
// policy => policy.RequireClaim(Constant.Auth.PermissionsKey, Constant.Auth.FreeClaimValue, Constant.Auth.VipClaimValue));
|
|
// options.AddPolicy(Constant.Policy.VipPolicyName,
|
|
// policy => policy.RequireClaim(Constant.Auth.PermissionsKey, Constant.Auth.VipClaimValue));
|
|
//});
|
|
}
|
|
}
|
|
}
|