注册后,用户将获得一个链接,该链接由userId和令牌组成。当用户单击它时-angular项目打开,然后angular从链接中获取userId和令牌,然后将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和令牌
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);
}
尝试将Content-Type
设置为json
类型:
confirmEmail(userId: string, token: string) {
const body = JSON.stringify({token});
const options = {
headers: new HttpHeaders().append('Content-Type', 'application/json')
};
return this.http.post(this.authUrl + `confirmemail/${userId}`, body, options)
.pipe(
map(res => res)
, catchError(this.handleError())
);
}
由于代表,我似乎还无法发表评论,但您的邮递员路线似乎使用了与Angular代码不同的localhost端口。您的Angular代码是否正确插入端口?