ibms-dome/FrontendWebApi/Startup.cs

188 lines
7.9 KiB
C#
Raw Normal View History

2022-10-14 16:08:54 +08:00
using FrontendWebApi.Jwt;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.IdentityModel.Logging;
using Microsoft.IdentityModel.Tokens;
using Repository.BackendRepository.Implement;
using Repository.BackendRepository.Interface;
using Repository.BaseRepository.Implement;
using Repository.BaseRepository.Interface;
using Repository.FrontendRepository.Implement;
using Repository.FrontendRepository.Interface;
using Repository.Models;
using Repository.Services.Implement;
using System;
using System.Collections.Generic;
using System.IdentityModel.Tokens.Jwt;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static FrontendWebApi.Jwt.JwtHelpers;
namespace FrontendWebApi
{
public class Startup
{
public DBConfig dBConfig = new DBConfig()
{
MSSqlDBConfig = new MSSqlDBConfig(),
MySqlDBConfig = new MySqlDBConfig()
};
public Startup(IConfiguration configuration)
{
Configuration = configuration;
dBConfig.MSSqlDBConfig.Server = Configuration.GetValue<string>("DBConfig:MSSqlDBConfig:Server");
dBConfig.MSSqlDBConfig.Port = Configuration.GetValue<string>("DBConfig:MSSqlDBConfig:Port");
dBConfig.MSSqlDBConfig.Root = Configuration.GetValue<string>("DBConfig:MSSqlDBConfig:Root");
dBConfig.MSSqlDBConfig.Password = Configuration.GetValue<string>("DBConfig:MSSqlDBConfig:Password");
dBConfig.MSSqlDBConfig.Database = Configuration.GetValue<string>("DBConfig:MSSqlDBConfig:Database");
dBConfig.MySqlDBConfig.Server = Configuration.GetValue<string>("DBConfig:MySqlDBConfig:Server");
dBConfig.MySqlDBConfig.Port = Configuration.GetValue<string>("DBConfig:MySqlDBConfig:Port");
dBConfig.MySqlDBConfig.Root = Configuration.GetValue<string>("DBConfig:MySqlDBConfig:Root");
dBConfig.MySqlDBConfig.Password = Configuration.GetValue<string>("DBConfig:MySqlDBConfig:Password");
dBConfig.MySqlDBConfig.Database = Configuration.GetValue<string>("DBConfig:MySqlDBConfig:Database");
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddSingleton<JwtHelpers>();
2022-10-14 16:08:54 +08:00
services.AddControllersWithViews();
//services.AddControllers();
services.AddCors(options =>
{
options.AddPolicy("CorsPolicy", policy =>
{
policy.AllowAnyOrigin()
.AllowAnyHeader()
.AllowAnyMethod()
.WithExposedHeaders("Content-Disposition");
2022-10-14 16:08:54 +08:00
});
});
services.AddLogging(
builder =>
{
builder.AddFilter("Microsoft", LogLevel.Warning)
.AddFilter("System", LogLevel.Warning)
.AddFilter("NToastNotify", LogLevel.Warning)
.AddConsole();
});
#region DBHelper <EFBFBD>`<EFBFBD>J
services.Configure<DBConfig>(Configuration.GetSection("DBConfig"));
services.AddTransient<Repository.Helper.IDatabaseHelper, Repository.Helper.DatabaseHelper>();
#endregion DBHelper <EFBFBD>`<EFBFBD>J
services.Configure<ObixApiConfig>(Configuration.GetSection("ObixApiConfig"));
#region Repository <EFBFBD>`<EFBFBD>J
services.AddTransient<IBackendRepository, BackendRepository>();
services.AddTransient<IFrontendRepository, FrontendRepository>();
services.AddTransient<IBaseRepository, BaseRepository>();
services.AddTransient<IBackgroundServiceMsSqlRepository, BackgroundServiceMsSqlRepository>();
2022-10-14 16:08:54 +08:00
#endregion Repository <EFBFBD>`<EFBFBD>J
#region JWT <EFBFBD>`<EFBFBD>J
services.AddTransient<IJwtHelpers, JwtHelpers>();
//services.AddSingleton<JwtHelpers>();
services
.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(options =>
{
// <20><><EFBFBD><EFBFBD><EFBFBD>ҥ<EFBFBD><D2A5>ѮɡA<C9A1>^<5E><><EFBFBD><EFBFBD><EFBFBD>Y<EFBFBD>|<7C>]<5D>t WWW-Authenticate <20><><EFBFBD>Y<EFBFBD>A<EFBFBD>o<EFBFBD>̷|<7C><><EFBFBD>ܥ<EFBFBD><DCA5>Ѫ<EFBFBD><D1AA>Բӿ<D4B2><D3BF>~<7E><><EFBFBD>]
options.IncludeErrorDetails = true; // <20>w<EFBFBD>]<5D>Ȭ<EFBFBD> true<75>A<EFBFBD><41><EFBFBD>ɷ|<7C>S<EFBFBD>O<EFBFBD><4F><EFBFBD><EFBFBD>
options.TokenValidationParameters = new TokenValidationParameters
2022-10-14 16:08:54 +08:00
{
// <20>z<EFBFBD>L<EFBFBD>o<EFBFBD><6F><EFBFBD>ŧi<C5A7>A<EFBFBD>N<EFBFBD>i<EFBFBD>H<EFBFBD>q "sub" <20><><EFBFBD>Ȩó]<5D>w<EFBFBD><77> User.Identity.Name
NameClaimType = "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier",
// <20>z<EFBFBD>L<EFBFBD>o<EFBFBD><6F><EFBFBD>ŧi<C5A7>A<EFBFBD>N<EFBFBD>i<EFBFBD>H<EFBFBD>q "roles" <20><><EFBFBD>ȡA<C8A1>åi<C3A5><69> [Authorize] <20>P<EFBFBD>_<EFBFBD><5F><EFBFBD><EFBFBD>
RoleClaimType = "http://schemas.microsoft.com/ws/2008/06/identity/claims/role",
// <20>@<40><><EFBFBD>ڭ̳<DAAD><CCB3>|<7C><><EFBFBD><EFBFBD> Issuer
ValidateIssuer = true,
ValidIssuer = Configuration.GetValue<string>("JwtSettings:Issuer"),
RequireExpirationTime = true,
// <20>q<EFBFBD>`<60><><EFBFBD>ӻݭn<DDAD><6E><EFBFBD><EFBFBD> Audience
ValidateAudience = false,
//ValidAudience = "JwtAuthDemo", // <20><><EFBFBD><EFBFBD><EFBFBD>ҴN<D2B4><4E><EFBFBD>ݭn<DDAD><6E><EFBFBD>g
// <20>@<40><><EFBFBD>ڭ̳<DAAD><CCB3>|<7C><><EFBFBD><EFBFBD> Token <20><><EFBFBD><EFBFBD><EFBFBD>Ĵ<EFBFBD><C4B4><EFBFBD>
ValidateLifetime = true,
// <20>p<EFBFBD>G Token <20><><EFBFBD>]<5D>t key <20>~<7E>ݭn<DDAD><6E><EFBFBD>ҡA<D2A1>@<40><EFBFBD>u<EFBFBD><75>ñ<EFBFBD><C3B1><EFBFBD>Ӥw
ValidateIssuerSigningKey = false,
// "1234567890123456" <20><><EFBFBD>ӱq IConfiguration <20><><EFBFBD>o
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(Configuration.GetValue<string>("JwtSettings:SignKey")))
};
});
#endregion JWT <EFBFBD>`<EFBFBD>J
double loginExpireMinute = this.Configuration.GetValue<double>("LoginExpireMinute");
services.AddSession(options =>
{
options.Cookie.Name = "WebApi.Session";
options.IdleTimeout = TimeSpan.FromMinutes(loginExpireMinute);
});
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILoggerFactory loggerFactory)
{
loggerFactory.AddFile("Logs/log-{Date}.txt");
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseSession();
app.UseHttpsRedirection();
app.UseStaticFiles();
//IdentityModelEventSource.ShowPII = true;
app.UseRouting();
JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Clear();
app.UseCors(x => x
.AllowAnyMethod()
.AllowAnyHeader()
.SetIsOriginAllowed(origin => true) // allow any origin
.AllowCredentials());
app.UseAuthentication();
app.UseAuthorization();
//app.UseEndpoints(endpoints =>
//{
// endpoints.MapControllers();
//});
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Login}/{action=Index}/{id?}");
});
}
}
}