Beispiel #1
0
        public void UserLogin(Users viewModel)
        {
            using (var acchelper = new DbHelper.CustomAccount())
            {
                //if (Membership.ValidateUser(viewModel.UserName, viewModel.Password))
                var user = acchelper.GetUser(viewModel.UserName, viewModel.Password);
                if (user != null)
                {
                    using (var acaHelper = new DbHelper.AcademicYear())
                        using (var helper = new DbHelper.User())
                        {
                            //var user = foundUser;//helper.Users.First(u => u.UserName == viewModel.UserName);

                            CustomPrincipalSerializeModel serializeModel = new CustomPrincipalSerializeModel();
                            serializeModel.Id         = user.Id;
                            serializeModel.UserName   = user.UserName;
                            serializeModel.FirstName  = user.FirstName;
                            serializeModel.LastName   = user.LastName;
                            serializeModel.SchoolId   = user.SchoolId ?? 0;
                            serializeModel.TestString = "kkkkk";

                            var sess = acaHelper.GetCurrentSession();
                            if (sess != null)
                            {
                                serializeModel.AcademicYearId = sess.AcademicYearId;
                                serializeModel.SessionId      = sess.Id;
                            }


                            JavaScriptSerializer serializer = new JavaScriptSerializer();

                            string userData = serializer.Serialize(serializeModel);

                            FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket(
                                1,
                                //viewModel.Email,
                                viewModel.UserName,
                                DateTime.Now,
                                DateTime.Now.AddMinutes(15),
                                false,
                                userData);

                            string     encTicket = FormsAuthentication.Encrypt(authTicket);
                            HttpCookie faCookie  = new HttpCookie(FormsAuthentication.FormsCookieName, encTicket);
                            Response.Cookies.Add(faCookie);

                            //var reurl = FormsAuthentication.GetRedirectUrl(viewModel.UserName, true);
                            //if (!string.IsNullOrEmpty(reurl))
                            //{
                            //    UpdateLoginTime(user.Id);
                            //    //FormsAuthentication.RedirectFromLoginPage(user.UserName, true);
                            //    Response.Redirect(reurl);
                            //}
                            //else
                            {
                                //FormsAuthentication.set
                                //return RedirectToAction("Index", "Home");
                                string returnUrl = Request.QueryString["ReturnUrl"] as string;



                                if (returnUrl != null)
                                {
                                    //if (returnUrl.Contains("DashBoard%2fStudent") && roles.Contains("student"))
                                    //{
                                    //}

                                    UpdateLoginTime(user.Id);

                                    var qs      = Request.QueryString.ToString().Replace("ReturnUrl=", "");
                                    var queries = qs.Split(new char[] { '&' });
                                    var i       = 0;
                                    foreach (var q in queries)
                                    {
                                        if (i > 0)
                                        {
                                            returnUrl += "&" + q;
                                        }
                                        i++;
                                    }

                                    Response.Redirect(returnUrl);
                                }
                                else
                                {
                                    //Response.Redirect("~/ViewsSite/Default.aspx");
                                    UpdateLoginTime(user.Id);
                                    Response.Redirect("~/");
                                }
                            }
                        }
                }
            }
        }
Beispiel #2
0
        //protected void CreateUserWizard1_CreatedUser(object sender, EventArgs e)
        //{

        //    var username = CreateUserWizard1.UserName;
        //    var password = CreateUserWizard1.Password;
        //    var email = CreateUserWizard1.Email;
        //    var que = CreateUserWizard1.Question;
        //    var ans = CreateUserWizard1.Answer;
        //    var user = new Academic.DbEntities.User.Users()
        //    {
        //        UserName = CreateUserWizard1.UserName
        //        ,
        //        Password = CreateUserWizard1.Password
        //        ,
        //        Email = CreateUserWizard1.Email
        //        ,
        //        SecurityQuestion = CreateUserWizard1.Question
        //        ,
        //        SecurityAnswer = CreateUserWizard1.Answer

        //    };

        //    using (var helper = new DbHelper.CustomAccount())
        //    {
        //        var success = helper.Register(user);
        //        if (success)
        //            UserLogin(user);
        //    }
        //}

        public void UserLogin(Users viewModel)
        {
            using (var acchelper = new DbHelper.CustomAccount())
            {
                //if (Membership.ValidateUser(viewModel.UserName, viewModel.Password))
                if (acchelper.CheckUser(viewModel.UserName, viewModel.Password))
                {
                    using (var acaHelper = new DbHelper.AcademicYear())
                        using (var helper = new DbHelper.User())
                        {
                            var user = helper.Users.First(u => u.UserName == viewModel.UserName);

                            CustomPrincipalSerializeModel serializeModel = new CustomPrincipalSerializeModel();
                            serializeModel.Id        = user.Id;
                            serializeModel.FirstName = user.FirstName;
                            serializeModel.LastName  = user.LastName;

                            serializeModel.SchoolId = user.SchoolId ?? 0;

                            var sess = acaHelper.GetCurrentSession();
                            if (sess != null)
                            {
                                serializeModel.AcademicYearId = sess.AcademicYearId;
                                serializeModel.SessionId      = sess.Id;
                            }

                            JavaScriptSerializer serializer = new JavaScriptSerializer();

                            string userData = serializer.Serialize(serializeModel);

                            FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket(
                                1,
                                //viewModel.Email,
                                viewModel.UserName,
                                DateTime.Now,

                                DateTime.Now.AddMinutes(15),
                                false,
                                userData);

                            string     encTicket = FormsAuthentication.Encrypt(authTicket);
                            HttpCookie faCookie  = new HttpCookie(FormsAuthentication.FormsCookieName, encTicket);
                            Response.Cookies.Add(faCookie);

                            //FormsAuthentication.set
                            //return RedirectToAction("Index", "Home");
                            string returnUrl = Request.QueryString["ReturnUrl"] as string;
                            if (returnUrl != null)
                            {
                                //if (returnUrl.Contains("DashBoard%2fStudent") && roles.Contains("student"))
                                //{

                                //}
                                Response.Redirect(returnUrl);
                            }
                            else
                            {
                                //Response.Redirect("~/ViewsSite/Default.aspx");
                                Response.Redirect("~/ViewsSite/User/Dashboard/Dashboard.aspx");
                            }
                        }
                }
            }
        }
Beispiel #3
0
        public bool UpdateSchoolInfoInCookie(CustomPrincipal user, int schoolId)
        {
            try
            {
                using (var acchelper = new DbHelper.CustomAccount())
                {
                    //if (Membership.ValidateUser(viewModel.UserName, viewModel.Password))
                    //if (acchelper.CheckUser(viewModel.UserName, viewModel.Password))
                    //{
                    //Response.Cookies.Remove(FormsAuthentication.FormsCookieName);

                    using (var acaHelper = new DbHelper.AcademicYear())
                        using (var helper = new DbHelper.User())
                        {
                            //var user = helper.Users.First(u => u.UserName == userName);

                            CustomPrincipalSerializeModel serializeModel = new CustomPrincipalSerializeModel();
                            serializeModel.Id        = user.Id;
                            serializeModel.FirstName = user.FirstName;
                            serializeModel.LastName  = user.LastName;
                            serializeModel.SchoolId  = schoolId;

                            var sess = acaHelper.GetCurrentSession();
                            if (sess != null)
                            {
                                serializeModel.AcademicYearId = sess.AcademicYearId;
                                serializeModel.SessionId      = sess.Id;
                            }


                            //var acaId = acaHelper.GetCurrentAcademicYear(user.SchoolId);
                            //if (acaId != null)
                            //{
                            //    serializeModel.AcademicYearId = acaId.Id;
                            //    var sess = acaHelper.GetCurrentSession(acaId.Id);
                            //    if (sess != null)
                            //    {
                            //        serializeModel.SessionId = sess.Id;
                            //    }
                            //    //else
                            //    //{
                            //    //    serializeModel.SessionId = 0;
                            //    //}
                            //}

                            JavaScriptSerializer serializer = new JavaScriptSerializer();

                            string userData = serializer.Serialize(serializeModel);

                            FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket(
                                1,
                                //viewModel.Email,
                                user.UserName,
                                DateTime.Now,
                                DateTime.Now.AddMinutes(15),
                                false,
                                userData);

                            string     encTicket = FormsAuthentication.Encrypt(authTicket);
                            HttpCookie faCookie  = new HttpCookie(FormsAuthentication.FormsCookieName, encTicket);
                            Response.Cookies.Set(faCookie);//.Add(faCookie);
                            //Response.Cookies[FormsAuthentication.FormsCookieName]= faCookie;

                            /*    //FormsAuthentication.set
                             * //return RedirectToAction("Index", "Home");
                             * string returnUrl = Request.QueryString["ReturnUrl"] as string;
                             * if (returnUrl != null)
                             * {
                             *  //if (returnUrl.Contains("DashBoard%2fStudent") && roles.Contains("student"))
                             *  //{
                             *
                             *  //}
                             *  Response.Redirect(returnUrl);
                             * }
                             * else
                             * {
                             *  Response.Redirect("~/ViewsSite/Default.aspx");
                             * }*/
                        }
                    //}
                }
                return(true);
            }
            catch (Exception exee)
            {
                return(false);
            }
        }