110 lines
3.7 KiB
C#
110 lines
3.7 KiB
C#
using Microsoft.AspNetCore.Builder;
|
|
using Microsoft.AspNetCore.Hosting;
|
|
using Microsoft.Extensions.Configuration;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Microsoft.Extensions.Hosting;
|
|
using System;
|
|
using System.Threading.Tasks;
|
|
using BackEndAaaapero.Data;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.IdentityModel.Tokens;
|
|
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
|
using System.Text;
|
|
using AutoMapper;
|
|
using Services;
|
|
|
|
namespace BackEndAaaapero
|
|
{
|
|
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.AddDbContext<Context>(opt =>
|
|
{
|
|
opt.UseMySql(Configuration.GetConnectionString("DefaultConnection"));
|
|
});
|
|
|
|
//--------------------------------------------------------------------------------
|
|
|
|
services.AddCors();
|
|
services.AddControllers();
|
|
services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies());
|
|
|
|
// configure jwt authentication
|
|
var key = Encoding.ASCII.GetBytes(Configuration["Secret"]);
|
|
services.AddAuthentication(x =>
|
|
{
|
|
x.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
|
|
x.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
|
|
})
|
|
.AddJwtBearer(x =>
|
|
{
|
|
x.Events = new JwtBearerEvents
|
|
{
|
|
OnTokenValidated = context =>
|
|
{
|
|
var userService = context.HttpContext.RequestServices.GetRequiredService<IUserService>();
|
|
var userId = int.Parse(context.Principal.Identity.Name);
|
|
var user = userService.GetById(userId);
|
|
if (user == null)
|
|
{
|
|
// return unauthorized if user no longer exists
|
|
context.Fail("Unauthorized");
|
|
}
|
|
return Task.CompletedTask;
|
|
}
|
|
};
|
|
x.RequireHttpsMetadata = false;
|
|
x.SaveToken = true;
|
|
x.TokenValidationParameters = new TokenValidationParameters
|
|
{
|
|
ValidateIssuerSigningKey = true,
|
|
IssuerSigningKey = new SymmetricSecurityKey(key),
|
|
ValidateIssuer = false,
|
|
ValidateAudience = false
|
|
};
|
|
});
|
|
|
|
// configure DI for application services
|
|
services.AddScoped<IUserService, UserService>();
|
|
//------------------------------------------------------------------------
|
|
|
|
}
|
|
|
|
// 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(x => x
|
|
.AllowAnyOrigin()
|
|
.AllowAnyMethod()
|
|
.AllowAnyHeader());
|
|
|
|
app.UseHttpsRedirection();
|
|
|
|
app.UseRouting();
|
|
|
|
app.UseAuthentication();
|
|
|
|
app.UseAuthorization();
|
|
|
|
app.UseEndpoints(endpoints =>
|
|
{
|
|
endpoints.MapControllers();
|
|
});
|
|
}
|
|
}
|
|
}
|