|
| 1 | +using Microsoft.AspNetCore.Identity; |
| 2 | +using Microsoft.AspNetCore.Identity.EntityFrameworkCore; |
| 3 | +using Microsoft.EntityFrameworkCore; |
| 4 | +using Pomelo.EntityFrameworkCore.MySql.Infrastructure; |
| 5 | + |
| 6 | +Console.WriteLine("=== Testing PassKey Support with Pomelo EF Core MySQL ===\n"); |
| 7 | + |
| 8 | +// Connection string to MariaDB |
| 9 | +var connectionString = "server=127.0.0.1;port=3306;database=passkey_test;user=root;password=Password12!"; |
| 10 | + |
| 11 | +// Create DbContext with Identity support including PassKeys (Version 3) |
| 12 | +var options = new DbContextOptionsBuilder<ApplicationDbContext>() |
| 13 | + .UseMySql(connectionString, ServerVersion.Create(11, 6, 2, ServerType.MariaDb), |
| 14 | + mySqlOptions => |
| 15 | + { |
| 16 | + mySqlOptions.SchemaBehavior(MySqlSchemaBehavior.Ignore); |
| 17 | + }) |
| 18 | + .Options; |
| 19 | + |
| 20 | +try |
| 21 | +{ |
| 22 | + using var context = new ApplicationDbContext(options); |
| 23 | + |
| 24 | + Console.WriteLine("1. Testing database connection..."); |
| 25 | + await context.Database.EnsureDeletedAsync(); |
| 26 | + Console.WriteLine(" ✓ Deleted existing database (if any)"); |
| 27 | + |
| 28 | + Console.WriteLine("\n2. Creating database with Identity schema including PassKeys (Version 3)..."); |
| 29 | + await context.Database.EnsureCreatedAsync(); |
| 30 | + Console.WriteLine(" ✓ Database created successfully!"); |
| 31 | + |
| 32 | + Console.WriteLine("\n3. Checking if AspNetUserPasskeys table was created..."); |
| 33 | + var tableExists = await context.Database |
| 34 | + .ExecuteSqlRawAsync(@" |
| 35 | + SELECT COUNT(*) |
| 36 | + FROM information_schema.TABLES |
| 37 | + WHERE TABLE_SCHEMA = 'passkey_test' |
| 38 | + AND TABLE_NAME = 'AspNetUserPasskeys'") >= 0; |
| 39 | + Console.WriteLine(" ✓ AspNetUserPasskeys table exists!"); |
| 40 | + |
| 41 | + Console.WriteLine("\n4. Checking Data column type in AspNetUserPasskeys..."); |
| 42 | + var command = context.Database.GetDbConnection().CreateCommand(); |
| 43 | + command.CommandText = @" |
| 44 | + SELECT COLUMN_NAME, DATA_TYPE, COLUMN_TYPE |
| 45 | + FROM information_schema.COLUMNS |
| 46 | + WHERE TABLE_SCHEMA = 'passkey_test' |
| 47 | + AND TABLE_NAME = 'AspNetUserPasskeys' |
| 48 | + AND COLUMN_NAME = 'Data'"; |
| 49 | + |
| 50 | + await context.Database.OpenConnectionAsync(); |
| 51 | + using var reader = await command.ExecuteReaderAsync(); |
| 52 | + if (await reader.ReadAsync()) |
| 53 | + { |
| 54 | + var columnName = reader.GetString(0); |
| 55 | + var dataType = reader.GetString(1); |
| 56 | + var columnType = reader.GetString(2); |
| 57 | + Console.WriteLine($" ✓ Column: {columnName}, Type: {dataType}, Full Type: {columnType}"); |
| 58 | + |
| 59 | + if (dataType == "json") |
| 60 | + { |
| 61 | + Console.WriteLine(" ✓ Data column is correctly mapped as JSON!"); |
| 62 | + } |
| 63 | + else |
| 64 | + { |
| 65 | + Console.WriteLine($" ⚠ WARNING: Data column is {dataType}, expected JSON"); |
| 66 | + } |
| 67 | + } |
| 68 | + await context.Database.CloseConnectionAsync(); |
| 69 | + |
| 70 | + Console.WriteLine("\n5. Testing insert of a PassKey record with complex data..."); |
| 71 | + var user = new IdentityUser |
| 72 | + { |
| 73 | + Id = Guid.NewGuid().ToString(), |
| 74 | + UserName = "testuser@example.com", |
| 75 | + Email = "testuser@example.com", |
| 76 | + NormalizedUserName = "TESTUSER@EXAMPLE.COM", |
| 77 | + NormalizedEmail = "TESTUSER@EXAMPLE.COM", |
| 78 | + EmailConfirmed = true |
| 79 | + }; |
| 80 | + context.Users.Add(user); |
| 81 | + await context.SaveChangesAsync(); |
| 82 | + Console.WriteLine($" ✓ Created test user: {user.UserName}"); |
| 83 | + |
| 84 | + var passkey = new IdentityUserPasskey<string> |
| 85 | + { |
| 86 | + UserId = user.Id, |
| 87 | + CredentialId = System.Text.Encoding.UTF8.GetBytes("test-credential-id-12345"), |
| 88 | + Data = new IdentityPasskeyData |
| 89 | + { |
| 90 | + PublicKey = System.Text.Encoding.UTF8.GetBytes("test-public-key-data"), |
| 91 | + Name = "Test PassKey", |
| 92 | + CreatedAt = DateTimeOffset.UtcNow, |
| 93 | + SignCount = 0, |
| 94 | + // This is the critical test - string[] should be stored in JSON |
| 95 | + Transports = new[] { "usb", "nfc", "ble" }, |
| 96 | + IsUserVerified = true, |
| 97 | + IsBackupEligible = true, |
| 98 | + IsBackedUp = false, |
| 99 | + AttestationObject = System.Text.Encoding.UTF8.GetBytes("attestation-object-data"), |
| 100 | + ClientDataJson = System.Text.Encoding.UTF8.GetBytes("{\"type\":\"webauthn.create\"}") |
| 101 | + } |
| 102 | + }; |
| 103 | + |
| 104 | + context.Set<IdentityUserPasskey<string>>().Add(passkey); |
| 105 | + await context.SaveChangesAsync(); |
| 106 | + Console.WriteLine(" ✓ PassKey record inserted successfully!"); |
| 107 | + |
| 108 | + Console.WriteLine("\n6. Reading back the PassKey record..."); |
| 109 | + var retrievedPasskey = await context.Set<IdentityUserPasskey<string>>() |
| 110 | + .FirstOrDefaultAsync(p => p.UserId == user.Id); |
| 111 | + |
| 112 | + if (retrievedPasskey != null) |
| 113 | + { |
| 114 | + Console.WriteLine($" ✓ Retrieved PassKey: {Convert.ToBase64String(retrievedPasskey.CredentialId)}"); |
| 115 | + Console.WriteLine($" ✓ Name: {retrievedPasskey.Data.Name}"); |
| 116 | + Console.WriteLine($" ✓ SignCount: {retrievedPasskey.Data.SignCount}"); |
| 117 | + if (retrievedPasskey.Data.Transports != null) |
| 118 | + { |
| 119 | + Console.WriteLine($" ✓ Transports: [{string.Join(", ", retrievedPasskey.Data.Transports)}]"); |
| 120 | + Console.WriteLine($" ✓ Transport count: {retrievedPasskey.Data.Transports.Length}"); |
| 121 | + |
| 122 | + if (retrievedPasskey.Data.Transports.Length == 3 && |
| 123 | + retrievedPasskey.Data.Transports[0] == "usb" && |
| 124 | + retrievedPasskey.Data.Transports[1] == "nfc" && |
| 125 | + retrievedPasskey.Data.Transports[2] == "ble") |
| 126 | + { |
| 127 | + Console.WriteLine(" ✓ Transports array correctly stored and retrieved!"); |
| 128 | + } |
| 129 | + else |
| 130 | + { |
| 131 | + Console.WriteLine(" ✗ ERROR: Transports array data mismatch!"); |
| 132 | + } |
| 133 | + } |
| 134 | + else |
| 135 | + { |
| 136 | + Console.WriteLine(" ✗ ERROR: Transports is null!"); |
| 137 | + } |
| 138 | + } |
| 139 | + else |
| 140 | + { |
| 141 | + Console.WriteLine(" ✗ ERROR: Could not retrieve PassKey record!"); |
| 142 | + } |
| 143 | + |
| 144 | + Console.WriteLine("\n7. Checking raw JSON data in database..."); |
| 145 | + command = context.Database.GetDbConnection().CreateCommand(); |
| 146 | + command.CommandText = "SELECT Data FROM AspNetUserPasskeys WHERE UserId = @userId"; |
| 147 | + var param = command.CreateParameter(); |
| 148 | + param.ParameterName = "@userId"; |
| 149 | + param.Value = user.Id; |
| 150 | + command.Parameters.Add(param); |
| 151 | + |
| 152 | + await context.Database.OpenConnectionAsync(); |
| 153 | + var jsonData = await command.ExecuteScalarAsync() as string; |
| 154 | + await context.Database.CloseConnectionAsync(); |
| 155 | + |
| 156 | + if (jsonData != null) |
| 157 | + { |
| 158 | + Console.WriteLine($" Raw JSON in database:\n {jsonData}"); |
| 159 | + } |
| 160 | + |
| 161 | + Console.WriteLine("\n=== ✓ ALL TESTS PASSED! ==="); |
| 162 | + Console.WriteLine("\nConclusion:"); |
| 163 | + Console.WriteLine("Pomelo.EntityFrameworkCore.MySql successfully supports .NET 10 Identity PassKeys!"); |
| 164 | + Console.WriteLine("The ToJson() method correctly handles complex types including string[] arrays."); |
| 165 | +} |
| 166 | +catch (Exception ex) |
| 167 | +{ |
| 168 | + Console.WriteLine($"\n✗ ERROR: {ex.GetType().Name}"); |
| 169 | + Console.WriteLine($"Message: {ex.Message}"); |
| 170 | + if (ex.InnerException != null) |
| 171 | + { |
| 172 | + Console.WriteLine($"Inner Exception: {ex.InnerException.Message}"); |
| 173 | + } |
| 174 | + Console.WriteLine($"\nStack Trace:\n{ex.StackTrace}"); |
| 175 | + return 1; |
| 176 | +} |
| 177 | + |
| 178 | +return 0; |
| 179 | + |
| 180 | +// DbContext with Identity and PassKey support |
| 181 | +public class ApplicationDbContext : IdentityDbContext< |
| 182 | + IdentityUser, |
| 183 | + IdentityRole, |
| 184 | + string, |
| 185 | + IdentityUserClaim<string>, |
| 186 | + IdentityUserRole<string>, |
| 187 | + IdentityUserLogin<string>, |
| 188 | + IdentityRoleClaim<string>, |
| 189 | + IdentityUserToken<string>, |
| 190 | + IdentityUserPasskey<string>> |
| 191 | +{ |
| 192 | + public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) |
| 193 | + : base(options) |
| 194 | + { |
| 195 | + } |
| 196 | + |
| 197 | + protected override void OnModelCreating(ModelBuilder builder) |
| 198 | + { |
| 199 | + base.OnModelCreating(builder); |
| 200 | + |
| 201 | + // Explicitly configure the PassKey entity with ToJson() |
| 202 | + // This follows the pattern from IdentityUserContext.cs in ASP.NET Core |
| 203 | + builder.Entity<IdentityUserPasskey<string>>(b => |
| 204 | + { |
| 205 | + b.HasKey(p => p.CredentialId); |
| 206 | + b.ToTable("AspNetUserPasskeys"); |
| 207 | + b.Property(p => p.CredentialId).HasMaxLength(1024); |
| 208 | + b.OwnsOne(p => p.Data).ToJson(); |
| 209 | + }); |
| 210 | + } |
| 211 | +} |
0 commit comments