AspNet MVC 5 Identity确认用户注册 [英] AspNet MVC 5 Identity confirm user registration
问题描述
我正在使用ASP.NET MVC 5 Identity模板,并且具有AccountController标准方法来进行用户注册
I'm using ASP.NET MVC 5 Identity template and have in AccountController standard method for user registration
// 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
确认用户注册.
And I use callbackUrl
to confirm user registration.
然后,我作为用户将Url放入浏览器并证明注册成功:
Then I as a user put that Url into browser and prove registration:
// 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
方法将返回错误的结果.
So, it work when I put both methods (register and confirm) from one instance of my application.
But when I have two instances and register with the first one and confirm with second one, ConfirmEmailAsync
method returns wrong result.
我认为UserManager
必须具有用于连接数据库的商店,并且可以检查来自其他站点实例的电子邮件.这是正确的吗?
I think UserManager
must have a store for connection to the database and can check an email from different instance of a site. It this correct?
推荐答案
您需要将machineKey
放入web.config
中-在应用程序的所有实例中都具有相同的密钥.
You need to put machineKey
into your web.config
- to have identical key in all instances of the application.
这篇关于AspNet MVC 5 Identity确认用户注册的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!