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);
        }
예제 #2
0
        public async System.Threading.Tasks.Task <ActionResult> Register(string username, string email, string password)
        {
            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);
            var user      = new Microsoft.AspNet.Identity.EntityFramework.IdentityUser()
            {
                UserName = username, Email = email, EmailConfirmed = false
            };

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

            Microsoft.AspNet.Identity.IdentityResult result = await manager.CreateAsync(user, password);

            if (result.Succeeded)
            {
                //I have some options: log them in, or I can send them an email to "Confirm" their account details.'
                //I don't have email set up this week, so we'll come back to that.
                string confirmationToken = await manager.GenerateEmailConfirmationTokenAsync(user.Id);

                string confirmationLink = Request.Url.GetLeftPart(UriPartial.Authority) + "/Account/Confirm/" + user.Id + "?token=" + confirmationToken;

                string apiKey = System.Configuration.ConfigurationManager.AppSettings["SendGrid.ApiKey"];

                SendGrid.ISendGridClient           client = new SendGrid.SendGridClient(apiKey);
                SendGrid.Helpers.Mail.EmailAddress from   = new SendGrid.Helpers.Mail.EmailAddress("*****@*****.**", "Coding Cookware Administrator");

                SendGrid.Helpers.Mail.EmailAddress to = new SendGrid.Helpers.Mail.EmailAddress(email);

                string subject = "Confirm your Coding Cookware Account";

                string htmlContent      = string.Format("<a href=\"{0}\">Confirm Your Account</a>", confirmationLink);
                string plainTextContent = confirmationLink;

                SendGrid.Helpers.Mail.SendGridMessage message = SendGrid.Helpers.Mail.MailHelper.CreateSingleEmail(from, to, subject, plainTextContent, htmlContent);

                SendGrid.Response response = await client.SendEmailAsync(message);

                TempData["EmailAddress"] = email;

                return(RedirectToAction("ConfirmationSent"));


                //Commenting this out: I'm not going to log the user in on registration anymore - I'm going to send them a confirmation email instead.
                //This authentication manager will create a cookie for the current user, and that cookie will be exchanged on each request until the user logs out
                //var authenticationManager = HttpContext.GetOwinContext().Authentication;
                //var userIdentity = await manager.CreateIdentityAsync(user, Microsoft.AspNet.Identity.DefaultAuthenticationTypes.ApplicationCookie);
                //authenticationManager.SignIn(new Microsoft.Owin.Security.AuthenticationProperties() { }, userIdentity);
            }
            else
            {
                ViewBag.Error = result.Errors;
                return(View());
            }

            return(RedirectToAction("Index", "Home"));
        }