Esempio n. 1
0
        public ActionResult UpdateProfile(UserProfile model)
        {
            int userId = WebSecurity.GetUserId(User.Identity.Name);
            model.UserName = User.Identity.Name;
            model.UserId = userId;
            bool hasLocalAccount = OAuthWebSecurity.HasLocalAccount(userId);
            ViewBag.HasLocalPassword = hasLocalAccount;
            ViewBag.ReturnUrl = Url.Action("Manage");
            if (ModelState.IsValid)
            {
                // ChangePassword will throw an exception rather than return false in certain failure scenarios.
                bool success;
                try
                {
                    using (SecretSantaContext db = new SecretSantaContext())
                    {
                        db.Entry(model).State = EntityState.Modified;
                        db.SaveChanges();
                    }
                    success = true;
                }
                catch (Exception)
                {
                    success = false;
                }

                if (success)
                {
                    return RedirectToAction("Manage", new { Message = ManageMessageId.UpdateProfileSuccess });
                }
                else
                {
                    ModelState.AddModelError("", "Failed to save changes.");
                }
            }

            return View("Manage", new ManageAccountModel() { Password = new LocalPasswordModel(), Profile = model });
        }
Esempio n. 2
0
        public ActionResult ExternalLoginConfirmation(RegisterExternalLoginModel model, string returnUrl)
        {
            string provider = null;
            string providerUserId = null;

            if (User.Identity.IsAuthenticated || !OAuthWebSecurity.TryDeserializeProviderUserId(model.ExternalLoginData, out provider, out providerUserId))
            {
                return RedirectToAction("Manage");
            }

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

                        OAuthWebSecurity.CreateOrUpdateAccount(provider, providerUserId, model.UserName);
                        OAuthWebSecurity.Login(provider, providerUserId, createPersistentCookie: false);

                        return RedirectToLocal(returnUrl);
                    }
                    else
                    {
                        ModelState.AddModelError("UserName", "User name already exists. Please enter a different user name.");
                    }
                }
            }

            ViewBag.ProviderDisplayName = OAuthWebSecurity.GetOAuthClientData(provider).DisplayName;
            ViewBag.ReturnUrl = returnUrl;
            return View(model);
        }