private async Task SignInAsync(UmbracoApplicationMember member, bool isPersistent)
 {
     this.OwinContext.Authentication.SignOut(DefaultAuthenticationTypes.ExternalCookie);
     this.OwinContext.Authentication.SignIn(new AuthenticationProperties() { IsPersistent = isPersistent },
         await member.GenerateUserIdentityAsync(this.UserManager));
 }
        public async Task<ActionResult> ExternalLoginConfirmation(ExternalLoginConfirmationViewModel model, string returnUrl)
        {
            if (this.User.Identity.IsAuthenticated)
            {
                //go home, already authenticated
                return this.RedirectToLocal(returnUrl);
            }

            if (this.ModelState.IsValid)
            {
                // Get the information about the user from the external login provider
                var info = await this.OwinContext.Authentication.GetExternalLoginInfoAsync();
                if (info == null)
                {
                    return this.View("ExternalLoginFailure");
                }

                var user = new UmbracoApplicationMember()
                {
                    Name = info.ExternalIdentity.Name,
                    UserName = model.Email,
                    Email = model.Email
                };

                var result = await this.UserManager.CreateAsync(user);
                if (result.Succeeded)
                {
                    result = await this.UserManager.AddLoginAsync(user.Id, info.Login);
                    if (result.Succeeded)
                    {
                        await this.SignInAsync(user, isPersistent: 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);
                        // SendEmail(user.Email, callbackUrl, "Confirm your account", "Please confirm your account by clicking this link");

                        return this.RedirectToLocal(returnUrl);
                    }
                }
                this.AddModelErrors(result);
            }

            this.ViewBag.ReturnUrl = returnUrl;
            return this.View(model);
        }
        public async Task<ActionResult> HandleRegisterMember([Bind(Prefix = "registerModel")]RegisterModel model)
        {

            if (this.ModelState.IsValid == false)
            {
                return this.CurrentUmbracoPage();
            }

            var user = new UmbracoApplicationMember()
            {
                UserName = model.UsernameIsEmail || model.Username == null ? model.Email : model.Username,
                Email = model.Email,
                MemberProperties = model.MemberProperties
            };

            var result = await this.UserManager.CreateAsync(user, model.Password);
            if (result.Succeeded)
            {
                await this.SignInAsync(user, isPersistent: 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);
                // await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>");

                this.TempData["FormSuccess"] = true;

                //if there is a specified path to redirect to then use it
                if (model.RedirectUrl.IsNullOrWhiteSpace() == false)
                {
                    return this.Redirect(model.RedirectUrl);
                }
                //redirect to current page by default                
                return this.RedirectToCurrentUmbracoPage();
            }
            else
            {
                this.AddModelErrors(result, "registerModel");
            }

            return this.CurrentUmbracoPage();
        }