c# - 如何从 Controller 获取到 Identity/Pages/Account/ConfirmEmail 的回调 URL?

标签 c# asp.net-core asp.net-core-mvc

我有一个场景,我想以与身份区域(“身份/页面/帐户/注册”)的注册页面相同的方式生成电子邮件确认 URL。我已经搭建了标识区域并可以访问其页面。

我需要从位于完全不同区域的 Controller 发生这种情况。 我尝试做一些与 Register.cshtml.cs 中类似的事情,但我没有使用 Url.Page,而是使用 Url.Action(我们位于 Controller 内部,请记住)。

问题是下面的代码生成了一个空字符串,这不是我们想要的:

string confirmationLink = Url.Action("ConfirmEmail",
                        "Account", new
                        {
                            area = "Identity",
                            userid = viewModel.User.Id,
                            token = code
                        },
                        protocol: HttpContext.Request.Scheme);

我在这里可能做错了什么?这位于

/Areas/CompanyUsers/Controllers/HomeController.cs.

请不要评论从“随机” Controller 生成此 url 的想法。我需要一种从已经通过身份验证的用户注册用户的方法(公司所有者用户必须能够将用户注册到应用程序中,而无需使用默认身份方式来执行此操作。)。

最佳答案

Url.Action 用于为操作 生成 URL,但 ConfirmEmail 是一个页面。使用 Url.Page 生成正确的 URL:

string confirmationLink = Url.Page(
    "/Account/ConfirmEmail",
    null, // handler
    new { area = "Identity", userid = viewModel.User.Id, token = code },
    Request.Scheme);

关于c# - 如何从 Controller 获取到 Identity/Pages/Account/ConfirmEmail 的回调 URL?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57224065/

相关文章:

azure - WebApp 中托管的 .Net core 2.1 MVC 中的推送通知

c# - await Task.Run(() => semaphore.WaitOne()) 有什么问题吗?

c# - 客户端通常在几十分钟后与服务器断开连接

asp.net-core - .NET Core MVC 自动迁移后,类库在 VS2017 中丢失智能感知

asp.net-mvc - 如何在 AspNet5/Mvc6 中检测 dnx451 Web 应用程序关闭?

macos - 找不到与命令 "dotnet-ef"匹配的可执行文件 (Csproj)

c# - 在 C# 中使用 String.Format 格式化字符串的问题

c# - 在 Asp.net Core :Invalid setup on a non-virtual (overridable in VB) 中使用 Moq 的问题

ASP .Net Core 2.1.1 中的 Razor 类库

c# - 如何简单地从 appsettings.json 读取