How to implement Identity, Connection String, IdentityDbContext in .Net 6 Part - 2
11:52 Amit Kumar
In this article, we are going to create Web API using .Net 6 and Asp.Net Core and also implement JWT Authentication.
Right click on "TimeManager.DAL" add create a folder and put name "Models"
Create "User" class inside "Models" folder see below code.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace TimeManager.DAL.Models
{
public class User
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string Password { get; set; }
public string Role { get; set; }
public DateTime Created { get; set; } = DateTime.Now;
}
}
namespace TimeManager.DAL.Models
public class User
{
public string FirstName { get; set; }
}
Now in User class will inherit "IdentityUser". We will add the package "Microsoft.AspNetCore.Identity". Please follow these step Right click on TimeManager.DAL >> Manage NuGet Packages... >> Search "Microsoft.AspNetCore.Identity" >> Install. Now you can inherit "IdentityUser" code will look like this
using
Microsoft.AspNetCore.Identity;
namespace TimeManager.DAL.Models
{
public class User : IdentityUser
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string Password { get; set; }
public string Role { get; set; }
public DateTime Created { get; set; } = DateTime.Now;
}
}
namespace TimeManager.DAL.Models
public class User : IdentityUser
public string FirstName { get; set; }
}
Now we are going to implement context class. In this article, we will see Entity Framework Code First development approach requries us to create a data access context class that inherits from the IdentityDbContext class. In this class, we override the OnModelCreating() method and used to initialize the context such that the model can be further configured before it is locked down. The following is the code snippet for the context class.
Add NuGet Packages before creating IdentityDbContext
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
using
Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using
Microsoft.EntityFrameworkCore;
using TimeManager.DAL.Models;
namespace TimeManager.DAL
{
public class ApplicationDbContext : IdentityDbContext<User>
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options) { }
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
}
}
}
namespace TimeManager.DAL
public class ApplicationDbContext : IdentityDbContext<User>
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options) { }
protected override void OnModelCreating(ModelBuilder builder)
base.OnModelCreating(builder);
}
}
Now we register our context to dependency injection during the application start up. Once we register ApplicationDbContext context as a service to dependency injection, then provide it via constructor to API controller
Following steps to register ApplicationDbContext as a service.
Open the appsettings.json(stores application level settings such as connection string, SMTP etc) file and write the connection string.
{
"ConnectionStrings": {
"DefaultConnection":
"Server=DESKTOP-LL7L6L9;Database=TimeManager;Trusted_Connection=True"
},
"Logging": {
"LogLevel":
{
"Default":
"Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"AllowedHosts": "*"
}
"ConnectionStrings": {
},
"Logging": {
}
},
"AllowedHosts": "*"
}
Setp 1: Install below packages
Microsoft.EntityFrameworkCore.SqlServer
Microsoft.EntityFrameworkCore
Step 2: Add below lines of code for register ApplicationDbContext to dependency injection as per follwing code snippet.
builder.Services.AddDbContext<ApplicationDbContext>(options => options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection")));
For Identity Configure you have to open Program.cs and add the identity system configuration for the specified User and Role types.
builder.Services.AddIdentity<User, IdentityRole>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();
Migration
Now we have created model so time to create database using migration
Step 1: Install Nuget package in Main Project
Microsoft.EntityFrameworkCore.Design
Microsoft.EntityFrameworkCore.Tools
Step 2: Go to manu Tools > NuGet Package Manager > Package Manager Console
Step 3: Add-Migration firstmigration to create the initial set of tables for our model
Step 4: Update-Database to apply the new migration to the database.
Database Tables
0 comments:
Post a Comment
Do not enter spam link