protected virtual async Task <IEnumerable <Claim> > SetAccountEmailAsync(TKey userID, IEnumerable <Claim> claims)
        {
            var email = claims.FirstOrDefault(x => x.Type == Constants.ClaimTypes.Email);

            if (email != null)
            {
                var userEmail = await userManager.GetEmailAsync(userID);

                if (userEmail == null)
                {
                    // if this fails, then presumably the email is already associated with another account
                    // so ignore the error and let the claim pass thru
                    var result = await userManager.SetEmailAsync(userID, email.Value);

                    if (result.Succeeded)
                    {
                        var email_verified = claims.FirstOrDefault(x => x.Type == Constants.ClaimTypes.EmailVerified);
                        if (email_verified != null && email_verified.Value == "true")
                        {
                            var token = await userManager.GenerateEmailConfirmationTokenAsync(userID);

                            await userManager.ConfirmEmailAsync(userID, token);
                        }

                        var emailClaims = new string[] { Constants.ClaimTypes.Email, Constants.ClaimTypes.EmailVerified };
                        return(claims.Where(x => !emailClaims.Contains(x.Type)));
                    }
                }
            }

            return(claims);
        }
        public async Task <IActionResult> ConfirmEmail(string userId, string code)
        {
            if (userId == null || code == null)
            {
                return(View("Error"));
            }
            var user = await _userManager.FindByIdAsync(userId);

            if (user == null)
            {
                return(View("Error"));
            }
            var result = await _userManager.ConfirmEmailAsync(user, code);

            return(View(result.Succeeded ? "ConfirmEmail" : "Error"));
        }
Пример #3
0
        public async System.Threading.Tasks.Task <ActionResult> Confirm(string id, string token)
        {
            var userStore = new Microsoft.AspNet.Identity.EntityFramework.UserStore <Microsoft.AspNet.Identity.EntityFramework.IdentityUser>();
            var manager   = new Microsoft.AspNet.Identity.UserManager <Microsoft.AspNet.Identity.EntityFramework.IdentityUser>(userStore);

            manager.UserTokenProvider =
                new Microsoft.AspNet.Identity.EmailTokenProvider <Microsoft.AspNet.Identity.EntityFramework.IdentityUser>();


            var result = await manager.ConfirmEmailAsync(id, token);

            if (result.Succeeded)
            {
                TempData["Confirmed"] = true;
                return(RedirectToAction("LogOn"));
            }
            else
            {
                return(HttpNotFound());
            }
        }