Exemplo n.º 1
0
        public async Task <IActionResult> RegisterAssociatedUser(string userId, string code)
        {
            if (userId == null || code == null)
            {
                return(RedirectToAction(nameof(HomeController.Index), "Home"));
            }
            var user = await _userManager.FindByIdAsync(userId);

            if (user == null)
            {
                throw new ApplicationException($"Unable to load user with ID '{userId}'.");
            }
            /* Get the requestor display name */
            string displayName = string.Format("{0} {1} {2}", user.FirstName, user.MiddleName, user.LastName);
            UserConformationRequests userConformationRequest = _context.UserConformationRequests.Where(a => a.RegistrationCode == code).FirstOrDefault();
            /* Save the display name to the view model. */
            RegisterViewModel model = new RegisterViewModel();

            model.RequestorDisplayName   = displayName;
            model.IsLinkUserRegistration = true;
            model.Email       = (userConformationRequest == null) ? "" : userConformationRequest.Email.ToString();
            model.RequestorID = userId;
            var result = await _userManager.ConfirmEmailAsync(user, code);

            return(View(model));
        }
Exemplo n.º 2
0
 private bool ConfirmFriendsUserRequest(string ConformationCode)
 {
     try
     {
         /* Look up the original request. */
         UserConformationRequests originalRequest = _context.UserConformationRequests.Where(a => a.RegistrationCode == ConformationCode).FirstOrDefault();
         if (originalRequest != null)
         {
             originalRequest.IsConfirmed   = 1;
             originalRequest.DateConfirmed = DateTime.Now;
             _context.UserConformationRequests.Update(originalRequest);
             _context.SaveChanges();
             return(true);
         }
         else
         {
             return(false);
         }
     }
     catch (Exception)
     {
         throw new ApplicationException($"Failed to confirm friends request using the following code {ConformationCode}.");
     }
     return(false);
 }
        public async Task <IActionResult> DeleteConfirmed(int id)
        {
            try
            {
                var user = await _userManager.GetUserAsync(User);

                if (user == null)
                {
                    throw new ApplicationException($"Unable to load user with ID '{_userManager.GetUserId(User)}'.");
                }
                UserConformationRequests friendRequest = _context.UserConformationRequests.Where(a => a.RequestedUserID == user.Id && a.ID == id).FirstOrDefault();
                if (friendRequest != null)
                {
                    _context.UserConformationRequests.Remove(friendRequest);
                    await _context.SaveChangesAsync();

                    return(RedirectToAction(nameof(FriendRequests)));
                }
                else
                {
                    throw new ApplicationException($"Friend Request ID '{id}' does not exists.");
                }
            }
            catch (Exception ex)
            {
                _logger.LogError(string.Format("{0} - Error Message: {1}", System.Reflection.MethodBase.GetCurrentMethod(), ex.Message));
                return(RedirectToAction("Index", "StatusCode", 500));
            }
        }
        public async Task <IActionResult> SendFriendRequest(IndexViewModel model)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    return(View(model));
                }

                var user = await _userManager.GetUserAsync(User);

                if (user == null)
                {
                    throw new ApplicationException($"Unable to load user with ID '{_userManager.GetUserId(User)}'.");
                }
                /* Send the friend request via email to your friend.*/
                var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);

                var    callbackUrl             = Url.AssociatedUserLink(user.Id, code, Request.Scheme);
                var    associatedUserEmail     = model.Email;
                var    associatedUserFirstName = model.AssociatedUserFirstName;
                var    associatedUserLastName  = model.AssociatedUserLastName;
                string websiteurl = Url.Action("Index", "Home");
                await _emailSender.SendParentRequestEmailConfirmationAsync(associatedUserEmail, callbackUrl, user.FirstName, associatedUserFirstName, websiteurl);

                /* Save the request to database */

                var request = new UserConformationRequests
                {
                    Email            = associatedUserEmail,
                    DateSent         = DateTime.Now,
                    IsConfirmed      = 0,
                    ExpiredDate      = DateTime.Now.AddDays(7),
                    RequestedUserID  = user.Id,
                    FirstName        = associatedUserFirstName,
                    LastName         = associatedUserLastName,
                    RegistrationCode = code
                };
                _context.UserConformationRequests.Add(request);
                _context.SaveChanges();
                StatusMessage = "Parent link request sent. Please have then check their email your email.";
                return(RedirectToAction(nameof(Friends)));
            }
            catch (Exception ex)
            {
                _logger.LogError(string.Format("{0} - Error Message: {1}", System.Reflection.MethodBase.GetCurrentMethod(), ex.Message));
                return(RedirectToAction("Index", "StatusCode", 500));
            }
        }
Exemplo n.º 5
0
        public async Task <IActionResult> Register(string userId = null, string code = null, string returnUrl = null)
        {
            try
            {
                RegisterViewModel model = new RegisterViewModel();
                ViewData["ReturnUrl"] = returnUrl;
                // If the user id and code is not provided, then its a brand new user.
                if (userId == null && code == null)
                {
                    model.IsLinkUserRegistration = false;
                    return(View(model));
                    //return RedirectToLocal("/StatusCode/500");
                    //return RedirectToAction("Index", "StatusCode", 500);
                    //return RedirectToAction(nameof(HomeController.Index), "Home");
                }
                else
                {
                    var user = await _userManager.FindByIdAsync(userId);

                    if (user == null)
                    {
                        throw new ApplicationException($"Unable to load user with ID '{userId}'.");
                    }
                    /* Capture  the requestor info. */
                    string displayName = string.Format("{0} {1} {2}", user.FirstName, user.MiddleName, user.LastName);
                    UserConformationRequests friendRequest = _context.UserConformationRequests.Where(a => a.RegistrationCode == code).FirstOrDefault();

                    /* Save the requestor info into the register view model */
                    model.RequestorDisplayName   = displayName;
                    model.IsLinkUserRegistration = true;
                    model.Email            = (friendRequest == null) ? "" : friendRequest.Email.ToString();
                    model.RequestorID      = userId;
                    model.RegistrationCode = code;
                    //var result = await _userManager.ConfirmEmailAsync(user, code);
                    return(View(model));
                }
            }
            catch (Exception ex)
            {
                _logger.LogError(string.Format("{0} - Error Message: {1}", System.Reflection.MethodBase.GetCurrentMethod(), ex.Message));
                return(RedirectToAction("Index", "StatusCode", 500));
            }
        }