Skip to content

Commit

Permalink
ported web sample to .NET 6 MVC (#156)
Browse files Browse the repository at this point in the history
  • Loading branch information
ahwm authored May 16, 2023
1 parent 4b98bf2 commit 1f48074
Show file tree
Hide file tree
Showing 73 changed files with 74,394 additions and 79 deletions.
139 changes: 60 additions & 79 deletions Google.Authenticator.sln
Original file line number Diff line number Diff line change
@@ -1,79 +1,60 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 2019
VisualStudioVersion = 16.0
MinimumVisualStudioVersion = 15.0
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Google.Authenticator", "Google.Authenticator\Google.Authenticator.csproj", "{3D92DE47-0AB8-466F-9083-AF65D865E4BE}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Google.Authenticator.WinTest", "Google.Authenticator.WinTest\Google.Authenticator.WinTest.csproj", "{C2B44C17-B77B-4DA8-B924-96B28B50D198}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Google.Authenticator.WebSample", "Google.Authenticator.WebSample\Google.Authenticator.WebSample.csproj", "{21A63F79-D85F-4FE7-AC74-5171FAC0DCBF}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "PublishScripts", "PublishScripts", "{AEA3C33C-6FF6-447C-AB2A-97E39AD97441}"
ProjectSection(SolutionItems) = preProject
PublishScripts\AzureWebsitePublishModule.psm1 = PublishScripts\AzureWebsitePublishModule.psm1
PublishScripts\Publish-WebApplicationWebsite.ps1 = PublishScripts\Publish-WebApplicationWebsite.ps1
EndProjectSection
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Configurations", "Configurations", "{A4B0F582-7831-4BFD-825C-64153E7E2138}"
ProjectSection(SolutionItems) = preProject
PublishScripts\Configurations\GoogleAuthenticatorWebSample-WAWS-dev.json = PublishScripts\Configurations\GoogleAuthenticatorWebSample-WAWS-dev.json
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Google.Authenticator.Tests", "Google.Authenticator.Tests\Google.Authenticator.Tests.csproj", "{5671E1C5-7CD0-4F42-8FFD-33879A9663DA}"
EndProject
Global
GlobalSection(TeamFoundationVersionControl) = preSolution
SccNumberOfProjects = 6
SccEnterpriseProvider = {4CA58AB2-18FA-4F8D-95D4-32DDF27D184C}
SccTeamFoundationServer = https://x9tech.visualstudio.com/defaultcollection
SccLocalPath0 = .
SccProjectUniqueName1 = Google.Authenticator\\Google.Authenticator.csproj
SccProjectName1 = Google.Authenticator
SccLocalPath1 = Google.Authenticator
SccProjectUniqueName2 = Google.Authenticator.WinTest\\Google.Authenticator.WinTest.csproj
SccProjectName2 = Google.Authenticator.WinTest
SccLocalPath2 = Google.Authenticator.WinTest
SccProjectUniqueName3 = Google.Authenticator.WebSample\\Google.Authenticator.WebSample.csproj
SccProjectName3 = Google.Authenticator.WebSample
SccLocalPath3 = Google.Authenticator.WebSample
SccProjectUniqueName4 = Google.Authenticator.Tests\\Google.Authenticator.Tests.csproj
SccProjectName4 = Google.Authenticator.Tests
SccLocalPath4 = Google.Authenticator.Tests
SccProjectUniqueName5 = OTPCycler\\OTPCycler.csproj
SccProjectName5 = OTPCycler
SccLocalPath5 = OTPCycler
EndGlobalSection
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{3D92DE47-0AB8-466F-9083-AF65D865E4BE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{3D92DE47-0AB8-466F-9083-AF65D865E4BE}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3D92DE47-0AB8-466F-9083-AF65D865E4BE}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3D92DE47-0AB8-466F-9083-AF65D865E4BE}.Release|Any CPU.Build.0 = Release|Any CPU
{C2B44C17-B77B-4DA8-B924-96B28B50D198}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{C2B44C17-B77B-4DA8-B924-96B28B50D198}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C2B44C17-B77B-4DA8-B924-96B28B50D198}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C2B44C17-B77B-4DA8-B924-96B28B50D198}.Release|Any CPU.Build.0 = Release|Any CPU
{21A63F79-D85F-4FE7-AC74-5171FAC0DCBF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{21A63F79-D85F-4FE7-AC74-5171FAC0DCBF}.Debug|Any CPU.Build.0 = Debug|Any CPU
{21A63F79-D85F-4FE7-AC74-5171FAC0DCBF}.Release|Any CPU.ActiveCfg = Release|Any CPU
{21A63F79-D85F-4FE7-AC74-5171FAC0DCBF}.Release|Any CPU.Build.0 = Release|Any CPU
{5671E1C5-7CD0-4F42-8FFD-33879A9663DA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{5671E1C5-7CD0-4F42-8FFD-33879A9663DA}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5671E1C5-7CD0-4F42-8FFD-33879A9663DA}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5671E1C5-7CD0-4F42-8FFD-33879A9663DA}.Release|Any CPU.Build.0 = Release|Any CPU
{5069C6AB-90A3-4640-8070-A4B84FEC3F40}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{5069C6AB-90A3-4640-8070-A4B84FEC3F40}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5069C6AB-90A3-4640-8070-A4B84FEC3F40}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5069C6AB-90A3-4640-8070-A4B84FEC3F40}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{A4B0F582-7831-4BFD-825C-64153E7E2138} = {AEA3C33C-6FF6-447C-AB2A-97E39AD97441}
EndGlobalSection
EndGlobal

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.5.33516.290
MinimumVisualStudioVersion = 15.0
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Google.Authenticator", "Google.Authenticator\Google.Authenticator.csproj", "{3D92DE47-0AB8-466F-9083-AF65D865E4BE}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Google.Authenticator.WinTest", "Google.Authenticator.WinTest\Google.Authenticator.WinTest.csproj", "{C2B44C17-B77B-4DA8-B924-96B28B50D198}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Google.Authenticator.WebSample", "Google.Authenticator.WebSample\Google.Authenticator.WebSample.csproj", "{21A63F79-D85F-4FE7-AC74-5171FAC0DCBF}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "PublishScripts", "PublishScripts", "{AEA3C33C-6FF6-447C-AB2A-97E39AD97441}"
ProjectSection(SolutionItems) = preProject
PublishScripts\AzureWebsitePublishModule.psm1 = PublishScripts\AzureWebsitePublishModule.psm1
PublishScripts\Publish-WebApplicationWebsite.ps1 = PublishScripts\Publish-WebApplicationWebsite.ps1
EndProjectSection
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Configurations", "Configurations", "{A4B0F582-7831-4BFD-825C-64153E7E2138}"
ProjectSection(SolutionItems) = preProject
PublishScripts\Configurations\GoogleAuthenticatorWebSample-WAWS-dev.json = PublishScripts\Configurations\GoogleAuthenticatorWebSample-WAWS-dev.json
EndProjectSection
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Google.Authenticator.Tests", "Google.Authenticator.Tests\Google.Authenticator.Tests.csproj", "{5671E1C5-7CD0-4F42-8FFD-33879A9663DA}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GoogleAuthenticator.WebCoreSample", "GoogleAuthenticator.WebCoreSample\GoogleAuthenticator.WebCoreSample.csproj", "{2338DB83-00B0-40E2-8C75-F96CB2D63E87}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{3D92DE47-0AB8-466F-9083-AF65D865E4BE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{3D92DE47-0AB8-466F-9083-AF65D865E4BE}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3D92DE47-0AB8-466F-9083-AF65D865E4BE}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3D92DE47-0AB8-466F-9083-AF65D865E4BE}.Release|Any CPU.Build.0 = Release|Any CPU
{C2B44C17-B77B-4DA8-B924-96B28B50D198}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{C2B44C17-B77B-4DA8-B924-96B28B50D198}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C2B44C17-B77B-4DA8-B924-96B28B50D198}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C2B44C17-B77B-4DA8-B924-96B28B50D198}.Release|Any CPU.Build.0 = Release|Any CPU
{21A63F79-D85F-4FE7-AC74-5171FAC0DCBF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{21A63F79-D85F-4FE7-AC74-5171FAC0DCBF}.Debug|Any CPU.Build.0 = Debug|Any CPU
{21A63F79-D85F-4FE7-AC74-5171FAC0DCBF}.Release|Any CPU.ActiveCfg = Release|Any CPU
{21A63F79-D85F-4FE7-AC74-5171FAC0DCBF}.Release|Any CPU.Build.0 = Release|Any CPU
{5671E1C5-7CD0-4F42-8FFD-33879A9663DA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{5671E1C5-7CD0-4F42-8FFD-33879A9663DA}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5671E1C5-7CD0-4F42-8FFD-33879A9663DA}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5671E1C5-7CD0-4F42-8FFD-33879A9663DA}.Release|Any CPU.Build.0 = Release|Any CPU
{2338DB83-00B0-40E2-8C75-F96CB2D63E87}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{2338DB83-00B0-40E2-8C75-F96CB2D63E87}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2338DB83-00B0-40E2-8C75-F96CB2D63E87}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2338DB83-00B0-40E2-8C75-F96CB2D63E87}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{A4B0F582-7831-4BFD-825C-64153E7E2138} = {AEA3C33C-6FF6-447C-AB2A-97E39AD97441}
EndGlobalSection
EndGlobal
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\Google.Authenticator\Google.Authenticator.csproj" />
</ItemGroup>

</Project>
26 changes: 26 additions & 0 deletions GoogleAuthenticator.WebCoreSample/Pages/Index.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
@page
@model IndexModel
@{
ViewData["Title"] = "Home page";
}

<div class="text-center">
<p><strong>Account Secret Key (randomly generated):</strong> @Model.Key</p>
<hr />
<p>
<strong>Setup QR Code:</strong><br />
<img src="@Model.QRImageUrl" />
</p>
<p><strong>Manual Setup Code: </strong> @Model.ManualEntryKey</p>
<hr />
@using (Html.BeginForm(FormMethod.Post))
{
@Html.HiddenFor(x => x.Key)
<p>
Validate Code: <input type="text" name="txtCode" />
</p>
<p><button class="btn btn-primary">Validate My Code!</button></p>
}
<p style="font-weight:bold">@TempData["ValidationResult"]</p>
@*Validate Code: <asp:TextBox runat="server" ID="txtCode"></asp:TextBox> <asp:Button runat="server" ID="btnValidate" Text="Validate My Code!" OnClick="btnValidate_Click" /><br /><asp:Label runat="server" Font-Bold="true" ID="lblValidationResult"></asp:Label>*@
</div>
49 changes: 49 additions & 0 deletions GoogleAuthenticator.WebCoreSample/Pages/Index.cshtml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using Google.Authenticator;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using System.Security.Cryptography;
using static System.Runtime.CompilerServices.RuntimeHelpers;

namespace GoogleAuthenticator.WebCoreSample.Pages
{
public class IndexModel : PageModel
{
private readonly ILogger<IndexModel> _logger;

public string QRImageUrl;
public string Key;
public string ManualEntryKey;

public IndexModel(ILogger<IndexModel> logger)
{
_logger = logger;

}

public void OnGet()
{
byte[] key = new byte[6];
using var rng = RandomNumberGenerator.Create();
rng.GetBytes(key);
Key = Convert.ToBase64String(key);

TwoFactorAuthenticator tfa = new TwoFactorAuthenticator();
SetupCode setupInfo = tfa.GenerateSetupCode("我 & You", "[email protected]", Key, false, 3);
QRImageUrl = setupInfo.QrCodeSetupImageUrl;
ManualEntryKey = setupInfo.ManualEntryKey;
}

public void OnPost()
{
Key = Request.Form["Key"];
TwoFactorAuthenticator tfa = new TwoFactorAuthenticator();
bool result = tfa.ValidateTwoFactorPIN(Key, Request.Form["txtCode"]);

SetupCode setupInfo = tfa.GenerateSetupCode("我 & You", "[email protected]", Key, false, 3);
QRImageUrl = setupInfo.QrCodeSetupImageUrl;
ManualEntryKey = setupInfo.ManualEntryKey;

TempData["ValidationResult"] = $"{Request.Form["txtCode"]} is {(result ? "" : "not ")}a valid PIN at UTC time {DateTime.UtcNow.ToString()}";
}
}
}
48 changes: 48 additions & 0 deletions GoogleAuthenticator.WebCoreSample/Pages/Shared/_Layout.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>@ViewData["Title"] - GoogleAuthenticator.WebCoreSample</title>
<link rel="stylesheet" href="~/lib/bootstrap/dist/css/bootstrap.min.css" />
<link rel="stylesheet" href="~/css/site.css" asp-append-version="true" />
<link rel="stylesheet" href="~/GoogleAuthenticator.WebCoreSample.styles.css" asp-append-version="true" />
</head>
<body>
<header>
<nav class="navbar navbar-expand-sm navbar-toggleable-sm navbar-light bg-white border-bottom box-shadow mb-3">
<div class="container">
<a class="navbar-brand" asp-area="" asp-page="/Index">GoogleAuthenticator.WebCoreSample</a>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target=".navbar-collapse" aria-controls="navbarSupportedContent"
aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="navbar-collapse collapse d-sm-inline-flex justify-content-between">
<ul class="navbar-nav flex-grow-1">
<li class="nav-item">
<a class="nav-link text-dark" asp-area="" asp-page="/Index">Home</a>
</li>
</ul>
</div>
</div>
</nav>
</header>
<div class="container">
<main role="main" class="pb-3">
@RenderBody()
</main>
</div>

<footer class="border-top footer text-muted">
<div class="container">
&copy; 2023 - GoogleAuthenticator.WebCoreSample
</div>
</footer>

<script src="~/lib/jquery/dist/jquery.min.js"></script>
<script src="~/lib/bootstrap/dist/js/bootstrap.bundle.min.js"></script>
<script src="~/js/site.js" asp-append-version="true"></script>

@await RenderSectionAsync("Scripts", required: false)
</body>
</html>
48 changes: 48 additions & 0 deletions GoogleAuthenticator.WebCoreSample/Pages/Shared/_Layout.cshtml.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/* Please see documentation at https://docs.microsoft.com/aspnet/core/client-side/bundling-and-minification
for details on configuring this project to bundle and minify static web assets. */

a.navbar-brand {
white-space: normal;
text-align: center;
word-break: break-all;
}

a {
color: #0077cc;
}

.btn-primary {
color: #fff;
background-color: #1b6ec2;
border-color: #1861ac;
}

.nav-pills .nav-link.active, .nav-pills .show > .nav-link {
color: #fff;
background-color: #1b6ec2;
border-color: #1861ac;
}

.border-top {
border-top: 1px solid #e5e5e5;
}
.border-bottom {
border-bottom: 1px solid #e5e5e5;
}

.box-shadow {
box-shadow: 0 .25rem .75rem rgba(0, 0, 0, .05);
}

button.accept-policy {
font-size: 1rem;
line-height: inherit;
}

.footer {
position: absolute;
bottom: 0;
width: 100%;
white-space: nowrap;
line-height: 60px;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<script src="~/lib/jquery-validation/dist/jquery.validate.min.js"></script>
<script src="~/lib/jquery-validation-unobtrusive/jquery.validate.unobtrusive.min.js"></script>
3 changes: 3 additions & 0 deletions GoogleAuthenticator.WebCoreSample/Pages/_ViewImports.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@using GoogleAuthenticator.WebCoreSample
@namespace GoogleAuthenticator.WebCoreSample.Pages
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
3 changes: 3 additions & 0 deletions GoogleAuthenticator.WebCoreSample/Pages/_ViewStart.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@{
Layout = "_Layout";
}
34 changes: 34 additions & 0 deletions GoogleAuthenticator.WebCoreSample/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
namespace GoogleAuthenticator.WebCoreSample
{
public class Program
{
public static void Main(string[] args)
{
var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddRazorPages();

var app = builder.Build();

// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}

app.UseHttpsRedirection();
app.UseStaticFiles();

app.UseRouting();

app.UseAuthorization();

app.MapRazorPages();

app.Run();
}
}
}
Loading

0 comments on commit 1f48074

Please sign in to comment.