Implementing Asp.net Core Identity |Authentication and Authorization| Introduction To Asp.net Core Identity | asp.net core 2.0 identity tutorial
Hello & As-salam u alikum ! , In the previous articles we had discussed about generic Repository Pattern , There is the reference in the below link.
https://dotnetcorecommunity.blogspot.com/2019/11/generic-repository-pattern-with.html
Now, We are going to discuss a topic related to Authentication and Authorization which is the most important topic from the security end , First lets discuss these two terminologies.
- Authentication
- Authorization
Authentication :
It is a process of deciding if, in fact someone or something claims to be .
In this process a user claims himself as a part of that system by providing credentials to the system, if credentials are valid ,then he is allowed to enter the system.
Example :
When I came to the office this morning, I was needed to provide my Identity to the Door Entrance Attendance System, I put my finger on thumb scanner, machine scanned my thumb print and identified me as an employee or part of this office and opened the door for me or allowed me to enter the system. If any visitor or external person tries to do this, He/She won't be able to enter the system, because he isn't authenticated by the system.
Authorization :
It is a process of allowing someone to do something or have something.It is the next level of security after authentication.In this process we check the allowed user role and rights. In any system we do not allow user to use all the resources , we keep him in his limited or allowed resources which is good for security from every perspective .
Example :
After entering into the office, I am only allowed to sit in my cabin, when I am allowed or authenticated by the entrance security, It does not mean that I can go anywhere in the office, I can use any resources like Boss Laptop, Hr Cupboard etc. I am only allowed to sit on chair and has only access to my Laptop, This process identifies my role as a developer.
Now, Lets come to the practical:
Step# 1:
Create an asp.net core web project in visual studio 2017/ 2019 using .net core 2.2
Create an asp.net core web project in visual studio 2017/ 2019 using .net core 2.2
Step# 2:
Add an class of database connection named as "AppDbContext",install the EntityFramework core by using the package Manager Console.
Add an class of database connection named as "AppDbContext",install the EntityFramework core by using the package Manager Console.
link References :
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
namespace AspCoreIdentity.Models
{
public class AppDbContext: IdentityDbContext
{
public AppDbContext(DbContextOptions<AppDbContext> options) : base(options)
{
}
}
}
Step# 3:
Now go to the appSetting.json File and add Connectionstring as shown below.
Step# 4:
Go to the Startup.cs file and add the middle-ware to configure ConfigureServices method.
Step# 5:
open the Package Manager Console, Run the Migration and Update-database command,
Commands:
Step# 6:
Open the Sql server Management Studio and check the database that you have created, the name which you placed in connectionstring of database will be the created database for you . Run the below queries in Sql server management studio.
Conclusion :
that's all for this blog, we have successfully Implement the Asp.net core Identity,In the next coming articles we will discuss more details on this topic.Thank you for watching,Have a great day !
Subscribe to my youtube :
https://www.youtube.com/channel/UCHAmv9m1l_BqbiPFvwWv1aw
Now go to the appSetting.json File and add Connectionstring as shown below.
Step# 4:
Go to the Startup.cs file and add the middle-ware to configure ConfigureServices method.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using AspCoreIdentity.Models;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
namespace AspCoreIdentity
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddControllersWithViews();
services.AddDbContextPool<AppDbContext>(
options => options.UseSqlServer(Configuration.GetConnectionString("DBConnection")));
services.AddIdentity<IdentityUser, IdentityRole>()
.AddEntityFrameworkStores<AppDbContext>();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
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.UseAuthentication();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
});
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using AspCoreIdentity.Models;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
namespace AspCoreIdentity
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddControllersWithViews();
services.AddDbContextPool<AppDbContext>(
options => options.UseSqlServer(Configuration.GetConnectionString("DBConnection")));
services.AddIdentity<IdentityUser, IdentityRole>()
.AddEntityFrameworkStores<AppDbContext>();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
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.UseAuthentication();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
});
}
}
}
Step# 5:
open the Package Manager Console, Run the Migration and Update-database command,
Commands:
- add-migration initial
- Update-database
Step# 6:
Open the Sql server Management Studio and check the database that you have created, the name which you placed in connectionstring of database will be the created database for you . Run the below queries in Sql server management studio.
use coreIdentity
select * from sys.tables
select * from sys.tables
Conclusion :
that's all for this blog, we have successfully Implement the Asp.net core Identity,In the next coming articles we will discuss more details on this topic.Thank you for watching,Have a great day !
Subscribe to my youtube :
https://www.youtube.com/channel/UCHAmv9m1l_BqbiPFvwWv1aw
0 Comments:
Post a Comment
Do not Add Spam links in the Comment Box
Subscribe to Post Comments [Atom]
<< Home