Daha önce oluşturduğumuz ApplicationDbContext yapısını burada veritabanı işlemleri için yetkilendiriyoruz. Sistemin kullanıcı profili olarak AppUser yapısını yetkili olarak belirtiyoruz.
Program.cs dosyasını aşağıdaki gibi düzenliyoruz.
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
var builder = WebApplication.CreateBuilder(args);
//veritabanı
var connectionString = builder.Configuration.GetConnectionString("VeriYolu");
builder.Services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(connectionString));
//kimlik işlemleri
builder.Services.AddIdentity<AppUser, IdentityRole>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();
// Add services to the container.
builder.Services.AddControllersWithViews();
//giriş ve hata sayfalarını tanımlıyoruz
builder.Services.ConfigureApplicationCookie(options =>
{
options.LoginPath = "/Hesap/Index";
options.LogoutPath = "/Hesap/Cikis";
options.AccessDeniedPath = "/Hesap/YetkiHatasi";
});
var app = builder.Build();
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Home/Error");
// 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.UseStaticFiles();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
app.Run();
app.UseHttpsRedirection() https ye yönlendirme yapılmasını sağlayacak.
app.UseAuthentication() oturum yönetimi yapmamızı sağlayacak
app.UseAuthorization() kullanıcı rollerine göre yetkilendirme yapmamızı sağlayacak.
builder.Services.ConfigureApplicationCookie(options =>
{
options.LoginPath = "/Hesap/Index";
options.LogoutPath = "/Hesap/Cikis";
options.AccessDeniedPath = "/Hesap/YetkiHatasi";
});
Yukarıdaki kodlar ile üyelik sisteminde giriş, çıkış sayfalarını ve kullanıcıların yetkisi olmayan sayfalara erişmeye çalıştığında yönlendirilecekleri sayfaları tanımlıyor.