ูููู ู ููุงู ูุง ูู ุงูู Security Module Overview of ASP.NET Core Authentication | Microsoft Learn Authentication is the process of determining a userโs identity. ูู ูู User ุนูุฏูุง ุฃุตูุง ููุง ูุงุ Authorizationย is the process of determining whether a user has access to a resource. ูู ุตูุงุญูุฉ ุงูู ูุดูู ุงูุญุงุฌุฉ ุฏู ููุง ูุงุ ููู ู ุนูุงูุง Roles ูุนูู ูู User ูู Role ู ุนููุฉ ูุนูู ุงุณุงุณูุง ุจุชุญุฏุฏ ูู ู ุณู ูุญูุฉ ูุฏุฎู ุนูู ุงูู ูู ูุฏุฎูุด ุนูู ุงูู ุงูู ุง ูู ู ููุด Roles ูุจูุง ูุฏุง ู ุนูู ุงูู Authenticate ุงูู ุจุฑุถู Authorized ูุฏุฎู ุนูู ุงู ุญุงุฌุฉ
ููุนู ู Test ุฏูููุชู ููู Authorization ุงู ุนูุฏู Endpoint ูู ูููุนุด ุญุฏ ูููู ูุง ุบูุฑ ูู ุง ูุจูุง Authorized ุจู ุง ุงูู ู ุนูุฏูุด Roles ูู ุนูู Authorized ุงูู Authenticated ูุนูู ู ู ุงูุขุฎุฑ ูุจุนุชูู Token
Get all product
ููุฌุฑุจ ูู ุงูู Endpoint ุงููู ุจุชุฌูุจ ูู ุงูู Products
ูุฑูุญ ุนูู Product Controller ููุญุท ุงูู Decorator ุงููู ุงุณู
ู [Authorize]
ููู
ุงู ู
ุญุชุงุฌูู ูุฒูุฏ ุงูุงุชููู Middle Wares ุงูุฎุงุตูู ุจุงูู Authentication and Authorization
ูุจุญุทูู
ุจุนุฏ ุงูู Routing ุจุนุฏ ุงูู MapControllers
// main
app.UseStaticFiles();
app.MapControllers();
app.UseAuthentication();
app.UseAuthorization();
UseAuthentication
:
ูู
ุง ุจูุนุฏู ุนูููุง ุจูุจุฏุฃ ูุนู
ู Check ูู ูู ู
ุนุงู Token ููุง ูุง ูุงูู Token ุจูุชุจุนุช ูู ุงูู Header ุจุชุงุน ุงูู Request
ูุงูู Http ุนุจุงุฑุฉ ุนู ุงุชููู Messages ููู
ุง: Http Request Message, Http Response Message ูุนุดุงู ูุนู
ู Login ุจูุนู
ู Http Request ูุจูุจูุง ูููุง Header ู Body ููู ุจูุจุนุช ุงูู Token ูู ุงูู Header ุจุชุงุน ุงูู Request ูุงูู Middle ware ุจูุนู
ู Check ุนูู ุงูู Token ููุดูู ูู ุชู
ุงู
ููุง ูุง
UseAuthorization
:
ุงูุช ุนุงูุฒ ุชููู
ุฃู Endpointุ ุทุจ ูู ุฃู ุญุฏ ูููู
ูุง ุนุงุฏู ููุง ูุงุฒู
ุจู Role ู
ุนููุฉุ
ุงูู Middle ware ุจุชููู ุจูุนุฏู ุจููุง ูู ุงูู Request ูุงูู Response
ูู ุฌุฑุจูุง ุฏูููุชู ูุนู
ู Request ุนูู ุงูู Endpoint ุจุชุงุนูุง ููู ููุนุฏู ุนูู ูู ุงูู Middlewares ุจุณ ูููุฌู ุนูู UseAuthentication
ูููู ูุงููุง ู
ุจุนุชูุงุด ุงูู Token ูููุง ูููุฑุฌุน ุชุงูู ุนูู ููุณ ุงูู Middlewares ุจุณ Response ููููุฐ ูู ููู Logic ูููููุฐ Middleware ุงูู Error ุจู Code ุจูุณุงูู 404
ูุฏุง ุจุณุจุจ ุงูู ู
ุจุนุชุด ุงูู Token ูุงูู Token ุจูุจูุง ูู Schema ู
ุนููุฉ ููู ุฌุงูู Schema ู
ุฎุชููุฉ ูุฏุง ุจุณุจุจ ุงู ู
ู
ูู ูุจูุง ุนูุฏู Endpoint ุชุงููุฉ ุจุชุณู
ุญ ุงู ุงู ุญุฏ ูุฏุฎููุง ู
ุด ูุงุฒู
ูุจูุง Authorize ููู ู
ุด ุจูุชุนุฑู ุนูู ุงูู Endpoint ุฏู ูุจูููู ุงู ุงูู Error 404
ูุงูุง ู
ุญุชุงุฌ ูุงูุง ุจุนู
ู Request ุงุจุนุช ุงูู Token ูุงุจุนุชู ูู ุงูู headers ุจุชุงุน ุงูู Request ุงุฒูุฏ ูุงุญุฏ ุงุณู
ู Authorization ูุงุญุทูู ุงูู Token ู
ู ุฎูุงู ุงูู ุงุนู
ู Login ูููุจูุง ู
ุนุงูุง
ุจุณ ูุงุฒู
ุงุญุทูู ุงูู Schema ููุชูู ูุญุท Bearer Token
ุจุณ ุฏูููุชู ุงูุง ู
ุญุฏุฏุชุด Schema ู
ุนููุฉ ูู ุงูู Endpoint ุจุชุงุนูุง ููุฏุง ูู ููุนุชุจุฑูุง ุงูู Default ูู
ุด ูุงุฒู
ูุนุฑููุง ููู ุจุนุช ุจุฑุถู ููุจุนุช ููุณ ุงูู Error ูุงูู ู
ุญุชุงุฌ ุงูู Schema ุงูู Default
ูุงูุง ู
ุญุชุงุฌ ุงูููู ุงู ุงูู Schema ูู ุงูู Bearer
[Authorize(AuthenticationSchemes = "Bearer")]
ูุจุฑุถู ูู ุดุบููุงู ููุฏููู Error ุจุณ 500 ูุฃูู ู ุญุชุงุฌ ุฃุนู ู Authentication handler ูุฃู ูู ููู Token ุฌุงู ูุฃูุง ูุงุฒู ุงุนู ู ูู ุงูู Code ุงุฒุงู ุฃุนู ูู Validate
ูุงูู Scheme ุฏู ูู ุงููู ุจุชุญุฏุฏ ุทุฑููุฉ ุงูู Validation ุนูู ุงูู Token ูู ู ูู ูููู ุนูุฏู ุฃูุชุฑ ู ู Scheme
The registered authentication handlers and their configuration options are called โschemesโ.
Authentication schemes are specified by registering authentication services inย Program.cs
Theย authentication schemeย can select which authentication handler is responsible for generating the correct set of claims. For more information, seeย Authorize with a specific scheme.
An authentication scheme is a name that corresponds to:
- An authentication handler. ุฏุง ุจูุนู ู Validate ุนูู ุงูู Token ูู ุตุญ ููุง ูุงุ
- Options for configuring that specific instance of the handler.
authentication handler
ูุฃูุง ุฏูููุชู ู ุญุชุงุฌ ุงุนู ู authentication handler ูุฑูุญ ูู ุงูู Program ูู ุฌุฒุก ุงูู Configure Services
ูู ูุงูู Register ูุนูู DI ููุฑูุญ ูู ุงูู AddIdentityServices
ูุงุถูู
public static IServiceCollection AddIdentityServices(this IServiceCollection services, IConfiguration configuration)
// Add IConfiguration
// We be called by default, When I call `AddIdentity`
// Registers services required by authentication services
services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme) // "Bearer"
.AddJwtBearer(options =>
{
// Configure Authentication Handler
// options.ForwardSignIn // Go to page SignIn but for MVC
options.TokenValidationParameteres = new TokenValidationParameters()
{ // Object initializer
// Validate with Register Claims that i used to generate token
ValidateAudience = true,
// From configurations
ValidAudience = configuration["JWT:ValidAudience"],
ValidateIssuer = true,
ValidIssuer = configuration["JWT:ValidIssuer"],
ValidateIssuerSigningKey = true, // authKey
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_configuration["JWT:SecretKey"])),
ValidateLifetime = true,
ClockSkew = TimeSpan.FromDays(double.Parse(configuration["JWT:DurationInDays"]))
};
});
// If I want another Scheme
.AddJwtBearer("Bearer02", option =>
{
});
// Program
builder.Services.AddIdentityServices(builder.Configuration);
// Endpoint
[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
// Search how to add more than one scheme
ูู ุจูุชูุงุฏู ุนูููุง By Default ูู
ุง ุจุณุชุฎุฏู
AddIdentity
ูุงููุง ุจุชุนู
ู Allow DI Services ุงููู ู
ุญุชุงุฌุงูุง ุงูู Authentication Services
ูุจุฑุถู ุงูุทุจูุนู ุงููุง ู
ุด ุจุชุงุฎุฏ Parameter ุจุณ ุงูู
ุฑุงุฏู ูุณุชุฎุฏู
ุงูู Overload ุงููู ุจูุงุฎุฏ defaultScheme
ูุฃูู ูุงุญุฏุฉ ูุชูุงูููู ู
ุญุฏุฏุชุด ุงุณู
ู ููู ููุณุชุฎุฏู
ุงูู Default ุงููู ูู ูู ุงูุญุงูุฉ ุฏู ุงูู Bearer
ููู ู
ุญุฏุฏุชุด ููุณุชุฎุฏู
ุงูู Default ุงููู ูู ุจูุธูุฑ ูู ุงูู Error ูู ุงูุขุฎุฑ ุฎุงูุต
ุงูุง ูู ุงูุนุงุฏู ู ุด ุจุจุนุช ุงูู Token ูู ุงูู Header ุงูู ุง ุจุฏุฎู ูู ุฎุงูุฉ ุงูู Authorization ูุจุฎุชุงุฑ Bearer Token ูุจูุชุจ ุงูู Token
ูู ุฎุฏุช ุจุงูู ุงูุง ูุฏุง ูุญุชุงุฌ ูู ู ุฑุฉ ุงุญุฏุฏ ุงูู Authorize scheme ููู Endpoint ุทูุจ ูู ุงูุง ุนุงูุฒ ุฃุซุจุช ุงูู Scheme ุฏู ููู ุง ุงุนู ู ุญุงุฌุฉ ู ุฎุชููุฉ ุงุนู ู ูุงุญุฏุฉ ุชุงููุฉุ
Set Default Scheme
ููุฑูุญ ููู AddAuthentication
ูููุง ุงุณุชุฎุฏู
ูุง ุงูู Overload ุงูุชุงูู ุงููู ุจูุงุฎุฏ ุงูู Scheme
ุงูุง ูุนู
ู ูุงุญุฏ ุชุงูุช ุจูุงุฎุฏ ู
ูู Lambda ุงุณู
ูุง Option
services.AddAuthentication(options =>
{
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme; // ุฏู ุฒู ุงูุนุงุฏูุฉ ุฎุงูุต
options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
//ุงู ุงููุฏ ุจูููุช ุงูุซุฑูุฒุฏ ุงููู ุจูุณุชุฎุฏู
ูุง ุนุดุงู ูุชุญุฏู ุงู ุญุฏ
})
// Endpoint
[Authorize]