tycg_carviolation_BE/Traffic.Api/Startup.cs

193 lines
7.5 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
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.Tokens;
using Microsoft.OpenApi.Models;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using Traffic.Repository.Infrastructures;
using Traffic.Service.Filter;
using Traffic.Service.Helpers;
using Traffic.Service.Implements;
using Traffic.Service.Interfaces;
namespace Traffic.Api
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
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.AddControllers();
services.AddSingleton<JwtHelpers>();
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
services.AddScoped<ConnectionFactory>();
services.AddScoped<IUnitOfWork, UnitOfWork>();
services.AddScoped<ILoginService, LoginService>();
services.AddScoped<IAccountService, AccountService>();
services.AddScoped<ICompanyInformationService, CompanyInformationService>();
services.AddScoped<IPoliceStationService, PoliceStationService>();
services.AddScoped<IEventTypeService, EventTypeService>();
services.AddScoped<IAnnounceService, AnnounceService>();
services.AddScoped<IRoleService, RoleService>();
services.AddScoped<IPageListService, PageListService>();
services.AddScoped<INotPunishService, NotPunishService>();
services.AddScoped<ISiteInformationService, SiteInformationService>();
services.AddScoped<IEventService, EventService>();
services.AddScoped<IMalfunctionService, MalfunctionService>();
services.AddScoped<IReportService, ReportService>();
services.AddScoped<IRepairService, RepairService>();
services.AddScoped<IFileService, FileService>();
services.AddScoped<IMailService, MailService>();
services.AddScoped<LogFilter>();
services.AddLogging(builder =>
{
builder.AddConfiguration(Configuration.GetSection("Logging"))
.AddFilter("Microsoft", LogLevel.Warning)
.AddConsole()
.AddDebug();
});
SetCors(services);
SetSwagger(services);
SetJwtBearer(services);
}
private static void SetCors(IServiceCollection services)
{
services.AddCors(o => o.AddPolicy("Traffic", builder =>
{
builder.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader();
}));
}
private static void SetJwtBearer(IServiceCollection services)
{
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 = "Traffic",
// <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("wgQWt6kNQR3dyYoCopq1"))
};
});
}
private static void SetSwagger(IServiceCollection services)
{
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo { Title = "Traffic.Api", Version = "v1" });
c.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme
{
Description = $@"JWT Authorization header using the Bearer scheme. \r\n\r\n
Enter 'Bearer' [space] and then your token in the text input below. \r\n\r\n
Example: 'Bearer 12345abcdef'",
Name = "Authorization",
In = ParameterLocation.Header,
Type = SecuritySchemeType.ApiKey,
Scheme = "Bearer"
});
//Set the comments path for the Swagger JSON and UI.
var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
c.IncludeXmlComments(xmlPath);
c.AddSecurityRequirement(new OpenApiSecurityRequirement()
{
{
new OpenApiSecurityScheme
{
Reference = new OpenApiReference
{
Type = ReferenceType.SecurityScheme,
Id = "Bearer"
},
Scheme = "oauth2",
Name = "Bearer",
In = ParameterLocation.Header,
},
new List<string>()
}
});
});
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseSwagger();
app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "Traffic.Api v1"));
}
app.UseHttpsRedirection();
app.UseRouting();
app.UseCors("Traffic");
app.UseAuthentication();//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
app.UseAuthorization(); //<2F>A<EFBFBD><41><EFBFBD>v
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
}
}