예제 #1
0
        /// <summary>
        /// Redirects to login page.
        /// </summary>
        public void RedirectToLoginPage(bool includeReturnUrl)
        {
            var context = HttpContext.Current;

            var pageReference = LoginPageReference;

            if (includeReturnUrl)
            {
                var parms = new Dictionary <string, string>();

                var returnUrl = context.Request.QueryString["returnUrl"];
                if (returnUrl == null)
                {
                    // if there is a rckipid token, we don't want to include it when they go to login page since they are going there to login as a real user
                    // this also prevents an issue where they would log in as a real user, but then get logged in with the token instead after they are redirected
                    returnUrl = context.Server.UrlEncode(PersonToken.RemoveRockMagicToken(context.Request.RawUrl));
                }

                parms.Add("returnurl", returnUrl);
                pageReference.Parameters = parms;
            }

            context.Response.Redirect(pageReference.BuildUrl(), false);
            context.ApplicationInstance.CompleteRequest();
        }
예제 #2
0
        public async Task <IActionResult> Confirminvate(int?id, string token, int?familyId)
        {
            if (id == null || token == null || familyId == null)
            {
                return(NotFound());
            }
            PersonToken personToken = db.PersonTokens.FirstOrDefault(x => x.Code == token);

            if (personToken.Date.AddDays(1) < DateTime.Now)
            {
                return(NotFound());
            }
            if (personToken == null)
            {
                return(NotFound());
            }

            User user = await userManager.FindByIdAsync(personToken.UserId);

            if (user == null)
            {
                return(NotFound());
            }
            ConfirmInvateVM vm = new ConfirmInvateVM
            {
                Id       = (int)id,
                Token    = token,
                FamilyId = (int)familyId
            };

            return(View(vm));
        }
예제 #3
0
        public override void OnActionExecuting(HttpActionContext actionContext)
        {
            var value = actionContext.Request.Headers.Authorization;

            try
            {
                PersonToken pt = db.PersonToken.Where(x => x.Token == value.Scheme).SingleOrDefault();
                if (pt != null)
                {
                    pt.DateLastLogin = DateTime.Now;
                    db.SaveChanges();
                }
                else
                {
                    //dynamic cResponse = new ExpandoObject();
                    //cResponse.Result = "-1";
                    //cResponse.Feedback = "Valid token could not found";
                    //JsonConvert.DeserializeObject(JsonConvert.SerializeObject(cResponse));

                    actionContext.Response = new HttpResponseMessage(HttpStatusCode.Unauthorized);
                }
            }
            catch (Exception ex)
            {
                actionContext.Response = new HttpResponseMessage(HttpStatusCode.Unauthorized);
            }

            actionContext.ActionArguments[ParameterName] = value;
        }
예제 #4
0
        /// <summary>
        /// Handles the Click event of the bbtnLogin control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
        protected void bbtnLogin_Click(object sender, EventArgs e)
        {
            var context     = HttpContext.Current;
            var queryParams = new Dictionary <string, string>();

            queryParams.Add("returnUrl", context.Server.UrlEncode(PersonToken.RemoveRockMagicToken(context.Request.RawUrl)));

            NavigateToLinkedPage(AttributeKey.LoginPage, queryParams);
        }
예제 #5
0
        /// <summary>
        /// Gets the return URL to be used during a redirect.
        /// </summary>
        /// <returns></returns>
        private static string GetReturnUrl()
        {
            var context = RockRequestContext.Current;

            var returnUrl = context.PageParameter("returnUrl");

            if (string.IsNullOrWhiteSpace(returnUrl))
            {
                returnUrl = Uri.EscapeDataString(PersonToken.RemoveRockMagicToken(context.RawUrl));
            }

            return(returnUrl);
        }
예제 #6
0
 /// <summary>
 /// Copies the base properties from a source PersonToken object
 /// </summary>
 /// <param name="source">The source.</param>
 public void CopyPropertiesFrom(PersonToken source)
 {
     this.Id               = source.Id;
     this.ExpireDateTime   = source.ExpireDateTime;
     this.ForeignGuid      = source.ForeignGuid;
     this.ForeignKey       = source.ForeignKey;
     this.LastUsedDateTime = source.LastUsedDateTime;
     this.PageId           = source.PageId;
     this.PersonAliasId    = source.PersonAliasId;
     this.TimesUsed        = source.TimesUsed;
     this.UsageLimit       = source.UsageLimit;
     this.Guid             = source.Guid;
     this.ForeignId        = source.ForeignId;
 }
예제 #7
0
        public RckipidToken GetToken()
        {
            var person = GetPerson();

            if (person == null)
            {
                return(null);
            }
            var expiration = Rock.RockDateTime.Now.AddDays(7);
            var token      = PersonToken.CreateNew(person.PrimaryAlias, expiration, 1, null);

            return(new RckipidToken
            {
                Expiration = expiration,
                Token = token
            });
        }
예제 #8
0
        /// <summary>
        /// Gets the login URL with return URL.
        /// </summary>
        /// <returns></returns>
        public string GetLoginUrlWithReturnUrl()
        {
            if (LoginPageId is null)
            {
                return(string.Empty);
            }

            var context       = HttpContext.Current;
            var pageReference = LoginPageReference;
            var parms         = new Dictionary <string, string>();

            // if there is a rckipid token, we don't want to include it when they go to login page since they are going there to login as a real user
            // this also prevents an issue where they would log in as a real user, but then get logged in with the token instead after they are redirected
            var returnUrl = context.Request.QueryString["returnUrl"] ??
                            context.Server.UrlEncode(PersonToken.RemoveRockMagicToken(context.Request.RawUrl));

            parms.Add("returnurl", returnUrl);
            pageReference.Parameters = parms;
            var url = pageReference.BuildUrl();

            return(url);
        }
예제 #9
0
        public static string TokenCheck(string token)
        {
            MomenticEntities db = new MomenticEntities();

            try
            {
                PersonToken pt = db.PersonToken.Where(x => x.Token == token).SingleOrDefault();
                if (pt != null)
                {
                    pt.DateLastLogin = DateTime.Now;
                    db.SaveChanges();
                    return("0");
                }
                else
                {
                    return("-1");
                }
            }
            catch (Exception ex)
            {
                return("-1");
            }
        }
예제 #10
0
        public static string TokenCreate(int personID)
        {
            MomenticEntities db = new MomenticEntities();

            try
            {
                string temp = Guid.NewGuid().ToString();

                PersonToken pt = new PersonToken();
                pt.DateCreated   = DateTime.Now;
                pt.DateLastLogin = DateTime.Now;
                pt.PersonID      = personID;
                pt.Token         = temp;

                db.PersonToken.Add(pt);
                db.SaveChanges();

                return(temp);
            }
            catch (Exception ex)
            {
                return("-1");
            }
        }
예제 #11
0
        public async Task <IActionResult> Confirminvate(ConfirmInvateVM vm)
        {
            if (!ModelState.IsValid)
            {
                return(View(vm));
            }
            PersonToken personToken = db.PersonTokens.FirstOrDefault(x => x.Code == vm.Token);

            if (personToken == null)
            {
                return(NotFound());
            }
            if (personToken.Date.AddDays(1) < DateTime.Now)
            {
                return(NotFound());
            }
            User user = await userManager.FindByIdAsync(personToken.UserId);

            if (user == null)
            {
                return(NotFound());
            }

            User invateUser = await userManager.FindByEmailAsync(personToken.Email);

            if (invateUser != null)
            {
                return(NotFound());
            }
            Person person = db.People.Include(x => x.UserToPerson).FirstOrDefault(x => x.Id == vm.Id && x.FamilyId == vm.FamilyId);

            if (person == null)
            {
                return(NotFound());
            }
            if (person.UserToPerson != null)
            {
                return(NotFound());
            }
            int familId = FamlyMethods.GetFamilyId(db, user);

            if (familId != vm.FamilyId)
            {
                return(NotFound());
            }

            User newUser = new User
            {
                Firstname      = person.Firstname,
                Lastname       = person.LastName,
                Email          = personToken.Email,
                BirthDate      = person.Birthdate,
                UserName       = person.Firstname.Trim() + person.LastName.Trim() + Guid.NewGuid().ToString(),
                EmailConfirmed = true,
                Avatar         = person.Photo
            };

            switch (person.GenderId)
            {
            case 1:
                newUser.GenderId = 1;
                newUser.Avatar   = "default1.jpg";
                break;

            case 2:
                newUser.GenderId = 2;
                newUser.Avatar   = "default2.jpg";
                break;
            }
            IdentityResult identityResult = await userManager.CreateAsync(newUser, vm.Password);

            if (!identityResult.Succeeded)
            {
                foreach (var er in identityResult.Errors)
                {
                    ModelState.AddModelError("", er.Description);
                }

                return(View(vm));
            }
            await userManager.AddToRoleAsync(newUser, Utilities.SD.MemberRole);

            await userManager.UpdateAsync(newUser);

            await signInManager.SignInAsync(newUser, true);

            FamilyToUser familyToUser = new FamilyToUser {
                FamilyId = vm.FamilyId, UserId = newUser.Id
            };
            await db.FamilyToUsers.AddAsync(familyToUser);

            db.SaveChanges();
            UserToPerson userToPerson = new UserToPerson {
                PersonId = person.Id, UserId = newUser.Id
            };

            db.UserToPeople.Add(userToPerson);
            db.PersonTokens.Remove(personToken);
            db.SaveChanges();
            return(RedirectToAction("Index", "Home"));
        }
예제 #12
0
        public async Task <IActionResult> Invate(InvateEmail invateEmail)
        {
            if (!ModelState.IsValid)
            {
                return(View(invateEmail));
            }
            User user = await userManager.FindByNameAsync(User.Identity.Name);

            User invateUser = await userManager.FindByEmailAsync(invateEmail.Email);

            if (invateUser != null)
            {
                ViewBag.Error = "User already exists";
                return(View());
            }
            Person person = db.People.Include(x => x.UserToPerson).FirstOrDefault(x => x.Id == invateEmail.PersonId && x.FamilyId == invateEmail.FamilyId);

            if (person == null)
            {
                return(NotFound());
            }
            if (person.UserToPerson != null)
            {
                ViewBag.Error = "User already exists";
                return(View());
            }
            int familId = FamlyMethods.GetFamilyId(db, user);

            if (familId != invateEmail.FamilyId)
            {
                return(NotFound());
            }
            try
            {
                PersonToken token = new PersonToken
                {
                    Date     = DateTime.Now,
                    PersonId = person.Id,
                    UserId   = user.Id,
                    Code     = Guid.NewGuid().ToString(),
                    Email    = invateEmail.Email
                };
                #region Sending Email Invate Message
                SmtpClient client = new SmtpClient("smtp.gmail.com", 587);
                client.UseDefaultCredentials = false;
                client.EnableSsl             = true;
                client.Credentials           = new NetworkCredential(configuration["ConnectionStrings:SmtpClientCredentialEmail"], configuration["ConnectionStrings:SmtpClientCredentialPassword"]);

                MailMessage message = new MailMessage(configuration["ConnectionStrings:SmtpClientCredentialEmail"], invateEmail.Email);
                message.IsBodyHtml = true;
                message.Subject    = "Confirm invate";
                message.Body       = $"<table style='width:100%;background-color:#fbfbfb;padding:50px'><thead style ='width:100%;display:flex;justify-content:center;'><tr style ='width:100%;display:flex;justify-content:center;'><th style ='width:100%;color:#7e0f9a;font-family:Roboto, sans-serif;font-weight:400;font-size:50px'>Family Tree</th></tr><thead><tbody><tr><td style ='padding:30px 0px;color:#353535;font-family:Roboto Condensed, sans-serif;font-size:20px;'> Dear user, a friend invited you to his family. Click the 'Verify İnvate' button below to verify your invate.</td></tr><tr><td style ='font-family:Roboto Condensed, sans-serif;text-align:center;'><a href='https://localhost:44341/account/confirminvate?id={person.Id}&token={token.Code}&familyId={familId}' style ='text-decoration:none;padding:10px 30px;border-radius:3px;background-color:#8d11ff;color:black;font-weight:lighter;font-size:20px;cursor:pointer;'>Confirm account</a></td></tr></tbody></table>";

                client.Send(message);

                db.PersonTokens.Add(token);
                db.SaveChanges();
                #endregion
            }
            catch
            {
                ViewBag.Error = "An error occurred";
                return(View());
            }
            TempData["invate"] = true;
            return(View());
        }