示例#1
0
        public async Task <ActionResult> ExternalLoginCallback(string returnUrl)
        {
            if (string.IsNullOrEmpty(returnUrl))
            {
                returnUrl = "~/";
            }

            var loginInfo = await AuthenticationManager.GetExternalLoginInfoAsync();

            if (loginInfo == null)
            {
                return(RedirectToAction("LogOn"));
            }

            // AUTHENTICATED!
            var providerKey = loginInfo.Login.ProviderKey;


            // Aplication specific code goes here.
            var userBus = new busUser();
            var user    = userBus.ValidateUserWithExternalLogin(providerKey);

            if (user == null)
            {
                return(RedirectToAction("LogOn", new
                {
                    message = "Unable to log in with " + loginInfo.Login.LoginProvider +
                              ". " + userBus.ErrorMessage
                }));
            }

            // store on AppUser
            AppUserState appUserState = new AppUserState();

            appUserState.FromUser(user);

            // write the authentication cookie
            IdentitySignin(appUserState, providerKey, isPersistent: true);

            return(Redirect(returnUrl));
        }
示例#2
0
        public async Task <ActionResult> ExternalLinkLoginCallback()
        {
            // Handle external Login Callback
            var loginInfo = await AuthenticationManager.GetExternalLoginInfoAsync(XsrfKey, AppUserState.UserId);

            if (loginInfo == null)
            {
                IdentitySignout(); // to be safe we log out
                return(RedirectToAction("Register", new { message = "Unable to authenticate with external login." }));
            }

            // Authenticated!
            string providerKey  = loginInfo.Login.ProviderKey;
            string providerName = loginInfo.Login.LoginProvider;

            // Now load, create or update our custom user

            // normalize email and username if available
            if (string.IsNullOrEmpty(AppUserState.Email))
            {
                AppUserState.Email = loginInfo.Email;
            }
            if (string.IsNullOrEmpty(AppUserState.Name))
            {
                AppUserState.Name = loginInfo.DefaultUserName;
            }

            var  userBus = new busUser();
            User user    = null;

            if (!string.IsNullOrEmpty(AppUserState.UserId))
            {
                user = userBus.Load(AppUserState.UserId);
            }

            if (user == null && !string.IsNullOrEmpty(providerKey))
            {
                user = userBus.LoadUserByProviderKey(providerKey);
            }

            if (user == null && !string.IsNullOrEmpty(loginInfo.Email))
            {
                user = userBus.LoadUserByEmail(loginInfo.Email);
            }

            if (user == null)
            {
                user = userBus.NewEntity();
                userBus.SetUserForEmailValidation(user);
            }

            if (string.IsNullOrEmpty(user.Email))
            {
                user.Email = AppUserState.Email;
            }

            if (string.IsNullOrEmpty(user.Name))
            {
                user.Name = AppUserState.Name ?? "Unknown (" + providerName + ")";
            }


            if (loginInfo.Login != null)
            {
                user.OpenIdClaim = loginInfo.Login.ProviderKey;
                user.OpenId      = loginInfo.Login.LoginProvider;
            }
            else
            {
                user.OpenId      = null;
                user.OpenIdClaim = null;
            }

            // finally save user inf
            bool result = userBus.Save(user);

            // update the actual identity cookie
            AppUserState.FromUser(user);
            IdentitySignin(AppUserState, loginInfo.Login.ProviderKey);

            return(RedirectToAction("Register"));
        }
        public async Task<ActionResult> ExternalLoginCallback(string returnUrl)
        {
            if (string.IsNullOrEmpty(returnUrl))
                returnUrl = "~/";

            var loginInfo = await AuthenticationManager.GetExternalLoginInfoAsync();
            if (loginInfo == null)
                return RedirectToAction("LogOn");

            // AUTHENTICATED!
            var providerKey = loginInfo.Login.ProviderKey;


            // Aplication specific code goes here.
            var userBus = new busUser();
            var user = userBus.ValidateUserWithExternalLogin(providerKey);
            if (user == null)
            {
                return RedirectToAction("LogOn", new
                {
                    message = "Unable to log in with " + loginInfo.Login.LoginProvider +
                              ". " + userBus.ErrorMessage
                });
            }

            // store on AppUser
            AppUserState appUserState = new AppUserState();
            appUserState.FromUser(user);

            // write the authentication cookie
            IdentitySignin(appUserState, providerKey, isPersistent: true);

            return Redirect(returnUrl);
        }
示例#4
0
        public ActionResult Register(FormCollection formVars)
        {
            string id = formVars["Id"];

            if (!string.IsNullOrEmpty(formVars["btnDeleteAccount"]))
            {
                if (string.IsNullOrEmpty(AppUserState.UserId))
                {
                    return(View("Register", ViewModel));
                }

                if (!busUser.Delete(AppUserState.UserId))
                {
                    ViewModel.ErrorDisplay.ShowError("Unable to delete this account: " + busUser.ErrorMessage);
                }
                else
                {
                    IdentitySignout();
                    return(RedirectToAction("New", "Snippet"));
                }

                return(View("Register", ViewModel));
            }

            ViewData["IsNew"] = false;

            string confirmPassword = formVars["confirmPassword"];

            bool isNew = false;
            User user  = null;

            if (string.IsNullOrEmpty(id) || busUser.Load(id) == null)
            {
                user = busUser.NewEntity();
                ViewData["IsNew"] = true;

                // not validated yet
                user.InActive = true;
                isNew         = true;
            }
            else
            {
                user = busUser.Entity;
            }

            UpdateModel <User>(busUser.Entity,
                               new string[] { "Name", "Email", "Password", "Theme" });

            if (ModelState.Count > 0)
            {
                ErrorDisplay.AddMessages(ModelState);
            }

            if (string.IsNullOrEmpty(user.OpenId) &&
                confirmPassword != user.Password)
            {
                ErrorDisplay.AddMessage("Please make sure both password values match.", "confirmPassword");
            }


            if (ErrorDisplay.DisplayErrors.Count > 0)
            {
                return(View("Register", ViewModel));
            }

            if (!busUser.Validate())
            {
                ErrorDisplay.Message = "Please correct the following:";
                ErrorDisplay.AddMessages(busUser.ValidationErrors);
                return(View("Register", ViewModel));
            }

            if (!busUser.Save())
            {
                ErrorDisplay.ShowError("Unable to save User: "******"Register", ViewModel));
            }

            AppUserState appUserState = new AppUserState();

            appUserState.FromUser(user);
            IdentitySignin(appUserState, appUserState.UserId);

            if (isNew)
            {
                SetAccountForEmailValidation();

                ErrorDisplay.HtmlEncodeMessage = false;
                ErrorDisplay.ShowMessage(
                    @"Thank you for creating an account...
<hr />
<p>Before you can post and save new CodePastes we need to
verify your email address.</p>
<p>We just sent you an email with a confirmation
code. Please follow the instructions in the email 
to validate your email address.</p>");

                return(View("Register", ViewModel));
            }


            return(RedirectToAction("New", "Snippet", null));
        }
        public ActionResult Register(FormCollection formVars)
        {
            string id = formVars["Id"];

            if (!string.IsNullOrEmpty(formVars["btnDeleteAccount"]))
            {
                if (string.IsNullOrEmpty(AppUserState.UserId))
                    return View("Register", ViewModel);

                if (!busUser.Delete(AppUserState.UserId))
                    ViewModel.ErrorDisplay.ShowError("Unable to delete this account: " + busUser.ErrorMessage);
                else
                {
                    IdentitySignout();
                    return RedirectToAction("New", "Snippet");
                }

                return View("Register", ViewModel);
            }

            ViewData["IsNew"] = false;

            string confirmPassword = formVars["confirmPassword"];

            bool isNew = false;
            User user = null;
            if (string.IsNullOrEmpty(id) || busUser.Load(id) == null)
            {
                user = busUser.NewEntity();
                ViewData["IsNew"] = true;

                // not validated yet
                user.InActive = true;
                isNew = true;
            }
            else
                user = busUser.Entity;

            UpdateModel<User>(busUser.Entity,
                new string[] {"Name", "Email", "Password", "Theme"});

            if (ModelState.Count > 0)
                ErrorDisplay.AddMessages(ModelState);

            if (string.IsNullOrEmpty(user.OpenId) &&
                confirmPassword != user.Password)
                ErrorDisplay.AddMessage("Please make sure both password values match.", "confirmPassword");


            if (ErrorDisplay.DisplayErrors.Count > 0)
                return View("Register", ViewModel);

            if (!busUser.Validate())
            {
                ErrorDisplay.Message = "Please correct the following:";
                ErrorDisplay.AddMessages(busUser.ValidationErrors);
                return View("Register", ViewModel);
            }

            if (!busUser.Save())
            {
                ErrorDisplay.ShowError("Unable to save User: "******"Register", ViewModel);
            }

            AppUserState appUserState = new AppUserState();
            appUserState.FromUser(user);
            IdentitySignin(appUserState, appUserState.UserId);

            if (isNew)
            {
                SetAccountForEmailValidation();

                ErrorDisplay.HtmlEncodeMessage = false;
                ErrorDisplay.ShowMessage(
                    @"Thank you for creating an account...
<hr />
<p>Before you can post and save new CodePastes we need to
verify your email address.</p>
<p>We just sent you an email with a confirmation
code. Please follow the instructions in the email 
to validate your email address.</p>");

                return View("Register", ViewModel);
            }


            return RedirectToAction("New", "Snippet", null);
        }