ุงูู Security Module ูู ุงูุชุทุจููุงุช ุจูุชููู ู ู ุนูุตุฑูู ุฃุณุงุณููู:
- Authentication (ุงูุชุญูู ู ู ุงููููุฉ)
- Authorization (ุงูุชูููุถ ุฃู ุงูุชุตุฑูุญ)
ููุจููู ุจุชุณุจููู ุฃู ูููู ุนูููุง Step zero ููู Identification/Registration
ุฎุทูุงุช ุชูููุฐ Security Module
-
Identification/Registration (ุงูุชุนุฑูู ุฃู ุงูุชุณุฌูู):
- ุฃูู ุฎุทูุฉ ูู ุงูุชุฃูุฏ ู ู ูููุฉ ุงูู ุณุชุฎุฏู ุ ูุฏู ุจุชููู ู ู ุฎูุงู ุชุณุฌูู ุงูู ุณุชุฎุฏู ูู ุงูุชุทุจูู ูุฅูุดุงุก ุญุณุงุจ ุฎุงุต ูู.
- ุจุนุฏ ูุฏู ุจููุฏุฑ ูุนุชุจุฑู ู ุณุชุฎุฏู (User) ุฏุงุฎู ุงูุชุทุจูู ุจุชุงุนูุง.
-
Login (ุชุณุฌูู ุงูุฏุฎูู):
- ุจุนุฏ ู ุง ูุชู ุชุณุฌูู ุงูู ุณุชุฎุฏู ุ ุจูุณุชุฎุฏู ุจูุงูุงุชู ููุชุญูู ู ู ูููุชู ุนูุฏ ูู ุนู ููุฉ ุฏุฎูู.
-
Authorization (ุงูุชูููุถ):
- ุจุนุฏ ุนู ููุฉ ุชุณุฌูู ุงูุฏุฎููุ ุจูุญุฏุฏ ููู ุณุชุฎุฏู ุงูุตูุงุญูุงุช ุงููู ู ุณู ูุญ ูู ุจูุง ูู ุงูุชุทุจูู. ุจู ุนููุ ุจูุญุฏุฏ ุฅูู ุงููู ููุฏุฑ ูุดูููุ ูุฅูู ุงููู ููุฏุฑ ูุนู ููุ ูุงูุญุงุฌุงุช ุงููู ู ุด ู ุณู ูุญ ูู ููุตู ููุง.
ุงุณุชุฎุฏุงู Microsoft Identity Package
ุนุดุงู ูุณุฑุน ุนู ููุฉ ุชุทููุฑ ุงูู Security Moduleุ ุจูุณุชุฎุฏู ุญุฒู ุฉ ุฌุงูุฒุฉ ู ู ู ุงููุฑูุณููุช ุงุณู ูุง Identity. ุงูุญุฒู ุฉ ุฏู ุจุชูุฌู ู ุฏู ุฌุฉ ูููุง ู ุฌู ูุนุฉ ุฎุฏู ุงุช ุฌุงูุฒุฉ ุจุชุณุงุนุฏูุง ุนูู ุชูููุฐ ุงูู ูุงู ุงูู ุทููุจุฉ ุจุดูู ุฃุณูู ูุฃุณุฑุน.
ุฎุฏู ุงุช Microsoft Identity ุงูุฃุณุงุณูุฉ
-
UserManager
:- ุฎุฏู
ุฉ ุจุชุณุงุนุฏูุง ุนูู ุฅุฏุงุฑุฉ ุงูู
ุณุชุฎุฏู
ูู ูู ุงูุชุทุจููุ ูุชููุฑ ููุง ูุธุงุฆู ุฒู:
- ุฅูุดุงุก ู ุณุชุฎุฏู ุฌุฏูุฏ (Create)
- ุชุญุฏูุซ ุจูุงูุงุช ุงูู ุณุชุฎุฏู (Update)
- ุญุฐู ู ุณุชุฎุฏู (Delete)
- ุงูุจุญุซ ุนู ู ุณุชุฎุฏู ุจูุงุณุทุฉ ุจูุงูุงุชู (Find by)
- ุฎุฏู
ุฉ ุจุชุณุงุนุฏูุง ุนูู ุฅุฏุงุฑุฉ ุงูู
ุณุชุฎุฏู
ูู ูู ุงูุชุทุจููุ ูุชููุฑ ููุง ูุธุงุฆู ุฒู:
-
SignInManager
:- ุฎุฏู
ุฉ ุจุชุณุงุนุฏูุง ุนูู ุชูููุฐ ุนู
ููุงุช ุชุณุฌูู ุงูุฏุฎูู ูุชููุฑ ุฅู
ูุงููุงุช ุฅุถุงููุฉ ุฒู:
- ุชุณุฌูู ุงูุฏุฎูู (Sign in)
- ุฅุนุงุฏุฉ ุชุนููู ููู ุฉ ุงูู ุฑูุฑ (Reset Password)
- ุงูุชุญูู ุจุฎุทูุชูู (Two Factor Authentication)
- ุชุณุฌูู ุงูุฏุฎูู ู ู ู ุตุงุฏุฑ ุฎุงุฑุฌูุฉ (External Login)
- ุฎุฏู
ุฉ ุจุชุณุงุนุฏูุง ุนูู ุชูููุฐ ุนู
ููุงุช ุชุณุฌูู ุงูุฏุฎูู ูุชููุฑ ุฅู
ูุงููุงุช ุฅุถุงููุฉ ุฒู:
-
RoleManager
:- ุฎุฏู
ุฉ ูุฅุฏุงุฑุฉ ุงูุฃุฏูุงุฑ ูุงูุตูุงุญูุงุชุ ูุชุชูุญ ููุง ุงููุธุงุฆู ุงูุชุงููุฉ:
- ุฅูุดุงุก ุฏูุฑ ุฌุฏูุฏ (Create)
- ุชุญุฏูุซ ุงูุจูุงูุงุช ุงูุฎุงุตุฉ ุจุงูุฏูุฑ (Update)
- ุญุฐู ุฏูุฑ (Delete)
- ุชุนููู ู ุณุชุฎุฏู ูุฏูุฑ ู ุนูู (Assign User to Role)
- ุฎุฏู
ุฉ ูุฅุฏุงุฑุฉ ุงูุฃุฏูุงุฑ ูุงูุตูุงุญูุงุชุ ูุชุชูุญ ููุง ุงููุธุงุฆู ุงูุชุงููุฉ:
ุงูููุงูุงุช ุงูุฃุณุงุณูุฉ ูู Security Module
ุจูููู ุนูุฏูุง ููุงููู ุฃุณุงุณููู ูู Security Module:
-
User (ุงูู ุณุชุฎุฏู ):
- ุงูููุงุณ ุงูุฎุงุต ุจุงูู
ุณุชุฎุฏู
ูู Microsoft Identity ูู
IdentityUser
ุ ูุจูุญุชูู ุนูู ุนุฏุฉ ุฎุตุงุฆุต (Properties) ู ุซู:ID
Username
NormalizedUsername
Email
NormalizedEmail
PhoneNumber
- ูุฎุตุงุฆุต ุฃุฎุฑู ูุฅุฏุงุฑุฉ ุงูู ุณุชุฎุฏู
- ุงูููุงุณ ุงูุฎุงุต ุจุงูู
ุณุชุฎุฏู
ูู Microsoft Identity ูู
-
Role (ุงูุฏูุฑ):
- ููุงุณ ุฎุงุต ุจุฅุฏุงุฑุฉ ุงูุฃุฏูุงุฑ ูุงูุตูุงุญูุงุช ุงููู ุจูู ุชูููุง ูู ู ุณุชุฎุฏู . ูุชูุญ ููุง ุชุฎุตูุต ูู ุฏูุฑ ุจู ูุงู ู ุนููุฉ ุฃู ุตูุงุญูุงุช ุฏุงุฎู ุงูุชุทุจูู.
Customization
ู
ู
ูู ูุนู
ู ุชุฎุตูุต (Customization) ููู Identity classes ุนุดุงู ุชูุงุณุจ ุงุญุชูุงุฌุงุช ุงูุชุทุจูู ุจุดูู ุฃูุถู.
ุจูุนู
ู ููุงุณ ุฎุงุต ุจููุง ููุณู
ูู ู
ุซููุง AppUser
ุ ูุงููู ุจููุฑุซ ู
ู ุงูููุงุณ ุงูุฌุงูุฒ IdentityUser
ุ ูุจูุฏู ููุฏุฑ ูุถูู ุฃู ูุนุฏู ุงูุฎุตุงุฆุต ุนูู ุญุณุจ ุงูู
ุชุทูุจุงุช.
ุจููุณ ุงูุทุฑููุฉุ ู
ู
ูู ูุนู
ู ุชุฎุตูุต ูููุงุณ ุงูู Role ุจุฅูุดุงุก ููุงุณ ุฎุงุต ุจุงุณู
ู
ุซููุง AppRole
ุ ูุงููู ุจููุฑุซ ู
ู IdentityRole
.
ุฏู ุจูุณุงุนุฏูุง ุฅููุง ูุชุญูู
ูู ู
ุนููู
ุงุช ุงูู
ุณุชุฎุฏู
ูู ูุงูุตูุงุญูุงุช ุจุดูู ุฃูุชุฑ ู
ุฑููุฉุ ููุถูู ุฎุตุงุฆุต ุฅุถุงููุฉ ุฃู ูุนุฏู ูู ููููุฉ ุงูุชุนุงู
ู ู
ุน ุงูุจูุงูุงุช ุงูุฎุงุตุฉ ุจูู ู
ุณุชุฎุฏู
ุฃู ุฏูุฑ.
Using it
Installing Package
ููุฑูุญ ููู Dependencies ุจุชุงุน ุงูู Core ููุถูู ุงูุจุงูุฏุฌ ุฏู Identity.EntityFrameWorkCore
ูููุฒู ุขุฎุฑ ูุณุฎุฉ ู
ููุง
Entity
ูุนู
ู ูููุฏุฑ ูุงูู Core ุฒู ู
ุง ููููุง ุฌูุง ูููุฏุฑ ุงูู Entities ููุนู
ู ูููุฏุฑ Identity
ููุนู
ู ุฌูุงู ุงูู
AppUser
ุจุชุงุนูุง ููุฎููู ููุฑุซ ู
ู ุงูู IdentityUser
ูููู ุงุชููู ูุงุญุฏ Generic ูุงูุชุงูู Non Generic
ููู ุงุณุชุฎุฏู
ูุง ุฏู ุฏุงูู
ูุง ุงูู ID ุจุชุงุนูุง ููููู String ูู ุนุงูุฒู ูุจูุง ุญุงุฌุฉ ุบูุฑ ุงูู String ูููุฑุซ ู
ู ุงูู Generic
public class AppUser : IdentityUser
{
public string DisplayName {get; set;}
public Address Address {get; set;}
// ููุนู
ู ููุน ุงุณู
ู ุงุฏุฑูุณ
}
public class Address
{
public int Id {get; set;}
public string FName {get; set;}
public string LName {get; set;}
public string Street {get; set;}
public string City {get; set;}
public string Country {get; set;}
public string AppUserId {get; set;} // Foreign Key: User
}
ุงูู Security ููุนู ููุง Database ู ููุตูุฉ ุชู ุงู ูุง ุนุดุงู ูุฏุง ูู ุฎุฏุช ุจุงูู ูุชูุงูู ุงู ุงูู
Address
ู ูุฑุซุชุด ู ู ุงููBaseEntity
ูุฃู ุงููBaseEntity
ูู ุงูู Base Class ููู Entities ุงูุฎุงุตุฉ ุจุงููStoreContext
ูู ู ูู ุชุณุชุฎุฏู ูุง ุนุงุฏู ุจุณ ูููุบุจุท ุงููู ูููุฑุฃ ุงูููุฏ ูุจูุงุด
ู
ุด ูููุฏุฑ ูุนู
ู BaseEntity
ูู Security ุนุดุงู ุงูู Id ูู ุงูู Address ููุนู Int ุงูู
ุง ูู ุงูู User ุจูุจูุง String ููู ุงูุญูููุฉ ู
ุด ู
ุญุชุงุฌู
ูุจุฑุถู ููู First name ู Last name ุนุดุงู ู ู ูู ุญุฏ ุชุงูู ูุณุชูุจู ุงูู Order
ูุจุนุฏูู ูุธุจุท ุงูุนูุงูุฉ ุจูู ุงูู User ูุงูู Address ูุฃููุง 1:1 ูููุญุท ุงูู Key ุจุชุงุน ุงูู user ูู ุงูู Address ูุฃู ู ุด ูุงุฒู ูู User ูุจูุง ุนูุฏู Address ุฃูู ู ุง ูุนู ู Account ูุงูู Address ุชุนุชุจุฑ May
DbContext
ููุนู ู DbContext ููู Security ุฒู ู ุง ุงุชูููุง ููุนู ู ูู ููุณ ุงูู Repository Layer ููุณ ุงูุชูุณูู ุฉ ุนุงุฏู ุจุณ ููู Security
ููุณุง ุงูู Migrations ููุญุท ูู
ุงู ุงูู DbContext Class
ุจุณ ุงููุฑู ููุง ูููุฑุซ ู
ู ุงูู
IdentityDbContext
ู
ุด ุงูุนุงุฏูุฉ
ุนุดุงู ููุฑุซ ุงูู 7 DbSet
public class AppIdenetityDbContext : IdentityDbContext<AppUser>
{
public AppIdentityDbContext(DbContextOptions<AppIdentityDbContext options)
: base(options)
{
}
// We inhertid 7 Dbset
// We can use `OnConfiguraing`
}
ุนูุฏูุง 3 ูุณุฎ ู
ู ุงูู IdentityDbContext
:
- ุงูุนุงุฏูุฉ ูุฏู ุจุชุงุฎุฏ ุงูู
IdentityUser
ุจุณ ูุฃูุช ูู ุนุงู ู Custom User ู ุด ููููุน - ุงูู Generic ูุฏู ููููุน ุชุณุชุฎุฏู ูุง ู ุน ุงูู Custom User
- ููู ุงู ูุงุญุฏุฉ
ูููุญุทูุง ูู ุงูู program ุฒู ู
ุง ุงุชูููุง
ูููุฒูุฏ ุงูู Connection string ูู ุงูู appsettings
"IdentityConnection": "Connection String"
builder.Services.AddDbContext<AppIdentityDbContext>(option =>
options.UseSqlServer("connectionString"));
// connections string: builder.Configuration.GetConnectionString("IdentityConnection")
// "SQL server ุนุงุฏู"
ุดุฑุญูุง ุงุฒุงู ุชุงูู ูู ุงูู DbContext
ูู ุง ุญุฏ ูุญุจ ููุชุญ Connection ู ุน ุงูู DB ุจุชุงุน ุงูู Security ุฃู ุงูู Identity:
- ููุฑูุญ ูุนู
ู Object ู
ู ุงูู Class ุงููู ุงุณู
ู
AppIdentityDbContext
ูุจุฏู ู ุง ูุนู ูู ูู ุจูุฎูู ุงูู CLR ูุนู ูู (DI) - ุจุณ ุงูุดุงุกู ู
ุนุชู
ุฏ ุนูู ุงูู
DbContextOptions
ูู ููุฑูุญ ูุนู ู Create ููู Options ููุจุนุชูุง - ูุฃูุง ุจูููู ูุงูุช ุจุชุนู
ู Create ููู Options ูุจูุจุนุช ู
ุนุงู ุงูู Connection string ูุงุญูุง ุจูุนู
ููุง ุฒู ู
ุง ุนู
ููุงูุง ุฃู ูุนู
ู Override ููู
OnConfiguring
ูู ููู ุฃู Properties ุฒูุงุฏุฉ ุจูุนู
ููุง DbSet
ูู ุฃูุง ุนุงูุฒูุง ุชุชุนู
ู
ูุนูู ูู ุงูู
ุซุงู ุจุชุงุนูุง ูู ููุง ู
ุญุชุงุฌูู ูุนู
ู ุฌุฏูู ููู Address ููุง ููุนู
ูู DbSet
ุจุณ ูู ุงูู DbContext ููู ูุงู ููุนู
ู Table ูู ูุฃูู ูู ุนูุงูุฉ ู
ุน ุงูู User
ููู ู
ุด ูุถูู ุญุงุฌุฉ ููุฌุฏูู ุฃู ุฃุบูุฑ ุญุงุฌุฉ ู
ุด ู
ุญุชุงุฌ ุฃุนู
ู OnModelCreating
ุฃุตููุง ุงูู Identity ููู 7 DbSet ุจุณ ุงุญูุง ูุฑุซูุง ุงูู IdentityDbContext
ููุฑุซูุงูุง ู
ูู
Migration
ููุนู ู Migration ุจุงูุทุฑููุฉ ุงูุนุงุฏูุฉ ูู ุจุฑูุฌูุช ุงูู Repository ุจุณ ุงูู ุฑุงุฏู ูุงุฒู ูุญุฏุฏ ุฃู Context ุจุณุจุจ ุงููุง ุนูุฏูุง ุงุชููู Context
Add-Migration "IdentityInitialCreate" -Context AppIdentityDbContext -Output Identity/Migrations
ูุฃู ุชุนุงู ู ุจุนุฏ ูุฏุง ูุงุฒู ุชุญุฏุฏ ุงูู Context ุจุชุงุนูุง ูุนูู ู ุซููุง ูู ุนุงูุฒ ุฃุญุฐู ุงู Migration
Remove-Migration -Context AppIdentityDbContext
Update Database
ููุง ุงุชููู ูุง ุนูููุง ูู ุงูู Database Updating
ุฒู ู
ุง ููุง ุจูุทูุจ Object ู
ู ุงูู StoreContext
ููุทูุจ Object ู
ู ุงูู AppIdentityDbContext
var scope = app.Services.CreateScope();
var services = scope.ServiceProvider;
// ููุนู
ู ุฏุง ุจุณ
var _identityContext = services.GetService<AppIdentityDbContext>();
try
{
await _identityContext.Database.MigrateAsync();
}
Data Seeding
ุงุชููู ูุง ุนููุง ูุจู ูุฏุง ูู ุงูู Data Seeding ุงูู ุฑุงุฏู ูุนู ู Seeding ูู User ูุงุญุฏ ุจุณ ู ุด ููู ุฒู ุงูู ุฑุฉ ุงููู ูุงุชุช
- ููู ุณุญ ูููุฏุฑ ุงูู Seeding ู ุด ู ุญุชุงุฌููู
- ููุนู
ู ููุงุณ ูู ุงูู Identity ุงุณู
ู
AppIdentityDbContextSeed
- ุจุณ ููุง ุนุดุงู ูุนู
ู User ู
ุด ุจูููู
ุงูู DbContext ุจุดูู ู
ุจุงุดุฑ ุจุณ ุจูููู
ุงูู
UserManager
ูุฏุง ุงุฎุชูุงู ูู ุงู- ูุฏุง ุนุดุงู ุฃุนู ู Create ููู User
public static class AppIdentityContextSeed
{
public static async Task SeedUsersAsync(UserManager<AppUser> _userManager)
{
// !_userManager.Users.Any()
if(_userManager.Users.Count() == 0)
{
var user = new AppUser()
{
DisplayName = "Mahmoud Feshar",
Email = "mahmoudfeshar11@gmail.com",
UserName = "mahfeshar",
PhoneNumber = "01289086935"
};
await _userManager.CreateAsync(user, "Pa$$w0rd");
}
}
}
ูุจุนุฏูู ูุณุชุฎุฏู ูุง ูู ุงูู Program ุฒู ุงูู ุฑุงุช ุงููู ูุงุชุช ุจุณ ุจุฑุถู ููู ุงุฎุชูุงู
var _userManager = services.GetRequiredService<UserManager<AppUser>>();
await AppIdentityDbContextSeed.SeedUsersAsync(_userManager);
ุจุณ ูุญุฏ ููุง ู
ุด ููุดุชุบู
ูุฃููุง ู
ุญุชุงุฌูู ูุนู
ู Allow ุงูู DI ูุฏุง ููุนู
ูู ุนู ุทุฑูู ุงูู AppIdentity
ุจูู ุจุณุงุทุฉ ูู ุจุชุฑูุน ุชุนู
ู Add ููู Identity Services ูุจุชุนู
ูู Allow ููู DI ูู ุงููููุชููุฑ ุงูุฎุงุต ุจูู
ููููุง 3 Overload:
- ุงูุฃูู ู ุด ุจูุงุฎุฏ ุฃู Parameters ููู ุจุณ ุจูุนู ู Add ููู Default Configurations
- ุงูุชุงูู ุจูุนู ู Add ูุจูุนู ู Configure ูู ุงู ููู Options ุจุชุงุน ุงูู Identity
builder.Services.AddIdentity<AppUser, IdentityRole>(options =>
{
options.Password.RequiredUniqueChars = 2;
options.Password.RequiredNonAlphanumeric = true;
options.Password.RequireUppercase = true;
options.Password.RequireLowercase = true;
});
ูุฃูุง ุนู ูุช Configure ููู Password ูููู ุญุงุฌุงุช ุชุงููุฉ ูุชูุฑ ู ู ูู ุฃุนู ููุง Configure ุชุงููุฉ ููุชููู ุนูููุง ุงูู Session ุงูุฌุงูุฉ ุจุณ ุงูุง ู ุด ูุนู ู Configure ูุฏุง ููุจุงุณูุฑุฏ ุฃูุง ูุดูู ูู ุง ููุฌู ูุนู ู Register ูุนู ู Regex ุนููู
- ุนุดุงู ุฃุนู
ู Seed ููู Users ู
ุญุชุงุฌ ูุจูุง ู
ุนุงูุง Object ู
ู ููุงุณ ุงูู
UserManager
ูุทูุจุช ู ู ุงูู CLR ูุนู ููููู ูููุนู ูู - ููู ุจูุนู
ู ุงูู Object ููููุฐ Ctor ุจูุนุชู
ุฏ ุนูู ุงูู
IUserStore
ุงููู ููู ุงูู Create ุงููู ุงูุง ุงุณุชุฎุฏู ุชูุง - ู
ุญุชุงุฌ ุฃุนู
ู Allow ููู DI ุจุงููุณุจุฉ ููู
UserStore
ููู ุจู ุซุงุจุฉ ุงููUserRepository
- ูุงูุง ุจุนู ู ููู ุงูู Stores
ุนุดุงู ูุฏุง ูุนู ู:
builder.Services.AddIdentity<AppUser, IdentityRole>()
.AddEntityFrameworkStores<AppIdentityDbContext>();