Skip to content

Commit 64f8c12

Browse files
Update two-factor-authentication.md
Updated members two factor example code for Umbraco v16
1 parent a43d1ad commit 64f8c12

File tree

1 file changed

+40
-27
lines changed

1 file changed

+40
-27
lines changed

16/umbraco-cms/reference/security/two-factor-authentication.md

Lines changed: 40 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ public class UmbracoAppAuthenticator : ITwoFactorProvider
100100
/// <returns>The required data to setup the authenticator app</returns>
101101
public Task<ISetupTwoFactorModel> GetSetupDataAsync(Guid userOrMemberKey, string secret)
102102
{
103-
var member = _memberService.GetByKey(userOrMemberKey);
103+
var member = _memberService.GetById(userOrMemberKey);
104104

105105
var applicationName = "testingOn15";
106106
var twoFactorAuthenticator = new TwoFactorAuthenticator();
@@ -175,45 +175,58 @@ If you already have a members-only page with the edit profile options, you can s
175175
@using Umbraco.Cms.Web.Website.Controllers;
176176
@using Umbraco.Cms.Web.Website.Models;
177177
@using My.Website;
178-
@inject MemberModelBuilderFactory memberModelBuilderFactory
179-
@inject ITwoFactorLoginService twoFactorLoginService
178+
@inject MemberModelBuilderFactory MemberModelBuilderFactory
179+
@inject IMemberTwoFactorLoginService MemberTwoFactorLoginService
180180
@{
181181
// Build a profile model to edit
182-
var profileModel = await memberModelBuilderFactory
183-
.CreateProfileModel()
184-
.BuildForCurrentMemberAsync();
182+
var profileModel = await MemberModelBuilderFactory
183+
.CreateProfileModel()
184+
.BuildForCurrentMemberAsync();
185+
186+
List<UserTwoFactorProviderModel>? providerNameList = null;
187+
if (profileModel != null)
188+
{
189+
var providerNamesAttempt = await MemberTwoFactorLoginService.GetProviderNamesAsync(profileModel.Key);
190+
191+
if (providerNamesAttempt.Success)
192+
{
193+
providerNameList = providerNamesAttempt.Result.ToList();
194+
}
195+
}
185196

186197
// Show all two factor providers
187-
var providerNames = twoFactorLoginService.GetAllProviderNames();
188-
if (providerNames.Any())
198+
if (providerNameList != null && providerNameList.Any())
189199
{
190200
<div asp-validation-summary="All" class="text-danger"></div>
191-
foreach (var providerName in providerNames)
192-
{
193-
var setupData = await twoFactorLoginService.GetSetupInfoAsync(profileModel.Key, providerName);
201+
foreach (var provider in providerNameList)
202+
{0
203+
var setupData = await MemberTwoFactorLoginService.GetSetupInfoAsync(profileModel.Key, provider.ProviderName);
194204

195-
// If the `setupData` is `null` for the specified `providerName` it means the provider is already set up.
196-
// In this case, a button to disable the authentication is shown.
197-
if (setupData is null)
205+
// If the `setupData.Success` is `true` for the specified `providerName` it means the provider is not set up.
206+
if (setupData.Success)
198207
{
199-
@using (Html.BeginUmbracoForm<UmbTwoFactorLoginController>(nameof(UmbTwoFactorLoginController.Disable)))
208+
if (setupData.Result is QrCodeSetupData qrCodeSetupData)
200209
{
201-
<input type="hidden" name="providerName" value="@providerName"/>
202-
<button type="submit">Disable @providerName</button>
210+
@using (Html.BeginUmbracoForm<UmbTwoFactorLoginController>(nameof(UmbTwoFactorLoginController.ValidateAndSaveSetup)))
211+
{
212+
<h3>Setup @providerName</h3>
213+
<img src="@qrCodeSetupData.SetupCode.QrCodeSetupImageUrl"/>
214+
<p>Scan the code above with your authenticator app <br /> and enter the resulting code here to validate:</p>
215+
<input type="hidden" name="providerName" value="@providerName" />
216+
<input type="hidden" name="secret" value="@qrCodeSetupData.Secret" />
217+
<input type="text" name="code" />
218+
<button type="submit">Validate & save</button>
219+
}
203220
}
204221
}
205-
// If `setupData` is not `null` the type is checked and the UI for how to set up the App Authenticator is shown.
206-
else if(setupData is QrCodeSetupData qrCodeSetupData)
222+
// If `setupData.Success` is `false` the provider is already setup.
223+
// In this case, a button to disable the authentication is shown.
224+
else
207225
{
208-
@using (Html.BeginUmbracoForm<UmbTwoFactorLoginController>(nameof(UmbTwoFactorLoginController.ValidateAndSaveSetup)))
226+
@using (Html.BeginUmbracoForm<UmbTwoFactorLoginController>(nameof(UmbTwoFactorLoginController.Disable)))
209227
{
210-
<h3>Setup @providerName</h3>
211-
<img src="@qrCodeSetupData.SetupCode.QrCodeSetupImageUrl"/>
212-
<p>Scan the code above with your authenticator app <br /> and enter the resulting code here to validate:</p>
213-
<input type="hidden" name="providerName" value="@providerName" />
214-
<input type="hidden" name="secret" value="@qrCodeSetupData.Secret" />
215-
<input type="text" name="code" />
216-
<button type="submit">Validate & save</button>
228+
<input type="hidden" name="providerName" value="@providerName"/>
229+
<button type="submit">Disable @providerName</button>
217230
}
218231
}
219232
}

0 commit comments

Comments
 (0)