Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net10.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<UserSecretsId>aspnet-IdentityUserExtension-41039A27-11C0-4BD1-98EF-4FF82DC4AB8A</UserSecretsId>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore" Version="7.0.7" />
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="7.0.7" />
<PackageReference Include="Microsoft.AspNetCore.Identity.UI" Version="7.0.7" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="7.0.7" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="7.0.9" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="7.0.7" />
<PackageReference Include="Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore" Version="10.0.12" />
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="10.0.12" />
<PackageReference Include="Microsoft.AspNetCore.Identity.UI" Version="10.0.12" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="10.0.12" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="10.0.12" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="10.0.12" />
</ItemGroup>

</Project>

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ protected override void BuildModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("ProductVersion", "7.0.9")
.HasAnnotation("ProductVersion", "10.0.12")
.HasAnnotation("Relational:MaxIdentifierLength", 128);

SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace IdentityUserExtension.Models;

public class ApplicationUser : IdentityUser<Guid>
{
public string DisplayName { get; set; }
public required string DisplayName { get; set; }
public DateTime LastLoginDateTime { get; set; }
public List<Post> Posts { get; set; }
public List<Post> Posts { get; set; } = [];
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ namespace IdentityUserExtension.Models;
public class Post
{
public Guid Id { get; set; }
public string Title { get; set; }
public string Text { get; set; }
public required string Title { get; set; }
public required string Text { get; set; }
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
using IdentityUserExtension.Data;
using IdentityUserExtension.Models;
Expand All @@ -11,6 +12,7 @@
builder.Services.AddDatabaseDeveloperPageExceptionFilter();

builder.Services.AddDefaultIdentity<ApplicationUser>(options => options.SignIn.RequireConfirmedAccount = true)
.AddRoles<IdentityRole<Guid>>()
.AddEntityFrameworkStores<ApplicationDbContext>();
builder.Services.AddControllersWithViews();

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System.Security.Claims;
using IdentityUserExtension.Models;
using Microsoft.AspNetCore.Identity;

namespace IdentityUserExtension.Services;

public class UserProfileService
{
private readonly UserManager<ApplicationUser> _userManager;

public UserProfileService(UserManager<ApplicationUser> userManager)
{
_userManager = userManager;
}

public async Task<IdentityResult> RecordSignIn(ClaimsPrincipal principal)
{
var user = await _userManager.GetUserAsync(principal);
if (user is null)
{
return IdentityResult.Failed();
}

user.LastLoginDateTime = DateTime.UtcNow;

return await _userManager.UpdateAsync(user);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using IdentityUserExtension.Data;
using IdentityUserExtension.Models;
using Microsoft.EntityFrameworkCore;

namespace Tests;

public class ApplicationUserModelTest
{
private static ApplicationDbContext CreateContext()
{
var options = new DbContextOptionsBuilder<ApplicationDbContext>()
.UseSqlite("DataSource=:memory:")
.Options;

return new ApplicationDbContext(options);
}

[Test]
public void GivenTheApplicationDbContext_WhenBuildingTheModel_ThenApplicationUserHasAGuidPrimaryKey()
{
using var context = CreateContext();

var keyProperties = context.Model
.FindEntityType(typeof(ApplicationUser))!
.FindPrimaryKey()!
.Properties;

Assert.That(keyProperties, Has.Count.EqualTo(1));
Assert.That(keyProperties[0].ClrType, Is.EqualTo(typeof(Guid)));
}

[Test]
public void GivenTheApplicationDbContext_WhenBuildingTheModel_ThenDisplayNameIsCappedAtOneHundredCharacters()
{
using var context = CreateContext();

var displayName = context.Model
.FindEntityType(typeof(ApplicationUser))!
.FindProperty(nameof(ApplicationUser.DisplayName))!;

Assert.That(displayName.GetMaxLength(), Is.EqualTo(100));
Assert.That(displayName.IsNullable, Is.False);
}

[Test]
public void GivenTheDefaultSchemaVersion_WhenBuildingTheModel_ThenThereIsNoPasskeyTable()
{
using var context = CreateContext();

var tables = context.Model
.GetEntityTypes()
.Select(e => e.GetTableName())
.ToList();

Assert.That(tables, Does.Not.Contain("AspNetUserPasskeys"));
Assert.That(tables, Does.Contain("AspNetUsers"));
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using IdentityUserExtension.Data;
using IdentityUserExtension.Models;
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
Expand All @@ -22,6 +23,7 @@ public static IServiceProvider CreateTestServiceProvider()
services.AddDbContext<ApplicationDbContext>(options => options.UseSqlServer(connectionString));

services.AddDefaultIdentity<ApplicationUser>(options => options.SignIn.RequireConfirmedAccount = true)
.AddRoles<IdentityRole<Guid>>()
.AddEntityFrameworkStores<ApplicationDbContext>();

return services.BuildServiceProvider();
Expand Down
12 changes: 6 additions & 6 deletions aspnetcore-features/IdentityUserExtension/Tests/Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

Expand All @@ -10,11 +10,11 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.5.0" />
<PackageReference Include="NUnit" Version="3.13.3" />
<PackageReference Include="NUnit3TestAdapter" Version="4.4.2" />
<PackageReference Include="NUnit.Analyzers" Version="3.6.1" />
<PackageReference Include="coverlet.collector" Version="3.2.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.10.0" />
<PackageReference Include="NUnit" Version="4.6.1" />
<PackageReference Include="NUnit3TestAdapter" Version="6.3.0" />
<PackageReference Include="NUnit.Analyzers" Version="4.15.0" />
<PackageReference Include="coverlet.collector" Version="10.0.1" />
</ItemGroup>

<ItemGroup>
Expand Down
Loading