Skip to main content

Add Identity as UI in ASP.NET Core 2.1 application

One of the new features of ASP.NET Core 2.1 is, Identity as UI library. Today, while working with Identity, you have to deal with thousands of lines of code and different files. But this new feature saves you from all the hassle of adding and configuring Identity to an ASP.NET Core application. In this post, find out how to add Identity as UI in ASP.NET Core 2.1 application.

Add Identity as UI in ASP.NET Core 2.1 application

Firstly, if you have still not installed .NET Core 2.1 preview, please do the following installation.

First thing first, open Visual Studio “Preview” version, hit Ctrl+Shift+N and select the ASP.NET Core Web Application (.NET Core) project type from the templates. When you click Ok, you will get the following prompt,

ASP.NET Core 2.1 Version Selector

As you can see, both the versions are listed in the drop-down. Select ASP.NET Core 2.1 and choose either Web Application or Web Application(MVC). The Web Application template will create an ASP.NET Core Razor pages based application. Hit Ok and let Visual Studio restores the packages. By default, in the ASP.NET Core 2.1 preview 1 version, the Identity is not added/configured. But adding Identity as UI is simple and straightforward. To add Identity as UI, follow the below given steps.

  • The Identity framework is available in a nuget package. So, you need to install the Microsoft.AspNetCore.Identity.UI nuget package.
    Install-Package Microsoft.AspNetCore.Identity.UI -Version 2.1.0-preview1-final
    
  • Next, we need to add only a single _ViewStart.cshtml file to the Areas->Identity->Pages folder in your project with the following code. These 3 folders are not present, so we need to create them.
    @{
        Layout = "/Pages/_Layout.cshtml";
    }
    

    Now, the solution will look like,

    Adding Identity as UI in ASP.NET Core 2.1 application

  • Next, add the following code to configure Identity UI in Startup.cs -> ConfigureService() method.

    The AddDefaultUI() method does the trick here.

  • Lastly, add the Register and Login links to the homepage of the application. To do that, open _Layout.cshtml located under Pages directory and add the following code after the navigation bar code.

That’s it. Run the application and you should see the following.

AspNetCore_IdentityAsUI

The sample application and the source code is available at Github.

Conclusion

To conclude, the Identity as UI library is a time-saver feature which allows you to include the Identity framework with ease and without dealing with 50 different files and thousand lines of code. If you want to have more control on the default implementation, you can anytime use the new identity scaffolder to add the identity code in your app.

Thank you for reading. Keep visiting this blog and share this in your network. Please put your thoughts and feedback in the comments section.

PS: If you found this content valuable and want to return the favour, then Buy Me A Coffee

16 thoughts on “Add Identity as UI in ASP.NET Core 2.1 application

  1. You’ve got to be kidding me. This really takes it to a new level of intrusion. As if Identity didn’t already go far enough with wanting to manhandle my database, now Microsoft wants to create my login and register pages for me? What is the point in that? Who in their right mind would not want complete control over these important pages?

  2. Hello,
    1) i want to change routing url for identity register and login page but its not working.

    2)
    public class ApplicationUser : IdentityUser
    {
    public string FirstName { get; set; }

    public string MiddleName { get; set; }

    public string LastName { get; set; }
    }

    IdentityHostingStartup.cs
    public void Configure(IWebHostBuilder builder)
    {
    builder.ConfigureServices((context, services) => {
    //services.AddScoped();
    services.AddDbContext(options =>
    options.UseSqlServer(context.Configuration.GetConnectionString(“inventorysystem”)));

    services.AddDefaultIdentity()
    .AddEntityFrameworkStores().AddDefaultTokenProviders();

    services.Configure(options =>
    {
    //Password Configuration
    options.Password.RequireDigit = false;
    options.Password.RequiredLength = 8;
    options.Password.RequireNonAlphanumeric = false;
    options.Password.RequireUppercase = false;
    options.Password.RequireLowercase = false;
    options.Password.RequiredUniqueChars = 0;

    //Lockout Configuration
    options.Lockout.DefaultLockoutTimeSpan = TimeSpan.FromMinutes(30);
    options.Lockout.MaxFailedAccessAttempts = 10;
    options.Lockout.AllowedForNewUsers = true;

    //user Configuration
    options.User.RequireUniqueEmail = true;
    options.User.AllowedUserNameCharacters = “abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-._@”;

    //Login Configuration
    options.SignIn.RequireConfirmedEmail = true;
    options.SignIn.RequireConfirmedPhoneNumber = true;
    });
    });
    }

    After debug getting this error :
    InvalidOperationException: No service for type ‘Microsoft.AspNetCore.Identity.UserManager`1[Microsoft.AspNetCore.Identity.IdentityUser]’ has been registered.

    HOW TO RESOLVE THIS ISSUE?

  3. I want to differentiate admin and user login How can I do that in ASP.NET Core 2.1 in which I can’t find any code of login or register?

  4. Hey thanks for the article. After filling the register form when I click on Register, it is not doing anything. Register is not working at all. What could be the reason?

    1. It works like a charm. As soon as I installed .NET Core 2.1, all the nuget packages were automatically updated to 2.1 version. Make sure your .NET Core 2.1 installation is correct.

  5. A little disappointed… Adding “Individual Account” Authentication generates migration that allows me to build Identity tables in the database. This does not such thing – so I get “Unable to resolve service for type IdentityDbContext” exception when I click Register link. I am sure you had to overcome the same problem; so it would be nice to share that with your readers.
    Also, the way I read it, this only works for Razor Pages project… not for SPA / Web API project. Right?
    Finally, you have a small typo. Areas->Pages->Identity should be Areas->Identity->Pages

    1. Felix,

      Thanks for stopping here and putting your comments. I have updated the post (correcting all the typos). Yes, I am also getting the same error and I think it’s nothing to do with ASP.NET Core 2.1. I remember these migration error also come with ASP.NET projects also when you add identity. Only way to fix it, to apply migration.

      Thanks.

  6. Thank you. I am looking forward to using Identity as a library as announced on Microsoft blog (https://blogs.msdn.microsoft.com/webdev/2018/02/02/asp-net-core-2-1-roadmap/#identity). Unfortunately, it requires Visual Studio 2017 15.7 Preview 1 (https://www.microsoft.com/net/download/dotnet-core/sdk-2.1.300-preview1); not 2017 15.6 as you mistakenly specify here.

    It would be interesting to understand how to modify the library. For example, scaffolded code always used email as login id. I assume that Register and Login links follow this annoying tradition. How time-saving it is compared to changing authentication to Individual Accounts. While it probably generates 50 files and thousand lines of code – I don’t have to touch them unless I need to make changes. What is the difference here?

    1. Hi Felix, there isn’t too much difference. This is the same identity template but bundled into separate area so it can be considered as a reusable library which could be added to the project at any time without need to start it from scratch with the proper template. Without this It will be more work. I still prefer the old way.

Leave a Reply

Your email address will not be published. Required fields are marked *