From e4ecd070ebe62b4f54a479dbf726668a8cf86098 Mon Sep 17 00:00:00 2001 From: Pranav K Date: Tue, 5 Dec 2017 15:34:11 -0800 Subject: [PATCH] Use AuthenticatorUrl generated on the server in EnableAuthenticator pages * Update prerelease package versions * Install the right CLI version --- Directory.Build.props | 2 + Directory.Build.targets | 5 ++- build/dependencies.props | 40 +++++++++--------- build/repo.props | 7 ++++ build/sources.props | 1 - .../Account/Manage/EnableAuthenticator.cshtml | 2 +- .../Manage/EnableAuthenticator.cshtml.cs | 14 +++---- .../Controllers/ManageController.cs | 41 +++++++++++-------- .../EnableAuthenticatorViewModel.cs | 4 +- .../Views/Manage/EnableAuthenticator.cshtml | 2 +- 10 files changed, 66 insertions(+), 52 deletions(-) create mode 100644 build/repo.props diff --git a/Directory.Build.props b/Directory.Build.props index e89ffae876..d867f8478f 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -1,5 +1,7 @@ + + false diff --git a/Directory.Build.targets b/Directory.Build.targets index 6902b49b99..ba8545d8c4 100644 --- a/Directory.Build.targets +++ b/Directory.Build.targets @@ -1,5 +1,6 @@  - - + + $(MicrosoftNETCoreApp20PackageVersion) + diff --git a/build/dependencies.props b/build/dependencies.props index 2622549aa1..a499aa7e71 100644 --- a/build/dependencies.props +++ b/build/dependencies.props @@ -5,32 +5,32 @@ 2.0.2-rc1-16007 - 2.0.3-rtm-207 - 2.0.1-rtm-207 - 2.0.1-rtm-207 - 2.0.1-rtm-207 - 2.0.1-rtm-207 - 2.0.1-rtm-207 - 2.0.1-rtm-207 - 2.0.1-rtm-207 - 2.0.1-rtm-207 - 2.0.1-rtm-207 - 2.0.1-rtm-207 + 2.0.3 + 2.0.1 + 2.0.1 + 2.0.1 + 2.0.1 + 2.0.1 + 2.0.1 + 2.0.1 + 2.0.1 + 2.0.1 + 2.0.1 2.0.0 15.3.409 15.3.409 - 2.0.1-rtm-207 - 2.0.1-rtm-207 - 2.0.1-rtm-207 - 2.0.1-rtm-207 - 2.0.1-rtm-207 + 2.0.1 + 2.0.1 + 2.0.1 + 2.0.1 + 2.0.1 2.0.0 2.0.0 15.3.0 - 2.0.1-rtm-207 - 2.0.1-rtm-207 - 2.0.1-rtm-207 - 2.0.2-servicing-25728-02 + 2.0.1 + 2.0.1 + 2.0.1 + 2.0.3 0.19.0 3.6.0 16.16299.0 diff --git a/build/repo.props b/build/repo.props new file mode 100644 index 0000000000..468b1c2261 --- /dev/null +++ b/build/repo.props @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file diff --git a/build/sources.props b/build/sources.props index 8ab0fd1511..281a1a25e6 100644 --- a/build/sources.props +++ b/build/sources.props @@ -5,7 +5,6 @@ $(DotNetRestoreSources) $(RestoreSources); - https://dotnet.myget.org/F/aspnet-2-0-2-october2017-patch/api/v3/index.json; https://dotnet.myget.org/F/aspnetcore-master/api/v3/index.json; https://dotnet.myget.org/F/aspnetcore-tools/api/v3/index.json; diff --git a/src/Microsoft.DotNet.Web.ProjectTemplates/content/RazorPagesWeb-CSharp/Pages/Account/Manage/EnableAuthenticator.cshtml b/src/Microsoft.DotNet.Web.ProjectTemplates/content/RazorPagesWeb-CSharp/Pages/Account/Manage/EnableAuthenticator.cshtml index 1d68558407..9d1113d779 100644 --- a/src/Microsoft.DotNet.Web.ProjectTemplates/content/RazorPagesWeb-CSharp/Pages/Account/Manage/EnableAuthenticator.cshtml +++ b/src/Microsoft.DotNet.Web.ProjectTemplates/content/RazorPagesWeb-CSharp/Pages/Account/Manage/EnableAuthenticator.cshtml @@ -24,7 +24,7 @@

Scan the QR Code or enter this key @Model.SharedKey into your two factor authenticator app. Spaces and casing do not matter.

To enable QR code generation please read our documentation.
-
+
  • diff --git a/src/Microsoft.DotNet.Web.ProjectTemplates/content/RazorPagesWeb-CSharp/Pages/Account/Manage/EnableAuthenticator.cshtml.cs b/src/Microsoft.DotNet.Web.ProjectTemplates/content/RazorPagesWeb-CSharp/Pages/Account/Manage/EnableAuthenticator.cshtml.cs index 09a6327de0..f23f1431f7 100644 --- a/src/Microsoft.DotNet.Web.ProjectTemplates/content/RazorPagesWeb-CSharp/Pages/Account/Manage/EnableAuthenticator.cshtml.cs +++ b/src/Microsoft.DotNet.Web.ProjectTemplates/content/RazorPagesWeb-CSharp/Pages/Account/Manage/EnableAuthenticator.cshtml.cs @@ -57,11 +57,6 @@ namespace Company.WebApplication1.Pages.Account.Manage } await LoadSharedKeyAndQrCodeUriAsync(user); - if (string.IsNullOrEmpty(SharedKey)) - { - await _userManager.ResetAuthenticatorKeyAsync(user); - await LoadSharedKeyAndQrCodeUriAsync(user); - } return Page(); } @@ -102,11 +97,14 @@ namespace Company.WebApplication1.Pages.Account.Manage { // Load the authenticator key & QR code URI to display on the form var unformattedKey = await _userManager.GetAuthenticatorKeyAsync(user); - if (!string.IsNullOrEmpty(unformattedKey)) + if (string.IsNullOrEmpty(unformattedKey)) { - SharedKey = FormatKey(unformattedKey); - AuthenticatorUri = GenerateQrCodeUri(user.Email, unformattedKey); + await _userManager.ResetAuthenticatorKeyAsync(user); + unformattedKey = await _userManager.GetAuthenticatorKeyAsync(user); } + + SharedKey = FormatKey(unformattedKey); + AuthenticatorUri = GenerateQrCodeUri(user.Email, unformattedKey); } private string FormatKey(string unformattedKey) diff --git a/src/Microsoft.DotNet.Web.ProjectTemplates/content/StarterWeb-CSharp/Controllers/ManageController.cs b/src/Microsoft.DotNet.Web.ProjectTemplates/content/StarterWeb-CSharp/Controllers/ManageController.cs index d840959dd3..437e01129e 100644 --- a/src/Microsoft.DotNet.Web.ProjectTemplates/content/StarterWeb-CSharp/Controllers/ManageController.cs +++ b/src/Microsoft.DotNet.Web.ProjectTemplates/content/StarterWeb-CSharp/Controllers/ManageController.cs @@ -371,18 +371,8 @@ namespace Company.WebApplication1.Controllers throw new ApplicationException($"Unable to load user with ID '{_userManager.GetUserId(User)}'."); } - var unformattedKey = await _userManager.GetAuthenticatorKeyAsync(user); - if (string.IsNullOrEmpty(unformattedKey)) - { - await _userManager.ResetAuthenticatorKeyAsync(user); - unformattedKey = await _userManager.GetAuthenticatorKeyAsync(user); - } - - var model = new EnableAuthenticatorViewModel - { - SharedKey = FormatKey(unformattedKey), - AuthenticatorUri = GenerateQrCodeUri(user.Email, unformattedKey) - }; + var model = new EnableAuthenticatorViewModel(); + await LoadSharedKeyAndQrCodeUriAsync(user, model); return View(model); } @@ -391,17 +381,18 @@ namespace Company.WebApplication1.Controllers [ValidateAntiForgeryToken] public async Task EnableAuthenticator(EnableAuthenticatorViewModel model) { - if (!ModelState.IsValid) - { - return View(model); - } - var user = await _userManager.GetUserAsync(User); if (user == null) { throw new ApplicationException($"Unable to load user with ID '{_userManager.GetUserId(User)}'."); } + if (!ModelState.IsValid) + { + await LoadSharedKeyAndQrCodeUriAsync(user, model); + return View(model); + } + // Strip spaces and hypens var verificationCode = model.Code.Replace(" ", string.Empty).Replace("-", string.Empty); @@ -410,7 +401,8 @@ namespace Company.WebApplication1.Controllers if (!is2faTokenValid) { - ModelState.AddModelError("model.Code", "Verification code is invalid."); + ModelState.AddModelError("Code", "Verification code is invalid."); + await LoadSharedKeyAndQrCodeUriAsync(user, model); return View(model); } @@ -500,6 +492,19 @@ namespace Company.WebApplication1.Controllers unformattedKey); } + private async Task LoadSharedKeyAndQrCodeUriAsync(ApplicationUser user, EnableAuthenticatorViewModel model) + { + var unformattedKey = await _userManager.GetAuthenticatorKeyAsync(user); + if (string.IsNullOrEmpty(unformattedKey)) + { + await _userManager.ResetAuthenticatorKeyAsync(user); + unformattedKey = await _userManager.GetAuthenticatorKeyAsync(user); + } + + model.SharedKey = FormatKey(unformattedKey); + model.AuthenticatorUri = GenerateQrCodeUri(user.Email, unformattedKey); + } + #endregion } } diff --git a/src/Microsoft.DotNet.Web.ProjectTemplates/content/StarterWeb-CSharp/Models/ManageViewModels/EnableAuthenticatorViewModel.cs b/src/Microsoft.DotNet.Web.ProjectTemplates/content/StarterWeb-CSharp/Models/ManageViewModels/EnableAuthenticatorViewModel.cs index 3f3323f216..2aaafedad2 100644 --- a/src/Microsoft.DotNet.Web.ProjectTemplates/content/StarterWeb-CSharp/Models/ManageViewModels/EnableAuthenticatorViewModel.cs +++ b/src/Microsoft.DotNet.Web.ProjectTemplates/content/StarterWeb-CSharp/Models/ManageViewModels/EnableAuthenticatorViewModel.cs @@ -4,6 +4,7 @@ using System.ComponentModel; using System.ComponentModel.DataAnnotations; using System.Linq; using System.Threading.Tasks; +using Microsoft.AspNetCore.Mvc.ModelBinding; namespace Company.WebApplication1.Models.ManageViewModels { @@ -15,9 +16,10 @@ namespace Company.WebApplication1.Models.ManageViewModels [Display(Name = "Verification Code")] public string Code { get; set; } - [ReadOnly(true)] + [BindNever] public string SharedKey { get; set; } + [BindNever] public string AuthenticatorUri { get; set; } } } diff --git a/src/Microsoft.DotNet.Web.ProjectTemplates/content/StarterWeb-CSharp/Views/Manage/EnableAuthenticator.cshtml b/src/Microsoft.DotNet.Web.ProjectTemplates/content/StarterWeb-CSharp/Views/Manage/EnableAuthenticator.cshtml index 79693d78e3..4cbe57304f 100644 --- a/src/Microsoft.DotNet.Web.ProjectTemplates/content/StarterWeb-CSharp/Views/Manage/EnableAuthenticator.cshtml +++ b/src/Microsoft.DotNet.Web.ProjectTemplates/content/StarterWeb-CSharp/Views/Manage/EnableAuthenticator.cshtml @@ -23,7 +23,7 @@

    Scan the QR Code or enter this key @Model.SharedKey into your two factor authenticator app. Spaces and casing do not matter.

    To enable QR code generation please read our documentation.
    -
    +