forked from ddrilling/AsbCloudServer
83 lines
3.3 KiB
C#
83 lines
3.3 KiB
C#
using AsbCloudInfrastructure.Services;
|
|
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
|
using Microsoft.AspNetCore.Mvc.ApiExplorer;
|
|
using Microsoft.Extensions.Configuration;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Microsoft.IdentityModel.Tokens;
|
|
using Microsoft.OpenApi.Models;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Reflection;
|
|
|
|
namespace AsbCloudWebApi
|
|
{
|
|
public static class DependencyInjection
|
|
{
|
|
public static void AddSwagger(this IServiceCollection services, IConfiguration configuration = null)
|
|
{
|
|
services.AddSwaggerGen(c =>
|
|
{
|
|
//c.CustomOperationIds(e => $"{e.ActionDescriptor.RouteValues["controller"]}_{e.HttpMethod}");
|
|
//c.CustomOperationIds(e => $"{e.HttpMethod}_{e.ActionDescriptor.Ac");
|
|
c.CustomOperationIds(e=> {
|
|
return $"{e.ActionDescriptor.RouteValues["action"]}";
|
|
});
|
|
|
|
c.SwaggerDoc("v1", new OpenApiInfo { Title = "Charging station", Version = "v1" });
|
|
c.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme
|
|
{
|
|
Description = @"JWT Authorization header using the Bearer scheme. Enter 'Bearer' [space] and then your token in the text input below. Example: 'Bearer 12345abcdef'",
|
|
Name = "Authorization",
|
|
In = ParameterLocation.Header,
|
|
Type = SecuritySchemeType.ApiKey,
|
|
Scheme = "Bearer"
|
|
});
|
|
|
|
c.AddSecurityRequirement(new OpenApiSecurityRequirement()
|
|
{
|
|
{
|
|
new OpenApiSecurityScheme
|
|
{
|
|
Reference = new OpenApiReference
|
|
{
|
|
Type = ReferenceType.SecurityScheme,
|
|
Id = "Bearer"
|
|
},
|
|
Scheme = "oauth2",
|
|
Name = "Bearer",
|
|
In = ParameterLocation.Header,
|
|
|
|
},
|
|
new List<string>()
|
|
}
|
|
});
|
|
|
|
var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
|
|
var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
|
|
c.IncludeXmlComments(xmlPath);
|
|
|
|
});
|
|
}
|
|
|
|
public static void AddJWTAuthentication(this IServiceCollection services, IConfiguration configuration = null)
|
|
{
|
|
services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
|
|
.AddJwtBearer(options =>
|
|
{
|
|
options.RequireHttpsMetadata = false;
|
|
options.TokenValidationParameters = new TokenValidationParameters
|
|
{
|
|
ValidateIssuer = true,
|
|
ValidIssuer = AuthService.issuer,
|
|
ValidateAudience = true,
|
|
ValidAudience = AuthService.audience,
|
|
ValidateLifetime = true,
|
|
IssuerSigningKey = AuthService.securityKey,
|
|
ValidateIssuerSigningKey = true,
|
|
};
|
|
});
|
|
}
|
|
}
|
|
}
|