120 lines
4.4 KiB
C#
120 lines
4.4 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using Microsoft.AspNetCore.Builder;
|
|
using Microsoft.AspNetCore.Hosting;
|
|
using Microsoft.AspNetCore.HttpsPolicy;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.Extensions.Configuration;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Microsoft.Extensions.Logging;
|
|
using Microsoft.Extensions.Options;
|
|
|
|
using MySql.Data.EntityFrameworkCore;
|
|
using MySql.Data.EntityFrameworkCore.Extensions;
|
|
using Microsoft.IdentityModel.Tokens;
|
|
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
|
|
|
using Auth.api.Models;
|
|
|
|
using Auth.api.Helpers;
|
|
|
|
using Auth.api.Services;
|
|
|
|
using Auth.api.Repositories;
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Pomelo.EntityFrameworkCore.MySql.Infrastructure;
|
|
|
|
|
|
|
|
namespace Auth.api
|
|
{
|
|
public class Startup
|
|
{
|
|
public Startup(IHostingEnvironment env)
|
|
{
|
|
var builder = new ConfigurationBuilder()
|
|
.SetBasePath(env.ContentRootPath)
|
|
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
|
|
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
|
|
.AddEnvironmentVariables();
|
|
Configuration = builder.Build();
|
|
}
|
|
|
|
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)
|
|
{
|
|
// Add framework services.
|
|
services.AddCors();
|
|
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
|
|
|
|
// configure strongly typed settings objects
|
|
var appSettingsSection = Configuration.GetSection("AppSettings");
|
|
services.Configure<AppSettings>(appSettingsSection);
|
|
|
|
|
|
// configure jwt authentication
|
|
var appSettings = appSettingsSection.Get<AppSettings>();
|
|
var key = Encoding.ASCII.GetBytes(appSettings.Secret);
|
|
services.AddAuthentication(x =>
|
|
{
|
|
x.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
|
|
x.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
|
|
})
|
|
.AddJwtBearer(x =>
|
|
{
|
|
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>();
|
|
|
|
services.Add(new ServiceDescriptor(typeof(UserContext), new UserContext(Configuration.GetConnectionString("DefaultConnection"))));
|
|
|
|
// Simple ORM db Connection
|
|
// services.AddDbContext<PostContext>(opt =>
|
|
// opt.UseMySql(Configuration.GetConnectionString("DefaultConnection")));
|
|
|
|
// Better ORM db Connection Pool
|
|
services.AddDbContextPool<PostContext>(
|
|
options => options.UseMySql(Configuration.GetConnectionString("DefaultConnection"), // replace with your Connection String
|
|
mySqlOptions =>
|
|
{
|
|
mySqlOptions.ServerVersion(new Version(10, 1, 38), ServerType.MariaDb); // replace with your Server Version and Type
|
|
}
|
|
));
|
|
}
|
|
|
|
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
|
|
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
|
|
{
|
|
if (env.IsDevelopment())
|
|
{
|
|
app.UseDeveloperExceptionPage();
|
|
}
|
|
else
|
|
{
|
|
// 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.UseHttpsRedirection();
|
|
app.UseAuthentication();
|
|
app.UseMvc();
|
|
}
|
|
}
|
|
}
|