study-lib-backend/Startup.cs

103 lines
3.9 KiB
C#
Raw Normal View History

2020-12-16 21:41:14 +01:00
using Microsoft.AspNetCore.Authentication.JwtBearer;
2020-12-12 14:47:20 +01:00
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
2020-12-16 21:41:14 +01:00
using Microsoft.AspNetCore.Identity;
2020-12-12 14:47:20 +01:00
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
2020-12-16 21:41:14 +01:00
using Microsoft.IdentityModel.Tokens;
2020-12-13 01:23:12 +01:00
using Newtonsoft.Json;
2020-12-12 14:47:20 +01:00
using StudyLib.API.Data;
2020-12-16 21:41:14 +01:00
using StudyLib.API.Models;
2020-12-20 18:53:24 +01:00
using System;
2020-12-16 21:41:14 +01:00
using System.Text;
2020-12-12 14:47:20 +01:00
namespace StudyLib
{
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)
{
2020-12-16 21:41:14 +01:00
services.Configure<ApplicationSettings>(Configuration.GetSection("ApplicationSettings"));
2020-12-12 14:47:20 +01:00
services.AddCors(options =>
{
options.AddPolicy(name: "AllowSpecificOrigins",
builder =>
{
builder.WithOrigins("http://localhost:4200")
.AllowAnyHeader()
.AllowAnyMethod();
});
});
2020-12-13 01:23:12 +01:00
services.AddControllers().AddNewtonsoftJson(options => {
options.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
options.SerializerSettings.NullValueHandling = NullValueHandling.Ignore;
2020-12-16 21:41:14 +01:00
});
services.AddAuthentication(opt => {
opt.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
opt.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
opt.DefaultScheme = JwtBearerDefaults.AuthenticationScheme;
})
.AddJwtBearer(options =>
{
options.RequireHttpsMetadata = false;
2020-12-20 18:53:24 +01:00
options.SaveToken = true;
2020-12-16 21:41:14 +01:00
options.TokenValidationParameters = new TokenValidationParameters
{
2020-12-20 18:53:24 +01:00
ValidateIssuer = false,
ValidateAudience = false,
2020-12-16 21:41:14 +01:00
ValidateLifetime = true,
ValidateIssuerSigningKey = true,
2020-12-20 18:53:24 +01:00
ClockSkew = TimeSpan.Zero,
2020-12-16 21:41:14 +01:00
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(Configuration["ApplicationSettings:JWTSecret"].ToString()))
};
});
2020-12-12 14:47:20 +01:00
services.AddDbContext<StudyLibContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("StudyLibContext")));
2020-12-20 18:53:24 +01:00
services.AddDefaultIdentity<User>(options => options.SignIn.RequireConfirmedAccount = true).AddEntityFrameworkStores<StudyLibContext>();
2020-12-16 21:41:14 +01:00
services.Configure<IdentityOptions>(options =>
{
options.Password.RequireDigit = false;
options.Password.RequiredLength = 4;
options.Password.RequireLowercase = false;
options.Password.RequireNonAlphanumeric = false;
options.Password.RequireUppercase = false;
});
2020-12-12 14:47:20 +01:00
}
// 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.UseCors("AllowSpecificOrigins");
app.UseHttpsRedirection();
app.UseRouting();
2020-12-16 21:41:14 +01:00
app.UseAuthentication();
2020-12-12 14:47:20 +01:00
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
}
}