Beispiel #1
0
        public async Task <ActionResult> Register(RegisterViewModel model)
        {
            if (ModelState.IsValid)
            {
                ApplicationUser user = new ApplicationUser {
                    UserName = model.Email, Email = model.Email
                };
                var result = await UserManager.CreateAsync(user, model.Password);

                if (result.Succeeded)
                {
                    UserManager.AddToRole(user.Id, "User");
                    await SignInManager.SignInAsync(user, isPersistent : false, rememberBrowser : false);

                    // For more information on how to enable account confirmation and password reset please visit https://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>");



                    // create the profile page !!
                    //return RedirectToAction("CreateProfile", "Profile", user);
                    ApplicationDbContext db = new ApplicationDbContext();
                    var profile             = new Profile();
                    profile.UserId    = user.Id;
                    profile.IsPrivate = true;
                    profile.Nickname  = user.Email;
                    profile.Email     = user.Email;

                    var friendsProfile = new FriendsProfile();
                    friendsProfile.UserId = user.Id;
                    //friendsProfile.User = user;

                    try
                    {
                        if (ModelState.IsValid)
                        {
                            db.Profiles.Add(profile);
                            db.SaveChanges();
                        }
                        else
                        {
                            return(RedirectToAction("Index", "Profile"));
                        }

                        if (ModelState.IsValid)
                        {
                            db.FriendsProfiles.Add(friendsProfile);
                            db.SaveChanges();
                        }
                        else
                        {
                            return(RedirectToAction("Index", "Profile"));
                        }


                        if (!ModelState.IsValid)
                        {
                            return(View(model));
                        }

                        // This doesn't count login failures towards account lockout
                        // To enable password failures to trigger account lockout, change to shouldLockout: true
                        string returnUrl = "";
                        var    result2   = await SignInManager.PasswordSignInAsync(model.Email, model.Password, false, shouldLockout : false);

                        switch (result2)
                        {
                        case SignInStatus.Success:
                            return(RedirectToAction("Index", "Home"));

                        case SignInStatus.LockedOut:
                            return(View("Lockout"));

                        case SignInStatus.RequiresVerification:
                            return(RedirectToAction("SendCode", new { ReturnUrl = returnUrl, RememberMe = false }));

                        case SignInStatus.Failure:
                        default:
                            ModelState.AddModelError("", "Invalid login attempt.");
                            return(View("Login", model));
                        }
                    }
                    catch (Exception e)
                    {
                        return(RedirectToAction("Index", "Profile"));
                    }

                    //return RedirectToAction("Index", "Home");
                }
                AddErrors(result);
            }

            // If we got this far, something failed, redisplay form
            return(View(model));
        }
Beispiel #2
0
        public ActionResult AcceptRequest(int id)
        {
            string currId = User.Identity.GetUserId();

            //get the involved profiles
            Profile currentProfile = new Profile();
            Profile friendProfile  = new Profile();

            //get currentprofile from the database
            var varCurrentProfile = from p in db.Profiles
                                    where p.UserId == currId
                                    select p;

            //save it in currentProfile
            foreach (var elem in varCurrentProfile)
            {
                currentProfile = elem;
                break;
            }
            //find the friend Profile
            friendProfile = db.Profiles.Find(id);
            //find the profiles for the friend request
            FriendsProfile friendsCurrentProfile = new FriendsProfile();
            FriendsProfile friendsNextProfile    = new FriendsProfile();

            //get the profile from database and save it in friendsNextProfile.
            var varFriends = from f in db.FriendsProfiles
                             where f.UserId == friendProfile.UserId
                             select f;

            foreach (var elem in varFriends)
            {
                friendsNextProfile = elem;
                break;
            }

            var varFriendsCurr = from f in db.FriendsProfiles
                                 where f.UserId == currentProfile.UserId
                                 select f;

            foreach (var elem in varFriendsCurr)
            {
                friendsCurrentProfile = elem;
                break;
            }

            try
            {
                if (TryUpdateModel(currentProfile))
                {
                    currentProfile.Friends.Add(friendsNextProfile);
                    db.SaveChanges();
                }
                else
                {
                    ViewBag.Test = "1"; return(View("FailedProfile"));
                }

                if (TryUpdateModel(friendProfile))
                {
                    friendProfile.Friends.Add(friendsCurrentProfile);
                    friendProfile.FriendRequests.Remove(currentProfile);
                    db.SaveChanges();
                }
                else
                {
                    ViewBag.Test = "2"; return(View("FailedProfile"));
                }
            }
            catch (Exception e)
            {
                ViewBag.Test = e.ToString();
                return(View("FailedProfile"));
            }
            return(RedirectToAction("ShowFriendRequest", "Profiles"));
        }