public override void ExecuteResult(ControllerContext context)
 {
     OAuthWebSecurity.RequestAuthentication(Provider, ReturnUrl);
 }
Пример #2
0
 public ActionResult ExternalLogin(string provider)
 {
     OAuthWebSecurity.RequestAuthentication(provider, Url.Action("ExternalLoginCallback"));
     return(RedirectToAction("Index", "Home"));
 }
Пример #3
0
        public virtual ActionResult ExternalLoginCallback(string returnUrl)
        {
            AuthenticationResult result = OAuthWebSecurity.VerifyAuthentication(Url.Action("ExternalLoginCallback", new { ReturnUrl = returnUrl }));

            if (!result.IsSuccessful)
            {
                return(RedirectToAction("ExternalLoginFailure"));
            }

            if (result.ExtraData.Keys.Contains("accesstoken"))
            {
                Session["googletoken"] = result.ExtraData["accesstoken"];
            }

            if (OAuthWebSecurity.Login(result.Provider, result.ProviderUserId, createPersistentCookie: false))
            {
                return(RedirectToLocal(returnUrl));
            }

            if (User.Identity.IsAuthenticated)
            {
                // If the current user is logged in add the new account
                OAuthWebSecurity.CreateOrUpdateAccount(result.Provider, result.ProviderUserId, User.Identity.Name);
                return(RedirectToLocal(returnUrl));
            }
            else
            {
                // User is new, ask for their desired membership name
                //string loginData = OAuthWebSecurity.SerializeProviderUserId(result.Provider, result.ProviderUserId);
                //ViewBag.ProviderDisplayName = OAuthWebSecurity.GetOAuthClientData(result.Provider).DisplayName;
                //ViewBag.ReturnUrl = returnUrl;
                //return View("ExternalLoginConfirmation", new RegisterExternalLoginModel { UserName = result.UserName, ExternalLoginData = loginData });

                var username = result.UserName;

                // Insert a new user into the database
                using (VroemmmContext db = new VroemmmContext())
                {
                    UserProfile user = db.UserProfiles.FirstOrDefault(u => u.UserName.ToLower() == username);
                    // Check if user already exists
                    if (user == null)
                    {
                        // Insert name into the profile table
                        db.UserProfiles.Add(new UserProfile {
                            UserName = username
                        });
                        db.SaveChanges();

                        OAuthWebSecurity.CreateOrUpdateAccount(result.Provider, result.ProviderUserId, username);
                        OAuthWebSecurity.Login(result.Provider, result.ProviderUserId, createPersistentCookie: false);

                        return(RedirectToLocal(returnUrl));
                    }
                    else
                    {
                        ModelState.AddModelError("UserName", "Je hebt al een acccount");
                    }
                }

                return(RedirectToLocal(returnUrl));
            }
        }
Пример #4
0
 public void Dispose()
 {
     OAuthWebSecurity.ClearProviders();
 }
Пример #5
0
 public void RegisterClientThrowsOnNullValue()
 {
     AssertEx.ThrowsArgumentNull(() => OAuthWebSecurity.RegisterClient(null), "client");
 }
Пример #6
0
        public ActionResult Manage(LocalPasswordModel model)
        {
            bool hasLocalAccount = OAuthWebSecurity.HasLocalAccount(WebSecurity.GetUserId(User.Identity.Name));

            ViewBag.HasLocalPassword = hasLocalAccount;
            ViewBag.ReturnUrl        = Url.Action("Manage");
            if (hasLocalAccount)
            {
                if (ModelState.IsValid)
                {
                    // ChangePassword va lever une exception plutôt que de renvoyer la valeur False dans certains scénarios de défaillance.
                    bool changePasswordSucceeded;
                    try
                    {
                        changePasswordSucceeded = WebSecurity.ChangePassword(User.Identity.Name, model.OldPassword, model.NewPassword);
                    }
                    catch (Exception)
                    {
                        changePasswordSucceeded = false;
                    }

                    if (changePasswordSucceeded)
                    {
                        return(RedirectToAction("Manage", new { Message = ManageMessageId.ChangePasswordSuccess }));
                    }
                    else
                    {
                        ModelState.AddModelError("", "Le mot de passe actuel est incorrect ou le nouveau mot de passe n'est pas valide.");
                    }
                }
            }
            else
            {
                // L’utilisateur n’a pas de mot de passe local. Veuillez donc supprimer les erreurs de validation provoquées par un
                // champ OldPassword manquant
                ModelState state = ModelState["OldPassword"];
                if (state != null)
                {
                    state.Errors.Clear();
                }

                if (ModelState.IsValid)
                {
                    try
                    {
                        WebSecurity.CreateAccount(User.Identity.Name, model.NewPassword);
                        return(RedirectToAction("Manage", new { Message = ManageMessageId.SetPasswordSuccess }));
                    }
                    catch (Exception e)
                    {
                        ModelState.AddModelError("", e);
                    }
                }
            }

            // Si nous sommes arrivés là, quelque chose a échoué, réafficher le formulaire
            if (!Request.IsAjaxRequest())
            {
                return(View(model));
            }
            else
            {
                return(PartialView("_Manage", model));
            }
        }