This commit is contained in:
damienbod
2024-01-07 05:22:06 +01:00
parent e4c35a5d9d
commit b01f2722f3
22 changed files with 62 additions and 86 deletions

View File

@@ -1,6 +1,4 @@
using Microsoft.AspNetCore.Hosting;
[assembly: HostingStartup(typeof(AspNetCoreIdentityFido2Mfa.Areas.Identity.IdentityHostingStartup))]
[assembly: HostingStartup(typeof(AspNetCoreIdentityFido2Mfa.Areas.Identity.IdentityHostingStartup))]
namespace AspNetCoreIdentityFido2Mfa.Areas.Identity;
public class IdentityHostingStartup : IHostingStartup

View File

@@ -2,19 +2,12 @@
// The .NET Foundation licenses this file to you under the MIT license.
#nullable disable
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Fido2Identity;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Identity.UI.Services;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.Extensions.Logging;
using Fido2Identity;
using System.ComponentModel.DataAnnotations;
namespace AspNetCoreIdentityFido2Mfa.Areas.Identity.Pages.Account
{

View File

@@ -2,13 +2,10 @@
// The .NET Foundation licenses this file to you under the MIT license.
#nullable disable
using System;
using System.Threading.Tasks;
using Fido2Identity;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.Extensions.Logging;
namespace AspNetCoreIdentityFido2Mfa.Areas.Identity.Pages.Account.Manage
{

View File

@@ -2,10 +2,9 @@
// The .NET Foundation licenses this file to you under the MIT license.
#nullable disable
using System;
using Microsoft.AspNetCore.Mvc.Rendering;
namespace AspNetCoreIdentityFido2Mfa.Areas.Identity.Pages.Account.Manage
namespace AspNetCoreIdentityFido2Mfa.Areas.Identity.Pages.Account.Manage
{
/// <summary>
/// This API supports the ASP.NET Core Identity default UI infrastructure and is not intended to be used

View File

@@ -2,10 +2,10 @@
// The .NET Foundation licenses this file to you under the MIT license.
#nullable disable
using System.Diagnostics;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using System.Diagnostics;
namespace AspNetCoreIdentityFido2Mfa.Areas.Identity.Pages
{

View File

@@ -61,7 +61,7 @@ public class Fido2Store
var cred = await _applicationDbContext.FidoStoredCredential
.Where(c => c.DescriptorJson != null && c.DescriptorJson.Contains(credentialIdString)).FirstOrDefaultAsync();
if(cred != null)
if (cred != null)
{
cred.SignatureCounter = counter;
await _applicationDbContext.SaveChangesAsync();
@@ -101,7 +101,7 @@ public class Fido2Store
public static byte[] GetUserNameInBytes(string? userName)
{
if(userName != null)
if (userName != null)
{
return Encoding.UTF8.GetBytes(userName);
}

View File

@@ -19,7 +19,7 @@ public class FidoStoredCredential
/// </summary>
public virtual string? UserName { get; set; }
public virtual byte[]? UserId { get; set; }
public virtual byte[]? UserId { get; set; }
/// <summary>
/// Gets or sets the public key for this user.
@@ -34,7 +34,7 @@ public class FidoStoredCredential
public virtual uint SignatureCounter { get; set; }
public virtual string? CredType { get; set; }
/// <summary>
/// Gets or sets the registration date for this user.
/// </summary>

View File

@@ -1,10 +1,10 @@
using System.Text;
using Fido2NetLib;
using Fido2NetLib.Objects;
using Fido2NetLib;
using Microsoft.AspNetCore.Mvc;
using static Fido2NetLib.Fido2;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using System.Text;
using static Fido2NetLib.Fido2;
namespace Fido2Identity;
@@ -63,7 +63,7 @@ public class MfaFido2RegisterController : Controller
var existingKeys = new List<PublicKeyCredentialDescriptor>();
if (identityUser.UserName != null)
{
var items = await _fido2Store.GetCredentialsByUserNameAsync(identityUser.UserName);
var items = await _fido2Store.GetCredentialsByUserNameAsync(identityUser.UserName);
foreach (var publicKeyCredentialDescriptor in items)
{
if (publicKeyCredentialDescriptor.Descriptor != null)
@@ -82,13 +82,13 @@ public class MfaFido2RegisterController : Controller
authenticatorSelection.AuthenticatorAttachment = authType.ToEnum<AuthenticatorAttachment>();
var exts = new AuthenticationExtensionsClientInputs
{
Extensions = true,
UserVerificationMethod = true,
{
Extensions = true,
UserVerificationMethod = true,
};
var options = _lib.RequestNewCredential(
user, existingKeys,
user, existingKeys,
authenticatorSelection, attType.ToEnum<AttestationConveyancePreference>(), exts);
// 4. Temporarily store options, session/in-memory cache/redis/db
@@ -126,7 +126,7 @@ public class MfaFido2RegisterController : Controller
// 2. Verify and make the credentials
var success = await _lib.MakeNewCredentialAsync(attestationResponse, options, callback);
if(success.Result != null)
if (success.Result != null)
{
// 3. Store the credentials in db
await _fido2Store.AddCredentialToUserAsync(options.User, new FidoStoredCredential
@@ -148,7 +148,7 @@ public class MfaFido2RegisterController : Controller
var user = await _userManager.GetUserAsync(User);
if (user == null)
{
return Json(new CredentialMakeResult("error",
return Json(new CredentialMakeResult("error",
$"Unable to load user with ID '{_userManager.GetUserId(User)}'.",
success.Result));
}

View File

@@ -1,9 +1,9 @@
using System.Text;
using Fido2NetLib;
using Fido2NetLib.Objects;
using Fido2NetLib;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using System.Text;
namespace Fido2Identity;

View File

@@ -1,10 +1,10 @@
using System.Text;
using Fido2NetLib;
using Fido2NetLib.Objects;
using Fido2NetLib;
using Microsoft.AspNetCore.Mvc;
using static Fido2NetLib.Fido2;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using System.Text;
using static Fido2NetLib.Fido2;
namespace Fido2Identity;

View File

@@ -1,9 +1,9 @@
using System.Text;
using Fido2NetLib;
using Fido2NetLib.Objects;
using Fido2NetLib;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using System.Text;
namespace Fido2Identity;

View File

@@ -1,5 +1,4 @@
using System;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Migrations;
namespace AspNetCoreIdentityFido2Mfa.Migrations
{

View File

@@ -1,8 +1,8 @@
using AspNetCoreIdentityFido2Mfa.Data;
using Fido2Identity;
using Fido2NetLib;
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
using Fido2NetLib;
using Fido2Identity;
var builder = WebApplication.CreateBuilder(args);
var connectionString = builder.Configuration.GetConnectionString("DefaultConnection") ?? throw new InvalidOperationException("Connection string 'ApplicationDbContextConnection' not found.");

View File

@@ -1,13 +1,4 @@
using System;
using AspNetCoreIdentityFido2Passwordless.Data;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Identity.UI;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
[assembly: HostingStartup(typeof(AspNetCoreIdentityFido2Passwordless.Areas.Identity.IdentityHostingStartup))]
[assembly: HostingStartup(typeof(AspNetCoreIdentityFido2Passwordless.Areas.Identity.IdentityHostingStartup))]
namespace AspNetCoreIdentityFido2Passwordless.Areas.Identity
{
public class IdentityHostingStartup : IHostingStartup

View File

@@ -61,7 +61,7 @@ public class Fido2Store
var cred = await _applicationDbContext.FidoStoredCredential
.Where(c => c.DescriptorJson != null && c.DescriptorJson.Contains(credentialIdString)).FirstOrDefaultAsync();
if(cred != null)
if (cred != null)
{
cred.SignatureCounter = counter;
await _applicationDbContext.SaveChangesAsync();
@@ -101,7 +101,7 @@ public class Fido2Store
public static byte[] GetUserNameInBytes(string? userName)
{
if(userName != null)
if (userName != null)
{
return Encoding.UTF8.GetBytes(userName);
}

View File

@@ -19,7 +19,7 @@ public class FidoStoredCredential
/// </summary>
public virtual string? UserName { get; set; }
public virtual byte[]? UserId { get; set; }
public virtual byte[]? UserId { get; set; }
/// <summary>
/// Gets or sets the public key for this user.
@@ -34,7 +34,7 @@ public class FidoStoredCredential
public virtual uint SignatureCounter { get; set; }
public virtual string? CredType { get; set; }
/// <summary>
/// Gets or sets the registration date for this user.
/// </summary>

View File

@@ -1,10 +1,10 @@
using System.Text;
using Fido2NetLib;
using Fido2NetLib.Objects;
using Fido2NetLib;
using Microsoft.AspNetCore.Mvc;
using static Fido2NetLib.Fido2;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using System.Text;
using static Fido2NetLib.Fido2;
namespace Fido2Identity;
@@ -63,7 +63,7 @@ public class MfaFido2RegisterController : Controller
var existingKeys = new List<PublicKeyCredentialDescriptor>();
if (identityUser.UserName != null)
{
var items = await _fido2Store.GetCredentialsByUserNameAsync(identityUser.UserName);
var items = await _fido2Store.GetCredentialsByUserNameAsync(identityUser.UserName);
foreach (var publicKeyCredentialDescriptor in items)
{
if (publicKeyCredentialDescriptor.Descriptor != null)
@@ -82,13 +82,13 @@ public class MfaFido2RegisterController : Controller
authenticatorSelection.AuthenticatorAttachment = authType.ToEnum<AuthenticatorAttachment>();
var exts = new AuthenticationExtensionsClientInputs
{
Extensions = true,
UserVerificationMethod = true,
{
Extensions = true,
UserVerificationMethod = true,
};
var options = _lib.RequestNewCredential(
user, existingKeys,
user, existingKeys,
authenticatorSelection, attType.ToEnum<AttestationConveyancePreference>(), exts);
// 4. Temporarily store options, session/in-memory cache/redis/db
@@ -126,7 +126,7 @@ public class MfaFido2RegisterController : Controller
// 2. Verify and make the credentials
var success = await _lib.MakeNewCredentialAsync(attestationResponse, options, callback);
if(success.Result != null)
if (success.Result != null)
{
// 3. Store the credentials in db
await _fido2Store.AddCredentialToUserAsync(options.User, new FidoStoredCredential
@@ -148,7 +148,7 @@ public class MfaFido2RegisterController : Controller
var user = await _userManager.GetUserAsync(User);
if (user == null)
{
return Json(new CredentialMakeResult("error",
return Json(new CredentialMakeResult("error",
$"Unable to load user with ID '{_userManager.GetUserId(User)}'.",
success.Result));
}

View File

@@ -1,9 +1,9 @@
using System.Text;
using Fido2NetLib;
using Fido2NetLib.Objects;
using Fido2NetLib;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using System.Text;
namespace Fido2Identity;

View File

@@ -1,10 +1,10 @@
using System.Text;
using Fido2NetLib;
using Fido2NetLib.Objects;
using Fido2NetLib;
using Microsoft.AspNetCore.Mvc;
using static Fido2NetLib.Fido2;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using System.Text;
using static Fido2NetLib.Fido2;
namespace Fido2Identity;

View File

@@ -1,9 +1,9 @@
using System.Text;
using Fido2NetLib;
using Fido2NetLib.Objects;
using Fido2NetLib;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using System.Text;
namespace Fido2Identity;

View File

@@ -1,5 +1,4 @@
using System;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Migrations;
namespace AspNetCoreIdentityFido2Passwordless.Migrations
{

View File

@@ -1,8 +1,8 @@
using AspNetCoreIdentityFido2Passwordless.Data;
using Fido2Identity;
using Fido2NetLib;
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
using Fido2NetLib;
using Fido2Identity;
var builder = WebApplication.CreateBuilder(args);
var connectionString = builder.Configuration.GetConnectionString("DefaultConnection") ?? throw new InvalidOperationException("Connection string 'ApplicationDbContextConnection' not found.");