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
1 change: 1 addition & 0 deletions IdentityServer/v8/Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
<PackageVersion Include="OpenTelemetry.Instrumentation.Http" Version="1.17.0" />
<PackageVersion Include="OpenTelemetry.Instrumentation.Runtime" Version="1.17.0" />
<PackageVersion Include="OpenTelemetry.Instrumentation.SqlClient" Version="1.17.0" />
<PackageVersion Include="QRCoder" Version="1.9.0" />
<PackageVersion Include="Serilog.AspNetCore" Version="10.0.0" />
<PackageVersion Include="Sustainsys.Saml2.AspNetCore2" Version="2.11.0" />
<PackageVersion Include="Swashbuckle.AspNetCore" Version="10.2.3" />
Expand Down
2 changes: 2 additions & 0 deletions IdentityServer/v8/UserManagement/FullSample/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,8 @@ if (authenticators.TotpDeviceNames.Any())

### TOTP Setup

Authenticator QR codes are generated locally on the server with QRCoder and embedded as PNG data URIs. The provisioning URI contains the TOTP shared secret and must not be sent to an external QR service or logged.

```csharp
// Generate key and provisioning URI
var key = PlainBytesTotpKey.New();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
</div>

<div class="text-center mb-4">
<img src="https://quickchart.io/qr?text=@Uri.EscapeDataString(Model.AuthenticatorUri)&size=200"
<img src="@Model.QrCodeDataUri"
alt="QR Code for authenticator app"
width="200" height="200"
class="border rounded" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using QRCoder;

namespace UserManagementSample.Pages.Manage;

Expand All @@ -19,7 +20,7 @@ public sealed class Setup2FAModel(
IUserAuthenticatorsSelfService authenticatorsSelfService) : PageModel
{
public string SharedKey { get; set; } = string.Empty;
public string AuthenticatorUri { get; set; } = string.Empty;
public string QrCodeDataUri { get; set; } = string.Empty;

[BindProperty]
[Required]
Expand All @@ -41,7 +42,7 @@ public IActionResult OnGet()
var key = PlainBytesTotpKey.New();
TotpKeyBase32 = key.EncodeToBase32();

LoadSharedKeyAndQrCodeUri(key);
LoadSharedKeyAndQrCode(key);

return Page();
}
Expand Down Expand Up @@ -73,7 +74,7 @@ public async Task<IActionResult> OnPostAsync()
{
ErrorMessage = "Invalid code format.";
TotpKeyBase32 = keyBase32;
LoadSharedKeyAndQrCodeUri(key);
LoadSharedKeyAndQrCode(key);
return Page();
}

Expand All @@ -88,7 +89,7 @@ public async Task<IActionResult> OnPostAsync()
{
ErrorMessage = "Verification code is invalid. Please try again.";
TotpKeyBase32 = keyBase32;
LoadSharedKeyAndQrCodeUri(key);
LoadSharedKeyAndQrCode(key);
return Page();
}

Expand All @@ -103,22 +104,25 @@ public async Task<IActionResult> OnPostAsync()
return RedirectToPage("/Manage/Setup2FA");
}

private void LoadSharedKeyAndQrCodeUri(PlainBytesTotpKey key)
private void LoadSharedKeyAndQrCode(PlainBytesTotpKey key)
{
SharedKey = FormatKeyToHumanReadable(key.EncodeToBase32());
var accountName = User.FindFirst(JwtClaimTypes.Name)?.Value
?? User.FindFirst(JwtClaimTypes.Email)?.Value
?? User.Identity?.Name
?? "user";
AuthenticatorUri = TotpAuthenticatorUri.Generate("User Management Sample", accountName, key);
var authenticatorUri = TotpAuthenticatorUri.Generate("User Management Sample", accountName, key);
using var qrCodeData = QRCodeGenerator.GenerateQrCode(authenticatorUri, QRCodeGenerator.ECCLevel.Q);
using var qrCode = new PngByteQRCode(qrCodeData);
QrCodeDataUri = $"data:image/png;base64,{Convert.ToBase64String(qrCode.GetGraphic(5))}";
}

private void RestoreKeyFromTempData()
{
var keyBase32 = TotpKeyBase32;
if (!string.IsNullOrEmpty(keyBase32))
{
LoadSharedKeyAndQrCodeUri(PlainBytesTotpKey.DecodeFromBase32(keyBase32));
LoadSharedKeyAndQrCode(PlainBytesTotpKey.DecodeFromBase32(keyBase32));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
<PackageReference Include="Duende.UserManagement.IdentityServer8" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.Google" />
<PackageReference Include="Microsoft.Data.Sqlite" />
<PackageReference Include="QRCoder" />
</ItemGroup>

</Project>
Loading