获得一个Aps.Net Core spa(Angular)应用程序,并希望在成功确认电子邮件并登录后重定向到页面。如何实施此操作(在Identity / Pages / Account / ConfirmEmail.chtml.cs代码中)。
...
var user = await _userManager.FindByIdAsync(userId);
if (user == null)
{
return NotFound($"Unable to load user with ID '{userId}'.");
}
code = Encoding.UTF8.GetString(WebEncoders.Base64UrlDecode(code));
var result = await _userManager.ConfirmEmailAsync(user, code);
if (result.Succeeded)
{
await _signInManager.SignInAsync(user, false, null);
return RedirectToPage("/"); // THIS DOES NOT WORK... AREA PROBLEM?
}
...
上面代码中的RedirectToPage会产生异常:InvalidOperationException:没有名为'/'的页面与提供的值匹配。也许与“区域”和登录相关?
与ConfirmEmail.chtml文件中的JavaScript结合解决。在ConfirmEmail.chtml.cs中,我添加了:
var result = await _userManager.ConfirmEmailAsync(user, code);
if (result.Succeeded)
{
await _signInManager.SignInAsync(user, false, null);
ViewData["Redirect"] = "/";
}
在ConfirmEmail.chtml中,我在文件末尾添加了以下更改:
@if (ViewData["Redirect"] != null)
{
<text>
<script>
window.location.href = '@ViewData["Redirect"]';
</script>
</text>
}