예제 #1
0
        public async Task <ActionResult> ExternalLoginCallback(string returnUrl)
        {
            var loginInfo = await OwinContext.Authentication.GetExternalLoginInfoAsync();

            if (loginInfo == null)
            {
                //go home, invalid callback
                return(RedirectToLocal(returnUrl));
            }

            // Sign in the user with this external login provider if the user already has a login
            var user = await UserManager.FindAsync(loginInfo.Login);

            if (user != null)
            {
                await SignInAsync(user, isPersistent : false);

                return(RedirectToLocal(returnUrl));
            }

            if (loginInfo.Email.IsNullOrWhiteSpace())
            {
                ViewBag.Description = "No email address found in the claims, ensure your OAuth provider is configured to return the Email address";
                return(View("ExternalLoginFailure"));
            }

            // If the user does not have an account, then create one

            user = new UmbracoApplicationMember()
            {
                Name     = loginInfo.ExternalIdentity.Name,
                UserName = loginInfo.Email,
                Email    = loginInfo.Email
            };

            var result = await UserManager.CreateAsync(user);

            if (result.Succeeded)
            {
                result = await UserManager.AddLoginAsync(user.Id, loginInfo.Login);

                if (result.Succeeded)
                {
                    await 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(RedirectToLocal(returnUrl));
                }
            }

            //something went wrong
            AddModelErrors(result);
            return(View("ExternalLoginFailure"));
        }
예제 #2
0
 private async Task SignInAsync(UmbracoApplicationMember member, bool isPersistent)
 {
     OwinContext.Authentication.SignOut(DefaultAuthenticationTypes.ExternalCookie);
     OwinContext.Authentication.SignIn(new AuthenticationProperties()
     {
         IsPersistent = isPersistent
     },
                                       await member.GenerateUserIdentityAsync(UserManager));
 }
        public async Task <ActionResult> ExternalLoginConfirmation(ExternalLoginConfirmationViewModel model, string returnUrl)
        {
            if (User.Identity.IsAuthenticated)
            {
                //go home, already authenticated
                return(RedirectToLocal(returnUrl));
            }

            if (ModelState.IsValid)
            {
                // Get the information about the user from the external login provider
                var info = await OwinContext.Authentication.GetExternalLoginInfoAsync();

                if (info == null)
                {
                    return(View("ExternalLoginFailure"));
                }

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

                var result = await UserManager.CreateAsync(user);

                if (result.Succeeded)
                {
                    result = await UserManager.AddLoginAsync(user.Id, info.Login);

                    if (result.Succeeded)
                    {
                        await 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(RedirectToLocal(returnUrl));
                    }
                }
                AddModelErrors(result);
            }

            ViewBag.ReturnUrl = returnUrl;
            return(View(model));
        }
예제 #4
0
        public async Task <ActionResult> HandleRegisterMember([Bind(Prefix = "registerModel")] RegisterModel model)
        {
            if (ModelState.IsValid == false)
            {
                return(CurrentUmbracoPage());
            }

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

            var result = await UserManager.CreateAsync(user, model.Password);

            if (result.Succeeded)
            {
                await 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>");

                TempData["FormSuccess"] = true;

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

            return(CurrentUmbracoPage());
        }