我正在使用 ASP.NET MVC 5 身份模板,并且在帐户控制器中具有用于用户注册的标准方法
// POST: /Account/Register
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Register(RegisterViewModel model)
{
if (ModelState.IsValid)
{
// bind viewModel --> Model
var user = new ApplicationUser
{
UserName = model.Email,
Email = model.Email,
Fio = model.Fio,
Street = model.Street,
...
};
var result = await UserManager.CreateAsync(user, model.Password);
if (result.Succeeded)
{
await SignInManager.SignInAsync(user, isPersistent:false, rememberBrowser:false);
// For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=320771
// Send an email with this link
string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);
var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme);
...
我使用callbackUrl
来确认用户注册。
然后我作为用户将该 URL 放入浏览器中并证明注册:
// GET: /Account/ConfirmEmail
[AllowAnonymous]
public async Task<ActionResult> ConfirmEmail(string userId, string code)
{
if (userId == null || code == null)
{
return View("Error");
}
var result = await UserManager.ConfirmEmailAsync(userId, code);
return View(result.Succeeded ? "ConfirmEmail" : "Error");
}
因此,当我从应用程序的一个实例中放置两种方法(注册和确认)时,它会起作用。但是当我有两个实例并注册第一个实例并使用第二个实例进行确认时,ConfirmEmailAsync
方法返回错误的结果。
我认为UserManager
必须有一个用于连接到数据库的存储,并且可以检查来自站点不同实例的电子邮件。这是对的吗?
您需要
将machineKey
放入web.config
中 - 以便在应用程序的所有实例中具有相同的密钥。