|
|
|
|
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
|
|
|
|
using Microsoft.AspNetCore.Builder;
|
|
|
|
|
using Microsoft.AspNetCore.Hosting;
|
|
|
|
|
using Microsoft.Extensions.Configuration;
|
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
|
using Microsoft.Extensions.Hosting;
|
|
|
|
|
using Microsoft.IdentityModel.Tokens;
|
|
|
|
|
using Microsoft.OpenApi.Models;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Reflection;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using PEIS.Common.Helper.Encryption;
|
|
|
|
|
using PEIS.Common.Middleware;
|
|
|
|
|
|
|
|
|
|
namespace PEIS.Interface
|
|
|
|
|
{
|
|
|
|
|
public class Startup
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
public Startup(IConfiguration configuration)
|
|
|
|
|
{
|
|
|
|
|
Configuration = configuration;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private readonly bool _swagger = AppSettingJsonHelper.GetSection("Swagger", "Using") == "true";
|
|
|
|
|
public IConfiguration Configuration { get; }
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD><EFBFBD>á<EFBFBD> ʹ<EFBFBD>ô˷<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӷ<EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="services"></param>
|
|
|
|
|
public void ConfigureServices(IServiceCollection services)
|
|
|
|
|
{
|
|
|
|
|
services.AddControllers();
|
|
|
|
|
services.AddControllersWithViews()
|
|
|
|
|
.AddNewtonsoftJson(options =>
|
|
|
|
|
options.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore
|
|
|
|
|
);
|
|
|
|
|
// swagger <EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
if (_swagger)
|
|
|
|
|
{
|
|
|
|
|
services.AddSwaggerGen(c =>
|
|
|
|
|
{
|
|
|
|
|
c.SwaggerDoc("v1", new OpenApiInfo { Title = "OutCollect", Version = "v1" });
|
|
|
|
|
var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
|
|
|
|
|
var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
|
|
|
|
|
c.IncludeXmlComments(xmlPath, true);
|
|
|
|
|
|
|
|
|
|
//<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Authorization
|
|
|
|
|
c.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme
|
|
|
|
|
{
|
|
|
|
|
Description = "JWT Authorization header using the Bearer scheme.",
|
|
|
|
|
Name = "Authorization",
|
|
|
|
|
In = ParameterLocation.Header,
|
|
|
|
|
Scheme = "bearer",
|
|
|
|
|
Type = SecuritySchemeType.Http,
|
|
|
|
|
BearerFormat = "JWT"
|
|
|
|
|
});
|
|
|
|
|
//<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>з<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>bearerͷ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ;
|
|
|
|
|
c.AddSecurityRequirement(new OpenApiSecurityRequirement
|
|
|
|
|
{
|
|
|
|
|
{
|
|
|
|
|
new OpenApiSecurityScheme
|
|
|
|
|
{
|
|
|
|
|
Reference = new OpenApiReference
|
|
|
|
|
{
|
|
|
|
|
Type = ReferenceType.SecurityScheme,
|
|
|
|
|
Id = "bearerAuth"
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
new string[] {}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
c.AddSecurityRequirement(new OpenApiSecurityRequirement
|
|
|
|
|
{
|
|
|
|
|
{
|
|
|
|
|
new OpenApiSecurityScheme
|
|
|
|
|
{
|
|
|
|
|
Reference = new OpenApiReference { Type = ReferenceType.SecurityScheme, Id = "Bearer" }
|
|
|
|
|
},
|
|
|
|
|
new List<string>()
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
// jwt <EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
services.AddAuthentication(options =>
|
|
|
|
|
{
|
|
|
|
|
// <EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ĭ<EFBFBD><EFBFBD>ʹ<EFBFBD><EFBFBD>jwt<EFBFBD><EFBFBD>֤<EFBFBD><EFBFBD>ʽ
|
|
|
|
|
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
|
|
|
|
|
options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
|
|
|
|
|
}).AddJwtBearer(options =>
|
|
|
|
|
{
|
|
|
|
|
var confSection = Configuration.GetSection("Authentication");
|
|
|
|
|
options.TokenValidationParameters = new TokenValidationParameters()
|
|
|
|
|
{
|
|
|
|
|
// <EFBFBD><EFBFBD>֤<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
ValidateAudience = true,
|
|
|
|
|
// <EFBFBD><EFBFBD>֤<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> //<EFBFBD>Ƿ<EFBFBD><EFBFBD><EFBFBD>֤<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ˣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>֤<EFBFBD>غ<EFBFBD><EFBFBD>е<EFBFBD>Iss<EFBFBD>Ƿ<EFBFBD><EFBFBD><EFBFBD>ӦValidIssuer<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
ValidateIssuer = true,
|
|
|
|
|
// <EFBFBD><EFBFBD>֤<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD>//<EFBFBD>Ƿ<EFBFBD><EFBFBD><EFBFBD>֤<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD>䣬<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˾;ܾ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
ValidateLifetime = true,
|
|
|
|
|
// <EFBFBD><EFBFBD>֤<EFBFBD><EFBFBD>Կ //<EFBFBD>Ƿ<EFBFBD><EFBFBD><EFBFBD>֤ǩ<EFBFBD><EFBFBD>,<EFBFBD><EFBFBD><EFBFBD><EFBFBD>֤<EFBFBD>Ļ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Դ۸<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȫ
|
|
|
|
|
ValidateIssuerSigningKey = true,
|
|
|
|
|
// <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Issuer//<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
ValidIssuer = confSection["IsSure"],
|
|
|
|
|
// <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Audience//<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
ValidAudience = confSection["Audience"],
|
|
|
|
|
// <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>token<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Կ //<EFBFBD><EFBFBD><EFBFBD>ܵ<EFBFBD><EFBFBD><EFBFBD>Կ
|
|
|
|
|
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(confSection["SecurityKey"]))
|
|
|
|
|
};
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// <EFBFBD>˷<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD><EFBFBD>á<EFBFBD> ʹ<EFBFBD>ô˷<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> HTTP <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ܵ<EFBFBD>
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="app"></param>
|
|
|
|
|
/// <param name="env"></param>
|
|
|
|
|
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
|
|
|
|
|
{
|
|
|
|
|
if (env.IsDevelopment())
|
|
|
|
|
{
|
|
|
|
|
app.UseDeveloperExceptionPage();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
app.UseExceptionHandler("/error");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (_swagger)
|
|
|
|
|
{
|
|
|
|
|
app.UseSwagger();
|
|
|
|
|
// Enable middleware to serve generated Swagger as a JSON endpoint.
|
|
|
|
|
app.UseSwagger(c =>
|
|
|
|
|
{
|
|
|
|
|
c.SerializeAsV2 = true;
|
|
|
|
|
});
|
|
|
|
|
app.UseSwaggerUI(c =>
|
|
|
|
|
{
|
|
|
|
|
c.SwaggerEndpoint("/swagger/v1/swagger.json", "debug NetFL v1");
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
app.UseHttpsRedirection();
|
|
|
|
|
app.UseRouting();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// custom jwt auth middleware
|
|
|
|
|
app.UseMiddleware<JwtMiddleware>();
|
|
|
|
|
// jwt
|
|
|
|
|
app.UseAuthentication();
|
|
|
|
|
app.UseAuthorization();
|
|
|
|
|
|
|
|
|
|
app.UseEndpoints(endpoints =>
|
|
|
|
|
{
|
|
|
|
|
endpoints.MapControllers();
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|