Implementing authentication and authorization in Web Api by using Identity and JWT
I will try to teach step by step how to set up implementing authentication and authorization
- Install below packages using Nuget
- Explanation about jwt and identity
- Settings class (jwt,identity)
- Add Configuration jwt and identity
- its implementation
- implementation login and register
JSON Web Token (JWT) is an open standard (RFC 7519) that defines a compact and self-contained way for securely transmitting information between parties as a JSON object.
ASP.NET Core Identity is a membership system which allows you to add login functionality to your application.used to implement forms authentication
Package | Description | Category |
---|---|---|
Microsoft.AspNetCore.Authentication.JwtBearer | Contains types that enable support for JWT bearer based authentication. | Jwt |
System.IdentityModel.Tokens.Jwt | Includes types that provide support for creating, serializing and validating JSON Web Tokens. | Jwt |
Microsoft.AspNetCore.Identity.EntityFrameworkCore | Provides types for persisting Identity data with Entity Framework Core. | Identity |
"Jwt": {
"Key": "",
"Issuer": "”
"Audience": "",
"Subject": ""
}
app.UseAuthentication();
app.UseAuthorization();
//Add the default identity system configuration for the specified User and Role types.
services.AddIdentity< ApplicationUser, ApplicationRole>()
.AddEntityFrameworkStores<Context>()
.AddDefaultTokenProviders();
//Add ContextConnection in startup
services.AddDbContext<Context>(options => {
options.UseSqlServer(Configuration.GetConnectionString("ContextConnection"));});