URL.Page为ASP.Core身份返回null

wor*_*red 5 razor asp.net-identity asp.net-core asp.net-core-2.0

我有一个ASP.Core应用程序。我添加了身份。然后,我对其进行了自定义,我复制了现有逻辑,以向另一目录中的另一个模型发送验证电子邮件。

自动生成的脚本创建了一个Areas / Identity目录。在该目录下,有Pages / Account / Manage / Index.cshtml.cs文件。此文件夹中的代码使用相同的调用来生成确认电子邮件代码。工作正常。

但是,当我调用Url.Page时,自定义模型返回null。我不知道为什么。以下是我如何在startup.cs中自定义身份

services.AddIdentity<WebUser, WebRole>()
        .AddEntityFrameworkStores<MyIdentityDbContext>()
        .AddDefaultTokenProviders()
        .AddDefaultUI();
Run Code Online (Sandbox Code Playgroud)

在尝试使用现有的Identity框架获取Page位置时,会在此中断。Url.Page返回null。

var userId = await _userManager.GetUserIdAsync(user);
var email = await _userManager.GetEmailAsync(user);
var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);
var callbackUrl = Url.Page(
                 "/Account/ConfirmEmail",
                 pageHandler: null,
                 values: new { userId = userId, code = code },
                 protocol: Request.Scheme);
await _emailSender.SendEmailAsync(
                email,
                "Confirm your email",
                $"Please confirm your account by <a href='{HtmlEncoder.Default.Encode(callbackUrl)}'>clicking here</a>.");
Run Code Online (Sandbox Code Playgroud)

为什么从不同的模型调用URL.Page会产生这种差异?我猜我在路由工作原理上缺少一些基本知识,但不确定是什么。

wor*_*red 7

那是由于事实。脚手架身份使用另一个称为身份的区域,因此要从其他位置进行调用,您必须添加到

values= new {area = "Identity", userId = userId, code = code}
Run Code Online (Sandbox Code Playgroud)

有关更多信息,请参见stackoverflow上的此链接。