public async Task <IActionResult> Register(RegisterViewModel model)
        {
            if (ModelState.IsValid)
            {
                var user = new ApplicationUser {
                    UserName = model.Email, Email = model.Email
                };
                var result = await _userManager.CreateAsync(user, model.Password);

                if (result.Succeeded)
                {
                    // For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=532713
                    // Send an email with this link
                    //var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);
                    //var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: HttpContext.Request.Scheme);
                    //await _emailSender.SendEmailAsync(model.Email, "Confirm your account",
                    //    "Please confirm your account by clicking this link: <a href=\"" + callbackUrl + "\">link</a>");
                    await _signInManager.SignInAsync(user, isPersistent : false);

                    _logger.LogInformation(3, "User created a new account with password.");
                    return(RedirectToLocal("/"));
//                    return RedirectToAction(nameof(HomeController.Index), "Home");
                }
                AddErrors(result);
            }

            // If we got this far, something failed, redisplay form
            return(View(model));
        }
예제 #2
0
        public async System.Threading.Tasks.Task <ActionResult> Register(string username, 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
            };

            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.
                //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"));
        }
예제 #3
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"));
        }
예제 #4
0
        public async Task <AuthResult> CreateAsync(User user)
        {
            user.ThrowIfNull("user");

            var appIdentityUser = IdentityModelFactory.Create(user);
            var identityResult  = await _userManager.CreateAsync(appIdentityUser);

            var appIdentityResult = IdentityModelFactory.Create(identityResult);

            // if create is successful, copy the AppIdentityUser's properties
            // back to the AppUser that was passed in
            if (appIdentityResult.Succeeded)
            {
                user.CopyFrom(appIdentityUser);
            }

            return(appIdentityResult);
        }
예제 #5
0
        public async Task <Microsoft.AspNet.Identity.IdentityResult> RegisterUser(UserModel userModel)
        {
            ApplicationUser user = new ApplicationUser
            {
                UserName = userModel.UserName
            };

            var result = await _userManager.CreateAsync(user, userModel.Password);

            return(result);
        }
        protected virtual async Task <AuthenticateResult> ProcessNewExternalAccountAsync(string provider, string providerId, IEnumerable <Claim> claims)
        {
            var user = await TryGetExistingUserFromExternalProviderClaimsAsync(provider, claims);

            if (user == null)
            {
                user = await InstantiateNewUserFromExternalProviderAsync(provider, providerId, claims);

                if (user == null)
                {
                    throw new InvalidOperationException("CreateNewAccountFromExternalProvider returned null");
                }

                var createResult = await userManager.CreateAsync(user);

                if (!createResult.Succeeded)
                {
                    return(new AuthenticateResult(createResult.Errors.First()));
                }
            }

            var externalLogin     = new Microsoft.AspNet.Identity.UserLoginInfo(provider, providerId);
            var addExternalResult = await userManager.AddLoginAsync(user.Id, externalLogin);

            if (!addExternalResult.Succeeded)
            {
                return(new AuthenticateResult(addExternalResult.Errors.First()));
            }

            var result = await AccountCreatedFromExternalProviderAsync(user.Id, provider, providerId, claims);

            if (result != null)
            {
                return(result);
            }

            return(await SignInFromExternalProviderAsync(user.Id, provider));
        }