2022-10-14 16:08:54 +08:00
|
|
|
|
using Microsoft.AspNetCore.Builder;
|
|
|
|
|
using Microsoft.AspNetCore.Hosting;
|
|
|
|
|
using Microsoft.AspNetCore.HttpsPolicy;
|
|
|
|
|
using Microsoft.Extensions.Configuration;
|
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
|
using Microsoft.Extensions.Hosting;
|
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
using Repository.Helper;
|
|
|
|
|
|
|
|
|
|
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 System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using Backend.Jwt;
|
|
|
|
|
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
|
|
|
|
using Microsoft.IdentityModel.Tokens;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.IdentityModel.Tokens.Jwt;
|
2022-11-17 10:49:49 +08:00
|
|
|
|
using Microsoft.AspNetCore.Http;
|
2022-10-14 16:08:54 +08:00
|
|
|
|
|
|
|
|
|
namespace Backend
|
|
|
|
|
{
|
|
|
|
|
public class Startup
|
|
|
|
|
{
|
|
|
|
|
public DBConfig dBConfig = new DBConfig()
|
|
|
|
|
{
|
|
|
|
|
MSSqlDBConfig = new MSSqlDBConfig(),
|
|
|
|
|
MySqlDBConfig = new MySqlDBConfig()
|
|
|
|
|
};
|
|
|
|
|
|
2023-03-02 14:24:34 +08:00
|
|
|
|
public BackEndConfig BackEndConfig = new BackEndConfig()
|
|
|
|
|
{
|
|
|
|
|
NiagaraDataSyncConfig=new NiagaraDataSyncConfig()
|
|
|
|
|
};
|
2022-10-14 16:08:54 +08:00
|
|
|
|
public Startup(IConfiguration configuration)
|
|
|
|
|
{
|
|
|
|
|
Configuration = configuration;
|
|
|
|
|
|
|
|
|
|
#region MSSql DB Config
|
|
|
|
|
//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");
|
|
|
|
|
#endregion MSSql DB Config
|
|
|
|
|
|
|
|
|
|
#region MySql DB Config
|
|
|
|
|
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");
|
|
|
|
|
#endregion MySql DB Config
|
|
|
|
|
|
2023-03-02 14:24:34 +08:00
|
|
|
|
#region NiagaraDataSync Config
|
|
|
|
|
BackEndConfig.NiagaraDataSyncConfig.UrlSlot = Configuration.GetValue<string>("BackEndConfig:NiagaraDataSyncConfig:UrlSlot");
|
|
|
|
|
BackEndConfig.NiagaraDataSyncConfig.ObixQuery= Configuration.GetValue<string>("BackEndConfig:NiagaraDataSyncConfig:ObixQuery");
|
|
|
|
|
BackEndConfig.NiagaraDataSyncConfig.ObixHisBqlQuery = Configuration.GetValue<string>("BackEndConfig:NiagaraDataSyncConfig:ObixHisBqlQuery");
|
|
|
|
|
BackEndConfig.NiagaraDataSyncConfig.ObixHisUrlQuery = Configuration.GetValue<string>("BackEndConfig:NiagaraDataSyncConfig:ObixHisUrlQuery");
|
|
|
|
|
#endregion
|
|
|
|
|
|
2022-10-14 16:08:54 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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.AddControllersWithViews();
|
|
|
|
|
|
|
|
|
|
services.AddCors(options =>
|
|
|
|
|
{
|
|
|
|
|
options.AddPolicy("CorsPolicy", policy =>
|
|
|
|
|
{
|
|
|
|
|
policy.AllowAnyOrigin()
|
|
|
|
|
.AllowAnyHeader()
|
|
|
|
|
.AllowAnyMethod();
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
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<IDatabaseHelper, DatabaseHelper>();
|
|
|
|
|
#endregion DBHelper <EFBFBD>`<EFBFBD>J
|
|
|
|
|
|
2023-03-02 14:24:34 +08:00
|
|
|
|
#region BackEndConfigHelper <EFBFBD>`<EFBFBD>J
|
|
|
|
|
services.Configure<BackEndConfig>(Configuration.GetSection("BackEndConfig"));
|
|
|
|
|
services.AddTransient<IBackEndConfigHelper, BackEndConfigHelper>();
|
|
|
|
|
#endregion DBHelper <EFBFBD>`<EFBFBD>J
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2022-10-14 16:08:54 +08:00
|
|
|
|
#region Repository <EFBFBD>`<EFBFBD>J
|
|
|
|
|
services.AddTransient<IBackendRepository, BackendRepository>();
|
|
|
|
|
services.AddTransient<IFrontendRepository, FrontendRepository>();
|
|
|
|
|
services.AddTransient<IBaseRepository, BaseRepository>();
|
|
|
|
|
services.AddTransient<IUserInfoRepository, UserInfoRepository>();
|
|
|
|
|
services.AddTransient<IDeviceManageRepository, DeviceManageRepository>();
|
|
|
|
|
services.AddTransient<IDeviceImportRepository, DeviceImportRepository>();
|
2022-11-02 17:26:18 +08:00
|
|
|
|
services.AddTransient<INiagaraDataSynchronizeRepository, NiagaraDataSynchronizeRepository>();
|
2022-10-14 16:08:54 +08:00
|
|
|
|
#endregion Repository <EFBFBD>`<EFBFBD>J
|
|
|
|
|
|
|
|
|
|
#region JWT <EFBFBD>`<EFBFBD>J
|
|
|
|
|
services.AddTransient<IJwtHelpers, 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()
|
|
|
|
|
{
|
|
|
|
|
// <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.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();
|
2022-11-17 10:49:49 +08:00
|
|
|
|
app.UseStaticFiles(new StaticFileOptions
|
|
|
|
|
{
|
|
|
|
|
OnPrepareResponse = ctx =>
|
|
|
|
|
{
|
|
|
|
|
ctx.Context.Response.Headers.Append("Access-Control-Allow-Origin", "*");
|
|
|
|
|
}
|
|
|
|
|
});
|
2022-10-14 16:08:54 +08:00
|
|
|
|
|
|
|
|
|
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.MapControllerRoute(
|
|
|
|
|
name: "default",
|
|
|
|
|
pattern: "{controller=Login}/{action=Index}/{id?}");
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|