Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit34a8202

Browse files
committed
add authentication verification
1 parent080c5cf commit34a8202

File tree

4 files changed

+139
-8
lines changed

4 files changed

+139
-8
lines changed

‎AspNetCoreIdentity/ClientApp/app/components/account/account.component.html‎

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -71,15 +71,23 @@ <h3>Account management</h3>
7171
<p>Scan the QR Code or enter the following key into your two factor authenticator app. Spaces and casing do not matter</p>
7272
<divstyle="text-align: center; color: lightgreen;font-size: 20px;">{{authenticatorDetails.sharedKey}}</div>
7373
<divid="genQrCode"><span*ngIf="generatingQrCode">Generating...</span></div>
74-
<div>
75-
Once you have scanned the QR code or input the key above, your two factor authentication app will provide you with a unique code. Enter the code in the confirmation box below
76-
<inputclass="form-control"type="text"placeholder="Verification Code"style="margin: 10px 0;"/>
77-
<buttontype="button"(click)="setupAuthenticator()"class="form-control btn btn-primary">Verify</button>
78-
</div>
74+
<div>
75+
Once you have scanned the QR code or input the key above, your two factor authentication app will provide you with a unique code. Enter the code in the confirmation box below
76+
<inputclass="form-control"name="username"id="username"[(ngModel)]="verificationCode"type="text"placeholder="Verification Code"style="margin: 10px 0;"/>
77+
<buttontype="button"(click)="verifyAuthenticator()"class="form-control btn btn-primary">Verify</button>
78+
</div>
79+
<divclass="form-group"*ngIf="errors.length > 0">
80+
<ul[innerHTML]="errors"style="color: #E91E63; margin: 10px 0;"></ul>
81+
</div>
7982
</div>
8083
<divclass="col-sm-6 col-sm-offset-3"*ngIf="accountDetails.hasAuthenticator">
8184
<buttontype="button"class="form-control btn btn-danger">Reset authenticator</button>
82-
85+
</div>
86+
<divclass="col-sm-6 col-sm-offset-3"*ngIf="recoveryCodes.length > 0">
87+
<h5>Copy and save your recovery codes</h5>
88+
<ul>
89+
<li*ngFor="let recoveryCode of recoveryCodes">{{recoveryCode}}</li>
90+
</ul>
8391
</div>
8492
</div>
8593
<divclass="tab-pane fade"id="resetpassword">Default 5</div>

‎AspNetCoreIdentity/ClientApp/app/components/account/account.component.ts‎

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import{Component,Inject}from'@angular/core';
22
import{Http}from'@angular/http';
3+
import{StateService}from'../../core/state.service';
34

45
declarevarQRCode:any;
56

@@ -14,10 +15,14 @@ export class AccountComponent {
1415
publicauthenticatorDetails:AuthenticatorDetailsVM=<AuthenticatorDetailsVM>{};
1516
publicdisplayAuthenticator:boolean=false;
1617
publicgeneratingQrCode:boolean=false;
18+
publicverificationCode:string='';
19+
publicerrors:string='';
20+
publicrecoveryCodes:string[]=[];
1721

1822
publicgeneratedQRCode:any;
1923

20-
constructor(publichttp:Http, @Inject('BASE_URL')publicbaseUrl:string){
24+
constructor(publichttp:Http, @Inject('BASE_URL')publicbaseUrl:string,
25+
publicstateService:StateService){
2126
this.http.get(this.baseUrl+'api/manageaccount/details').subscribe(result=>{
2227
this.accountDetails=result.json()asAccountDetailsVM;
2328
console.log(this.accountDetails);
@@ -31,7 +36,7 @@ export class AccountComponent {
3136
console.log(this.authenticatorDetails);
3237
this.displayAuthenticator=true;
3338
this.generatingQrCode=true;
34-
39+
3540
setTimeout(function(){
3641
self.generatedQRCode=newQRCode(document.getElementById("genQrCode"),
3742
{
@@ -49,6 +54,33 @@ export class AccountComponent {
4954

5055
},error=>console.error(error));
5156
}
57+
58+
verifyAuthenticator(){
59+
varverification={
60+
verificationCode:this.verificationCode
61+
};
62+
63+
this.errors='';
64+
65+
this.http.post(this.baseUrl+'api/manageaccount/verifyAuthenticator',verification).subscribe(result=>{
66+
67+
letverifyAuthenticatorResult=result.json()asResultVM;
68+
if(verifyAuthenticatorResult.status===StatusEnum.Success){
69+
this.stateService.displayNotification({message:verifyAuthenticatorResult.message,type:"success"});
70+
71+
if(verifyAuthenticatorResult.data&&verifyAuthenticatorResult.data.recoveryCodes){
72+
// display new recovery codes
73+
this.recoveryCodes=verifyAuthenticatorResult.data.recoveryCodes;
74+
}
75+
76+
this.displayAuthenticator=false;
77+
78+
}elseif(verifyAuthenticatorResult.status===StatusEnum.Error){
79+
this.errors=verifyAuthenticatorResult.data.toString();
80+
}
81+
},
82+
error=>console.error(error));
83+
}
5284
}
5385

5486
interfaceAccountDetailsVM{
@@ -66,4 +98,15 @@ interface AccountDetailsVM {
6698
interfaceAuthenticatorDetailsVM{
6799
sharedKey:string;
68100
authenticatorUri:string;
101+
}
102+
103+
interfaceResultVM{
104+
status:StatusEnum;
105+
message:string;
106+
data:any;
107+
}
108+
109+
enumStatusEnum{
110+
Success=1,
111+
Error=2
69112
}

‎AspNetCoreIdentity/Controllers/ManageAccountController.cs‎

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
usingMicrosoft.AspNetCore.Authorization;
99
usingMicrosoft.AspNetCore.Identity;
1010
usingMicrosoft.AspNetCore.Mvc;
11+
usingMicrosoft.AspNetCore.Mvc.ModelBinding;
1112

1213
namespaceAspNetCoreIdentity.Controllers
1314
{
@@ -58,6 +59,68 @@ public async Task<AuthenticatorDetailsVM> SetupAuthenticator()
5859
returnauthenticatorDetails;
5960
}
6061

62+
[HttpPost]
63+
[Authorize]
64+
publicasyncTask<ResultVM>VerifyAuthenticator([FromBody]VefiryAuthenticatorVMverifyAuthenticator)
65+
{
66+
varuser=await_userManager.GetUserAsync(User);
67+
if(!ModelState.IsValid)
68+
{
69+
varerrors=GetErrors(ModelState).Select(e=>"<li>"+e+"</li>");
70+
returnnewResultVM
71+
{
72+
Status=Status.Error,
73+
Message="Invalid data",
74+
Data=string.Join("",errors)
75+
};
76+
}
77+
78+
varverificationCode=verifyAuthenticator.VerificationCode.Replace(" ",string.Empty).Replace("-",string.Empty);
79+
80+
varis2FaTokenValid=await_userManager.VerifyTwoFactorTokenAsync(
81+
user,_userManager.Options.Tokens.AuthenticatorTokenProvider,verificationCode);
82+
83+
if(!is2FaTokenValid)
84+
{
85+
returnnewResultVM
86+
{
87+
Status=Status.Error,
88+
Message="Invalid data",
89+
Data="<li>Verification code is invalid.</li>"
90+
};
91+
}
92+
93+
await_userManager.SetTwoFactorEnabledAsync(user,true);
94+
95+
varresult=newResultVM
96+
{
97+
Status=Status.Success,
98+
Message="Your authenticator app has been verified",
99+
};
100+
101+
if(await_userManager.CountRecoveryCodesAsync(user)!=0)returnresult;
102+
103+
varrecoveryCodes=await_userManager.GenerateNewTwoFactorRecoveryCodesAsync(user,10);
104+
result.Data=new{recoveryCodes};
105+
returnresult;
106+
107+
}
108+
109+
privateList<string>GetErrors(ModelStateDictionarymodelState)
110+
{
111+
varerrors=newList<string>();
112+
113+
foreach(varstateinmodelState.Values)
114+
{
115+
foreach(varerrorinstate.Errors)
116+
{
117+
errors.Add(error.ErrorMessage);
118+
}
119+
}
120+
121+
returnerrors;
122+
}
123+
61124
privateasyncTask<AuthenticatorDetailsVM>LoadSharedKeyAndQrCodeUriAsync(IdentityUseruser)
62125
{
63126
// Load the authenticator key & QR code URI to display on the form
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
usingSystem;
2+
usingSystem.Collections.Generic;
3+
usingSystem.ComponentModel.DataAnnotations;
4+
usingSystem.Linq;
5+
usingSystem.Threading.Tasks;
6+
7+
namespaceAspNetCoreIdentity.ViewModels
8+
{
9+
publicclassVefiryAuthenticatorVM
10+
{
11+
[Required]
12+
[StringLength(7,ErrorMessage="The {0} must be at least {2} and at max {1} characters long.",MinimumLength=6)]
13+
[DataType(DataType.Text)]
14+
[Display(Name="Verification Code")]
15+
publicstringVerificationCode{get;set;}
16+
}
17+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp