90 lines
		
	
	
		
			3.0 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
			
		
		
	
	
			90 lines
		
	
	
		
			3.0 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
using GrossesMitainesAPI.Data;
 | 
						|
using GrossesMitainesAPI.Services;
 | 
						|
using Microsoft.AspNet.Identity;
 | 
						|
using Microsoft.AspNetCore.Authentication;
 | 
						|
using Microsoft.AspNetCore.Authentication.Cookies;
 | 
						|
using Microsoft.AspNetCore.Identity;
 | 
						|
using Microsoft.EntityFrameworkCore;
 | 
						|
using Microsoft.Extensions.Configuration;
 | 
						|
using System.Net;
 | 
						|
 | 
						|
var MyAllowSpecificOrigins = "_myAllowSpecificOrigins";
 | 
						|
var builder = WebApplication.CreateBuilder(args);
 | 
						|
 | 
						|
builder.Services.AddCors(options => {
 | 
						|
    options.AddPolicy(name: MyAllowSpecificOrigins,
 | 
						|
                      policy => {
 | 
						|
                          policy.WithOrigins("http://localhost:3000",
 | 
						|
                                             "https://localhost:3000")
 | 
						|
                          .AllowAnyMethod()
 | 
						|
                          .AllowAnyHeader()
 | 
						|
                          .AllowCredentials();
 | 
						|
                      });
 | 
						|
});
 | 
						|
 | 
						|
builder.Services.AddControllers();
 | 
						|
 | 
						|
builder.Services.AddIdentityCore<InventoryUser>()
 | 
						|
                    .AddRoles<IdentityRole>()
 | 
						|
                    .AddEntityFrameworkStores<InventoryContext>()
 | 
						|
                    .AddSignInManager();
 | 
						|
builder.Services.Configure<IdentityOptions>(o =>
 | 
						|
    o.User.RequireUniqueEmail = true);
 | 
						|
 | 
						|
builder.Services.AddAuthorization();
 | 
						|
builder.Services.AddAuthentication().AddIdentityCookies();
 | 
						|
 | 
						|
builder.Services.Configure<IdentityOptions>(options => { 
 | 
						|
    options.User.RequireUniqueEmail = true;
 | 
						|
});
 | 
						|
 | 
						|
// Source: https://github.com/dotnet/aspnetcore/issues/9039
 | 
						|
builder.Services.ConfigureApplicationCookie(o => {
 | 
						|
    o.Events = new CookieAuthenticationEvents() {
 | 
						|
        OnRedirectToLogin = (ctx) => {
 | 
						|
            if (ctx.Request.Path.StartsWithSegments("/api") && ctx.Response.StatusCode == 200) {
 | 
						|
                ctx.Response.StatusCode = 401;
 | 
						|
            }
 | 
						|
            return Task.CompletedTask;
 | 
						|
        },
 | 
						|
        OnRedirectToAccessDenied = (ctx) => {
 | 
						|
            if (ctx.Request.Path.StartsWithSegments("/api") && ctx.Response.StatusCode == 200) {
 | 
						|
                ctx.Response.StatusCode = 403;
 | 
						|
            }
 | 
						|
            return Task.CompletedTask;
 | 
						|
        },
 | 
						|
        OnRedirectToReturnUrl = (ctx) => {
 | 
						|
            if (ctx.Request.Path.StartsWithSegments("/api") && ctx.Response.StatusCode == 200) {
 | 
						|
                ctx.Response.StatusCode = 418;
 | 
						|
            }
 | 
						|
            return Task.CompletedTask;
 | 
						|
        }
 | 
						|
    };
 | 
						|
});
 | 
						|
 | 
						|
 | 
						|
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
 | 
						|
builder.Services.AddEndpointsApiExplorer();
 | 
						|
builder.Services.AddSwaggerGen();
 | 
						|
 | 
						|
builder.Services.AddDbContextFactory<InventoryContext>(options => { options.UseSqlServer("DefaultConnection"); });
 | 
						|
builder.Services.AddSingleton<DatabaseCacheService>();
 | 
						|
builder.Services.BuildServiceProvider().GetRequiredService<DatabaseCacheService>();
 | 
						|
 | 
						|
var app = builder.Build();
 | 
						|
 | 
						|
// Configure the HTTP request pipeline.
 | 
						|
if (app.Environment.IsDevelopment()) {
 | 
						|
    app.UseSwagger();
 | 
						|
    app.UseSwaggerUI();
 | 
						|
}
 | 
						|
 | 
						|
app.UseHttpsRedirection();
 | 
						|
 | 
						|
app.UseCors(MyAllowSpecificOrigins);
 | 
						|
app.UseAuthorization();
 | 
						|
app.UseAuthentication();
 | 
						|
 | 
						|
app.MapControllers();
 | 
						|
 | 
						|
app.Run(); |