我有一个使用 Identity Core 生成电子邮件确认令牌的 DotNet Core WebApi。当我生成令牌并使用 WebApi URL 将其发送到我的电子邮件地址时,它工作正常。当我使用 Angular Web 应用程序将 ur?token=token 发送到我的电子邮件以从应用程序进行 AJAX 调用时,我收到“无效令牌”错误。来自 Angular 服务的 GET 调用获取 QueryString 并对 API 进行 AJAX 调用 -> “无效令牌”。
WebAPI 代码:
//I tried
//var confirmationLink = Url.Content($"{this.Request.Scheme}://{siteUrl}{this.Request.PathBase}/Confirm?token={token}&email={appUser.Email}");
// And
var confirmationLink = Url.Content($"{this.Request.Scheme}://{siteUrl}{this.Request.PathBase}/Confirm?token={HttpUtility.UrlEncode(token)}&email={appUser.Email}");
SendEmail($"{model.FirstName} {model.LastName}", "Confirm your account", model.Email,
$"Please confirm your account by clicking {callback}");
角度视图
this.route.queryParams.subscribe(params => {
this.email = params[emailKey];
this.token = params[tokenKey];
});
this.authenticationService.confirmEmail(this.email, this.token).subscribe(
(result) => {
this.emailMsg = 'Thank you for confirming your email. Please procced to login page.';
},
(err) => {
this.emailMsg = 'Email confirmation link has expired or is invalid.';
}
);
角服务
return this.http.get<boolean>(`${environment.apiUrl}/authenticate/VerifyEmail?email=${email}&token=${token}`)
.pipe(
map((data: boolean) => {
return data;
}), catchError((error: HttpErrorResponse) => {
return throwError(error);
})
);
在电子邮件确认端点中,我尝试了一些不同的方法,但都没有奏效。
var user = await _userManager.FindByEmailAsync(email);
if (user == null) {
throw new InvalidOperationException();
}
var fix = token.Replace(' ', '+');
var emailConfirmationResultescapefix = await _userManager.ConfirmEmailAsync(user, Uri.EscapeDataString(fix));
var emailConfirmationResulttest = await _userManager.ConfirmEmailAsync(user, Uri.EscapeDataString(token));
var code = HttpUtility.UrlDecode(token);
var emailConfirmationResult = await _userManager.ConfirmEmailAsync(user, Uri.EscapeDataString(code));
var urisescape = Uri.EscapeDataString(token);
var emailConfirmationResultescape = await _userManager.ConfirmEmailAsync(user, Uri.EscapeDataString(urisescape));
var webUtil = WebUtility.UrlDecode(token);
var emailConfirmationResultweb = await _userManager.ConfirmEmailAsync(user, Uri.EscapeDataString(webUtil));
我尝试验证令牌的所有不同方法都失败了。他们都有一个无效的令牌。我尝试使用代码比较来检查令牌是否确实不同,并且不适用于第一种方法。令牌匹配,我仍然得到一个无效的令牌。