In the previous article you learned to use Microsoft Account as an external login to your ASP.NET Core web apps. In that article you didn’t use ASP.NET Core Identity in any way. The Microsoft Account alone was used in the authentication process without any local account. At times you may want to integrate the Microsoft account with a local account.

Suppose you want to implement role based security in your web app. The authentication will happen using Microsoft account but you further want to grant access based on roles that are local to the web app. In such cases, you are integrate ASP.NET Code Identity with Microsoft account. The application roles and user to role mappings will reside in the ASP.NET Core Identity. Upon successful sign in with Microsoft account you will create a local account that links to the Microsoft account. The local account will have necessary role information. Once you sign in using Microsoft account the role based security is provided by ASP.NET Core Identity.

In this article you will modify the previous example to use ASP.NET Core Identity. So, make sure you have previous code ready in Visual Studio. I am also going to assume that you have the necessary tables ready in a SQL Server database used by ASP.NET Core Identity.

Let’s get started!

Open Startup class and modify the ConfigureServices() method as shown below:

public void ConfigureServices(
IServiceCollection services)
{

    services.AddControllersWithViews();
    services.AddRazorPages();

    services.AddDbContext<ApplicationDbContext>
(options => options.UseSqlServer(
            Configuration.GetConnectionString
("DefaultConnection")));
    services.AddDatabaseDeveloperPageExceptionFilter();

    services.AddIdentity<IdentityUser,IdentityRole>()
            .AddEntityFrameworkStores<ApplicationDbContext>();

    services.AddAuthentication()
            .AddMicrosoftAccount(o =>
            {
                o.ClientId = Configuration
["Authentication:ClientId"];
                o.ClientSecret = Configuration
["Authentication:ClientSecret"];
            });
}

In this code, we first registered the ApplicationDbContext that is responsible for data access required by ASP.NET Core Identity. The ApplicationDbContext class looks like this:

public class ApplicationDbContext : 
IdentityDbContext
{
    public ApplicationDbContext
(DbContextOptions<ApplicationDbContext> options)
        : base(options)
    {
    }
}

The DefaultConnection is the database connection string stored in appsettings.json file:

"ConnectionStrings": {
    "DefaultConnection": "data source=.;initial 
catalog=northwind111;integrated security=true"
}

Make sure to modify the connection string as per your database environment.

Then the code calls AddIdentity() and AddEntityFrameworkStores() methods. These methods use IdentityUser, IdentityRole, and ApplicationDbContext classes. The IdentityUser and IdentityRole classes are provided by ASP.NET Core Identity itself.

Then we call AddAuthentication() and AddMicrosoftAccount() methods. If you followed the earlier article, these calls should look familiar to you.

Next, open the HomeController and add a constructor to it as shown below:

public class HomeController : Controller
{
    private readonly SignInManager<IdentityUser> 
signInManager;
    private readonly UserManager<IdentityUser> 
userManager;
    private readonly RoleManager<IdentityRole> 
roleManager;

    public HomeController(
        SignInManager<IdentityUser> signInManager,
        UserManager<IdentityUser> userManager,
        RoleManager<IdentityRole> roleManager)
    {
        this.signInManager = signInManager;
        this.userManager = userManager;
        this.roleManager = roleManager;
    }
    ...
    ...
}

We declare a few member variables inside the HomeController class - SignInManager, UserManager, and RoleManager. These objects are injected in the constructor and are used by the code we are going to write soon.

#aspdotnet #dotnet #programming #developer #microsoft

Integrate ASP.NET Core Identity with Microsoft Account
5.60 GEEK