AspNet MVC 5 Identity confirm user registration

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);
                ...

And I use callbackUrl to confirm user registration.

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");
    }

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.

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?

Upvotes: 2

Views: 472

Answers (1)

trailmax
trailmax

Reputation: 35116

You need to put machineKey into your web.config - to have identical key in all instances of the application.

Upvotes: 1

Related Questions