Extending ASP.NET Core 2.2 Identity Management

Scott Kuhl
Mar 11 · 5 min read

Individual User Accou

Add New Models

using Microsoft.AspNetCore.Identity;

namespace IdentityExample.Models
{
public class AppRole : IdentityRole<int>
{
public AppRole() { }

public AppRole(string name)
{
Name = name;
}
}
}
using Microsoft.AspNetCore.Identity;
using System.ComponentModel.DataAnnotations;

namespace IdentityExample.Models
{
public class AppUser : IdentityUser<int>
{
[PersonalData, Required, StringLength(20)]
public string FirstName { get; set; }

[PersonalData, Required, StringLength(20)]
public string LastName { get; set; }

public string FullName { get { return $"{FirstName} {LastName}"; } }
}
}

Update the Database Context

public class ApplicationDbContext : IdentityDbContext<AppUser, AppRole, int>
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
builder.Entity<AppUser>().Ignore(e => e.FullName);
}

Update Startup

services.AddDefaultIdentity<IdentityUser>()
.AddDefaultUI(UIFramework.Bootstrap4)
.AddEntityFrameworkStores<ApplicationDbContext>();
services.AddIdentity<AppUser, AppRole>()
.AddDefaultUI(UIFramework.Bootstrap4)
.AddEntityFrameworkStores<ApplicationDbContext>().AddDefaultTokenProviders();

Update _LoginPartial.cshtml

@using Microsoft.AspNetCore.Identity
@using IdentityExample.Models
@inject SignInManager<AppUser> SignInManager
@inject UserManager<AppUser> UserManager
Hello @User.Identity.Name!
@{ var user = await UserManager.GetUserAsync(User); }
Hello @(user.FirstName)!

Override Default Identity Pages

_ValidationScriptsPartial.cshtml

Register.cshtml.cs

[Required, DataType(DataType.Text), Display(Name = "First Name")]
public string FirstName { get; set; }

[Required, DataType(DataType.Text), Display(Name = "Last Name")]
public string LastName { get; set; }
var user = new AppUser { UserName = Input.Email, Email = Input.Email, FirstName = Input.FirstName, LastName = Input.LastName };

Register.cshtml

<div class="form-group">
<label asp-for="Input.FirstName"></label>
<input asp-for="Input.FirstName" class="form-control" />
<span asp-validation-for="Input.FirstName" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Input.LastName"></label>
<input asp-for="Input.LastName" class="form-control" />
<span asp-validation-for="Input.LastName" class="text-danger"></span>
</div>

Index.cshtml.cs

[Required, DataType(DataType.Text), Display(Name = "First Name")]
public string FirstName { get; set; }

[Required, DataType(DataType.Text), Display(Name = "Last Name")]
public string LastName { get; set; }
Input = new InputModel
{
Email = email,
PhoneNumber = phoneNumber,
FirstName = user.FirstName,
LastName = user.LastName
};
if (Input.FirstName != user.FirstName) user.FirstName = Input.FirstName;
if (Input.LastName != user.LastName) user.LastName = Input.LastName;
await _userManager.UpdateAsync(user);


await _signInManager.RefreshSignInAsync(user);
StatusMessage = "Your profile has been updated";
return RedirectToPage();

Index.cshtml

<div class="form-group">
<label asp-for="Input.FirstName"></label>
<input asp-for="Input.FirstName" class="form-control" />
<span asp-validation-for="Input.FirstName" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Input.LastName"></label>
<input asp-for="Input.LastName" class="form-control" />
<span asp-validation-for="Input.LastName" class="text-danger"></span>
</div>

Update Migrations

Seeding Test Data

public static class ApplicationDbContextExtensions
{
public static UserManager<AppUser> UserManager { get; set; }

public static void EnsureSeeded(this ApplicationDbContext context)
{
if (UserManager.FindByEmailAsync("scott@identity.local").GetAwaiter().GetResult() == null)
{
var user = new AppUser
{
FirstName = "Scott",
LastName = "Kuhl",
UserName = "scott@identity.local",
Email = "scott@idenity.local",
EmailConfirmed = true,
LockoutEnabled = false
};

UserManager.CreateAsync(user, "P@ssword1").GetAwaiter().GetResult();
}
}
}
ApplicationDbContextExtensions.UserManager = services.BuildServiceProvider().GetService<UserManager<AppUser>>();
public static void Main(string[] args)
{
var host = CreateWebHostBuilder(args).Build();

using (var scope = host.Services.CreateScope())
{
var services = scope.ServiceProvider;
var dbContext =
services.GetRequiredService<ApplicationDbContext>();
dbContext.Database.Migrate();
dbContext.EnsureSeeded();
}

host.Run();
}

Scott Kuhl

Written by

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade