【发布时间】:2020-09-19 21:31:58
【问题描述】:
用户注册后会得到一个链接,链接由userId和token组成。当用户点击它时 - angular 项目打开,然后 angular 从链接中获取 userId 和 token,并将 post 方法发送到后端以验证电子邮件。
链接示例
Post 方法,用于验证电子邮件:
[HttpPost("confirmEmail/{userId}")]
public async Task<IActionResult> ConfirmEmail(string userId, [FromBody]string token)
{
var codeDecodedBytes = WebEncoders.Base64UrlDecode(token);
var codeDecoded = Encoding.UTF8.GetString(codeDecodedBytes);
var user = await _userManager.FindByIdAsync(userId);
var result = await _userManager.ConfirmEmailAsync(user, codeDecoded);
return Ok(result);
}
在 Angular 中获取 userId 和 token
userId: string;
token: string;
constructor(private activatedRoute: ActivatedRoute, private authService: AuthService) {
this.activatedRoute.queryParams.subscribe(params => {
this.userId = params['userId'];
this.token = params['token'];
});
}
ngOnInit(): void {
this.confirmEmail();
}
confirmEmail(){
this.authService.confirmEmail(this.userId, this.token).subscribe(data => { console.log("success")});
}
(AuthService) 发送数据到后端
confirmEmail(userId: string, token: string){
console.log(userId);
console.log(token);
return this.http.post(this.authUrl + `confirmemail/${userId}`, token);
}
【问题讨论】:
标签: c# angular asp.net-core