Alice  Klocko

Alice Klocko


Integrate ASP.NET Core Identity with Microsoft Account

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)


(options => options.UseSqlServer(


            .AddMicrosoftAccount(o =>
                o.ClientId = Configuration
                o.ClientSecret = Configuration

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 : 
    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> 
    private readonly UserManager<IdentityUser> 
    private readonly RoleManager<IdentityRole> 

    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

What is GEEK

Buddha Community

Integrate ASP.NET Core Identity with Microsoft Account
Einar  Hintz

Einar Hintz


jQuery Ajax CRUD in ASP.NET Core MVC with Modal Popup

In this article, we’ll discuss how to use jQuery Ajax for ASP.NET Core MVC CRUD Operations using Bootstrap Modal. With jQuery Ajax, we can make HTTP request to controller action methods without reloading the entire page, like a single page application.

To demonstrate CRUD operations – insert, update, delete and retrieve, the project will be dealing with details of a normal bank transaction. GitHub repository for this demo project :

Sub-topics discussed :

  • Form design for insert and update operation.
  • Display forms in modal popup dialog.
  • Form post using jQuery Ajax.
  • Implement MVC CRUD operations with jQuery Ajax.
  • Loading spinner in .NET Core MVC.
  • Prevent direct access to MVC action method.

Create ASP.NET Core MVC Project

In Visual Studio 2019, Go to File > New > Project (Ctrl + Shift + N).

From new project window, Select Asp.Net Core Web Application_._

Image showing how to create ASP.NET Core Web API project in Visual Studio.

Once you provide the project name and location. Select Web Application(Model-View-Controller) and uncheck HTTPS Configuration. Above steps will create a brand new ASP.NET Core MVC project.

Showing project template selection for .NET Core MVC.

Setup a Database

Let’s create a database for this application using Entity Framework Core. For that we’ve to install corresponding NuGet Packages. Right click on project from solution explorer, select Manage NuGet Packages_,_ From browse tab, install following 3 packages.

Showing list of NuGet Packages for Entity Framework Core

Now let’s define DB model class file – /Models/TransactionModel.cs.

public class TransactionModel
    public int TransactionId { get; set; }

    [Column(TypeName ="nvarchar(12)")]
    [DisplayName("Account Number")]
    [Required(ErrorMessage ="This Field is required.")]
    [MaxLength(12,ErrorMessage ="Maximum 12 characters only")]
    public string AccountNumber { get; set; }

    [Column(TypeName ="nvarchar(100)")]
    [DisplayName("Beneficiary Name")]
    [Required(ErrorMessage = "This Field is required.")]
    public string BeneficiaryName { get; set; }

    [Column(TypeName ="nvarchar(100)")]
    [DisplayName("Bank Name")]
    [Required(ErrorMessage = "This Field is required.")]
    public string BankName { get; set; }

    [Column(TypeName ="nvarchar(11)")]
    [DisplayName("SWIFT Code")]
    [Required(ErrorMessage = "This Field is required.")]
    public string SWIFTCode { get; set; }

    [Required(ErrorMessage = "This Field is required.")]
    public int Amount { get; set; }

    [DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}")]
    public DateTime Date { get; set; }


Here we’ve defined model properties for the transaction with proper validation. Now let’s define  DbContextclass for EF Core. core article core #add loading spinner in core core crud without reloading core jquery ajax form core modal dialog core mvc crud using jquery ajax core mvc with jquery and ajax core popup window #bootstrap modal popup in core mvc. bootstrap modal popup in core #delete and viewall in core #jquery ajax - insert #jquery ajax form post #modal popup dialog in core #no direct access action method #update #validation in modal popup

Einar  Hintz

Einar Hintz


MVC User Registration & Login with ASP.NET Core Identity

User registration and authentication are mandatory in any application when you have little concern about privacy. Hence all most all application development starts with an authentication module. In this article, we will discuss the quickest way to use **ASP.NET Core Identity for User Login and Registration **in a new or existing MVC application.

Sub-topics discussed :

  • How to add ASP.NET Core Identity to MVC application.
  • Customize ASP.NET Core Identity.
  • Identity.UI Design Customization.
  • Next step.


ASP.NET Core Identity is an API, which provides both user interface(UI) and functions for user authentication, registration, authorization, etc. Modules/ APIs like this will really be helpful and fasten the development process. It comes with ASP.NET Core Framework and used in many applications before. Which makes the API more dependable and trustworthy.

ASP.NET Core MVC with user authentication can easily be accomplished using Identity.UI. While creating the MVC project, you just need to select Authentication as Individual User Accounts.

Showing how to create an MVC application with ASP.NET Core Identity API

The rest will be handled by ASP.NET Core Identity UI. It already contains razor view pages and backend codes for an authentication system. But that’s not what we want in most of the cases. we want to customize ASP.NET Core Identity as per our requirement. That’s what we do here.

Create an ASP.NET Core MVC Project

First of all, I will create a brand new ASP.NET Core MVC application without any authentication selected. We could add ASP.NET Core Identity later into the project.

In Visual Studio 2019, Go to File > New > Project (Ctrl + Shift + N). From new project window, select ASP.NET Core Web Application.

Create an ASP.NET Core Web application

Once you provide the project name and location. A new window will be opened as follows, Select _Web Application(Model-View-Controller), _uncheck _HTTPS Configuration _and DO NOT select any authentication method. Above steps will create a brand new ASP.NET Core MVC project.

Select Model View Controller templet under .NET Core core article core #add core identity to existing project core identity in mvc core mvc login and registration #login and logout in core

Lockout user core identity core core mvc #Core core tutorials core with entity framework

Edit identity user in asp net core core core mvc #Core core tutorials core with entity framework

Delete identity user in asp net core core core mvc #Core core tutorials core with entity framework