Exemplo n.º 1
0
 public ActionResult Edit(Profile profile)
 {
     profile.User = _db.Users.Single(u => u.UserId == (Guid)profile.UserId);
     var providerUserKey = Membership.GetUser().ProviderUserKey;
     if (providerUserKey != null && profile.UserId != (Guid)providerUserKey) { return HttpNotFound(); }
     profile.LastUpdatedDate = DateTime.Now;
     if (ModelState.IsValid)
     {
         _db.Entry(profile).State = EntityState.Modified;
         try
         {
             _db.SaveChanges();
             return RedirectToAction("Details", new { username = profile.User.UserName });
         }
         catch (EntityException es)
         {
             ViewBag.Message = es;
             return View(profile);
         }
     }
     ViewBag.UserId = new SelectList(_db.Users, "UserId", "UserName", profile.UserId);
     return View(profile);
 }
Exemplo n.º 2
0
        public ActionResult Register(RegisterModel model)
        {
            string provider = Request.Form["provider"];
            if (provider != null)
            {

                OAuthWebSecurity.RequestAuthentication(provider, "~/Account/RegisterService");
                return View(model);
            }
            if (ModelState.IsValid)
            {
                // Attempt to register the user
                MembershipCreateStatus createStatus;
                var userid = Membership.CreateUser(model.UserName, model.Password, model.Email, passwordQuestion: null, passwordAnswer: null, isApproved: true, providerUserKey: null, status: out createStatus).ProviderUserKey;

                if (createStatus == MembershipCreateStatus.Success && userid != null)
                {
                    FormsAuthentication.SetAuthCookie(model.UserName, createPersistentCookie: false);
                    // TODO make create profile method for

                        var profile = new Profile { User = _db.Users.Single(u => u.UserId == (Guid)userid),
                        LastUpdatedDate= DateTime.Now};
                        _db.Profiles.Add(profile);
                        _db.SaveChanges();
                        return RedirectToAction("Edit", new RouteValueDictionary(
                                                            new
                                                                {
                                                                    controller = "Profiles",
                                                                    action = "Edit",
                                                                    username = model.UserName
                                                                }));
                 }
                else
                {
                    ModelState.AddModelError("", ErrorCodeToString(createStatus));
                }
            }

            // If we got this far, something failed, redisplay form
            return View(model);
        }