Guid
Guid
context:
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (!optionsBuilder.IsConfigured)
{
optionsBuilder.UseSqlServer(GetConnectionString());
}
}
private string GetConnectionString()
{
IConfiguration config = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", true, true)
.Build();
var strConn = config["ConnectionStrings:MyConnectionString"];
return strConn;
}
PROGRAM.CS
public static void Main(string[] args)
{
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddSession();
// DI
builder.Services.AddScoped<IBranchAccountDAO, BranchAccountDAO>();
builder.Services.AddScoped<ISilverJewelryDAO, SilverJewelryDAO>();
builder.Services.AddScoped<ICategoryDAO, CategoryDAO>();
builder.Services.AddDbContext<SilverJewelry2024DBContext>();
builder.Services.AddScoped<IBranchAccountRepository, BranchAccountRepository>();
builder.Services.AddScoped<ISilverJewelryRepository, SilverJewelryRepository>();
builder.Services.AddScoped<ICategoryRepository, CategoryRepository>();
builder.Services.AddHttpContextAccessor();
builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();
//builder.Services.AddSwaggerGen();
builder.Services.AddControllersWithViews();
app.UseHttpsRedirection();
app.UseCors("AllowSpecificOrigin");
app.UseRouting();
// Authentication
app.UseSession();
app.UseMiddleware<JwtTokenMiddleware>();
app.UseAuthentication();
app.UseAuthorization();
app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "Assignment_PRN"));
app.MapControllers();
app.UseStaticFiles();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "account",
pattern: "{controller=BranchAccounts}/{action=Login}/{id?}");
});
app.Run();
}
MIDDLE WARE
public class JwtTokenMiddleware
{
private readonly RequestDelegate _next;