添加项目文件。
This commit is contained in:
118
TechHelper.Server/Program.cs
Normal file
118
TechHelper.Server/Program.cs
Normal file
@@ -0,0 +1,118 @@
|
||||
using Entities.Contracts;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using TechHelper.Context;
|
||||
using TechHelper.Repository;
|
||||
using SharedDATA.Api;
|
||||
using Entities.Configuration;
|
||||
using Microsoft.AspNetCore.Identity;
|
||||
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
||||
using Microsoft.IdentityModel.Tokens;
|
||||
using System.Text;
|
||||
using TechHelper.Features;
|
||||
using TechHelper.Services;
|
||||
|
||||
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
|
||||
builder.Services.AddControllers(); // <20><><EFBFBD><EFBFBD> MVC <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> (<28><><EFBFBD><EFBFBD> API)
|
||||
|
||||
// 2. <20><><EFBFBD>ݿ<EFBFBD><DDBF><EFBFBD><EFBFBD><EFBFBD> (DbContext)
|
||||
builder.Services.AddDbContext<ApplicationContext>(options =>
|
||||
options.UseMySql(
|
||||
builder.Configuration.GetConnectionString("XSDB"),
|
||||
ServerVersion.AutoDetect(builder.Configuration.GetConnectionString("XSDB"))
|
||||
)
|
||||
).AddUnitOfWork<ApplicationContext>()
|
||||
.AddCustomRepository<Assignment, AssignmentRepository>()
|
||||
.AddCustomRepository<AssignmentAttachment, AssignmentAttachmentRepository>()
|
||||
.AddCustomRepository<AssignmentGroup, AssignmentGroupRepository>()
|
||||
.AddCustomRepository<AssignmentQuestion, AssignmentQuestionRepository>()
|
||||
.AddCustomRepository<Class, ClassRepository>()
|
||||
.AddCustomRepository<ClassStudent, ClassStudentRepository>()
|
||||
.AddCustomRepository<ClassTeacher, ClassTeacherRepository>()
|
||||
.AddCustomRepository<Question, QuestionRepository>()
|
||||
.AddCustomRepository<Submission, SubmissionRepository>();
|
||||
|
||||
builder.Services.AddAutoMapper(typeof(AutoMapperProFile).Assembly);
|
||||
|
||||
// 3. <20><><EFBFBD>÷<EFBFBD><C3B7><EFBFBD> (IOptions)
|
||||
builder.Services.Configure<ApiConfiguration>(builder.Configuration.GetSection("ApiConfiguration"));
|
||||
builder.Services.Configure<JwtConfiguration>(builder.Configuration.GetSection("JWTSettings"));
|
||||
|
||||
|
||||
// 4. <20><>֤<EFBFBD><D6A4><EFBFBD><EFBFBD>Ȩ<EFBFBD><C8A8><EFBFBD><EFBFBD> (Identity, JWT, <20>Զ<EFBFBD><D4B6><EFBFBD> Auth)
|
||||
// <20><><EFBFBD><EFBFBD> ASP.NET Core Identity (<28><><EFBFBD><EFBFBD>Ĭ<EFBFBD>ϵ<EFBFBD> Cookie <20><>֤<EFBFBD><D6A4><EFBFBD><EFBFBD>Ȩ<EFBFBD><C8A8><EFBFBD><EFBFBD>)
|
||||
builder.Services.AddIdentity<User, IdentityRole<Guid>>(opt =>
|
||||
{
|
||||
opt.User.AllowedUserNameCharacters = "";
|
||||
opt.Lockout.AllowedForNewUsers = true;
|
||||
opt.Lockout.DefaultLockoutTimeSpan = TimeSpan.FromMinutes(2);
|
||||
opt.Lockout.MaxFailedAccessAttempts = 3;
|
||||
})
|
||||
.AddEntityFrameworkStores<ApplicationContext>()
|
||||
.AddDefaultTokenProviders();
|
||||
builder.Services.Configure<DataProtectionTokenProviderOptions>(Options =>
|
||||
{
|
||||
Options.TokenLifespan = TimeSpan.FromHours(2);
|
||||
});
|
||||
|
||||
|
||||
// <20><><EFBFBD><EFBFBD> JWT Bearer <20><>֤<EFBFBD><D6A4><EFBFBD><EFBFBD>
|
||||
var jwtSettings = builder.Configuration.GetSection("JWTSettings");
|
||||
builder.Services.AddAuthentication(options =>
|
||||
{
|
||||
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme; // <20><><EFBFBD><EFBFBD>Ĭ<EFBFBD><C4AC><EFBFBD><EFBFBD>֤<EFBFBD><D6A4><EFBFBD><EFBFBD>Ϊ JWT Bearer
|
||||
options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme; // <20><><EFBFBD><EFBFBD>Ĭ<EFBFBD><C4AC><EFBFBD><EFBFBD>ս<EFBFBD><D5BD><EFBFBD><EFBFBD>Ϊ JWT Bearer
|
||||
})
|
||||
.AddJwtBearer(options =>
|
||||
{
|
||||
options.TokenValidationParameters = new TokenValidationParameters
|
||||
{
|
||||
ValidateIssuer = true, // <20><>֤ǩ<D6A4><C7A9><EFBFBD><EFBFBD>
|
||||
ValidateAudience = true, // <20><>֤<EFBFBD><D6A4><EFBFBD><EFBFBD>
|
||||
ValidateLifetime = true, // <20><>֤<EFBFBD><D6A4><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ч<EFBFBD><D0A7>
|
||||
ValidateIssuerSigningKey = true, // <20><>֤ǩ<D6A4><C7A9><EFBFBD><EFBFBD>Կ
|
||||
|
||||
ValidIssuer = jwtSettings["validIssuer"], // <20>Ϸ<EFBFBD><CFB7><EFBFBD>ǩ<EFBFBD><C7A9><EFBFBD><EFBFBD>
|
||||
ValidAudience = jwtSettings["validAudience"], // <20>Ϸ<EFBFBD><CFB7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(jwtSettings["securityKey"])) // ǩ<><C7A9><EFBFBD><EFBFBD>Կ
|
||||
};
|
||||
});
|
||||
|
||||
builder.Services.AddScoped<IAuthenticationService, AuthenticationService>();
|
||||
builder.Services.AddScoped<IEmailSender, QEmailSender>();
|
||||
builder.Services.AddTransient<IUserRegistrationService, UserRegistrationService>();
|
||||
builder.Services.AddScoped<IClassService, ClassService>();
|
||||
|
||||
|
||||
builder.Services.AddEndpointsApiExplorer();
|
||||
builder.Services.AddSwaggerGen();
|
||||
|
||||
|
||||
builder.Services.AddCors(options =>
|
||||
{
|
||||
options.AddPolicy("AllowSpecificOrigin",
|
||||
builder => builder
|
||||
.WithOrigins("https://localhost:7047", "http://localhost:5190")
|
||||
.AllowAnyHeader()
|
||||
.AllowAnyMethod()
|
||||
.AllowCredentials());
|
||||
});
|
||||
|
||||
var app = builder.Build();
|
||||
|
||||
// Configure the HTTP request pipeline.
|
||||
if (app.Environment.IsDevelopment())
|
||||
{
|
||||
app.UseSwagger();
|
||||
app.UseSwaggerUI();
|
||||
}
|
||||
|
||||
app.UseCors("AllowSpecificOrigin");
|
||||
app.UseHttpsRedirection();
|
||||
|
||||
app.UseAuthorization();
|
||||
|
||||
app.MapControllers();
|
||||
|
||||
app.Run();
|
Reference in New Issue
Block a user