Пример #1
0
        protected void loginButton_ServerClick(object sender, EventArgs e)
        {
            var _email    = email.Value;
            var _password = password.Value;

            DatabaseEntities db   = new DatabaseEntities();
            EndUser          user = null;

            try
            {
                try {
                    user = db.EndUser.First(eu => eu.email == _email &&
                                            eu.password == _password && eu.active == true);
                } catch
                {
                    throw new SessionException();
                }

                /* set up session */
                Session.Add("userId", user.id.ToString());
                Session.Add("firstName", user.firstName);
                Session.Add("userType", user.type);
                Session.Add("profilePicture", user.profilePicturePath);
                Session.Timeout = 60;

                /* set up cookies for current login */
                Response.Cookies["login"]["userId"]         = user.id.ToString();
                Response.Cookies["login"]["firstName"]      = user.firstName;
                Response.Cookies["login"]["userType"]       = user.type;
                Response.Cookies["login"]["profilePicture"] = user.profilePicturePath;
                Response.Cookies["login"].Expires           = DateTime.Now.AddDays(30);

                if (user.type == "teacher")
                {
                    Response.Redirect("/teacher_quizzes/all_quizzes.aspx");
                }
                else
                {
                    Response.Redirect("/student_quizzes/open_quizzes.aspx");
                }
            } catch (Exception exception)
            {
                if (exception is System.Threading.ThreadAbortException)
                {
                    return; // safely ignore this exception
                }

                var message = "";

                if (exception is SessionException)
                {
                    message  = "Invalid email or password, please try again!<br>";
                    message += "Don't have an account? Please ";
                    message += "<a href='/signup.aspx'>Sign Up</a>!";
                }
                else
                {
                    message = "Something went wrong!";
                }

                loginErrors.InnerHtml = message;
                loginErrors.Visible   = true;
            }
        }
Пример #2
0
        /* handle the submission of form */
        protected void signUpSubmit_ServerClick(object sender, EventArgs e)
        {
            var Sender = (Button)sender;

            signupMessages.Visible = false;
            signupErrors.Visible   = false;
            String           loginLink = "<span><a href='/login.aspx'>Login</a></span>";
            DatabaseEntities db        = new DatabaseEntities();

            var user = db.EndUser.FirstOrDefault(eu => eu.email == email.Text);

            if (user == null)
            {
                user = new EndUser();
            }

            /* user already exist */
            if ((user.email == email.Text && Sender.CommandName != "update") ||
                (user.email == email.Text &&
                 Sender.CommandName == "update" && email.Text != Sender.CommandArgument))
            {
                String _message = "This email is already registered with us. Please choose another!";
                signupErrors.InnerHtml = _message;
                signupErrors.Visible   = true;
                return;
            }

            /* user password is invalid - in case of update */
            if (Sender.CommandName == "update")
            {
                var originalUser = db.EndUser.First(eu => eu.email == Sender.CommandArgument);
                if (oldPassword.Value != originalUser.password)
                {
                    signupErrors.InnerText = "Invalid old password!";
                    signupErrors.Visible   = true;
                    return;
                }
            }

            /* check image type - if file uploaded */
            if (isProfilePictureSet.Value == "true" && profilePictureFileUpload.HasFile)
            {
                if (profilePictureFileUpload.PostedFile.ContentType != "image/png")
                {
                    signupErrors.InnerText = "Only png files are supported as profile picture!";
                    signupErrors.Visible   = true;
                    return;
                }
            }

            try
            {
                user.email              = email.Text;
                user.password           = password.Value;
                user.firstName          = firstName.Text;
                user.secondName         = secondName.Text;
                user.countryCode        = country.SelectedValue;
                user.city               = city.Text;
                user.active             = true;
                user.profilePicturePath = profilePicture.Src;

                user.gender = "Female";
                if (male.Checked)
                {
                    user.gender = "Male";
                }
                if (unspecified.Checked)
                {
                    user.gender = "Unspecified";
                }

                if (teacher.Checked)
                {
                    user.type = "teacher";
                }
                else
                {
                    user.type = "student";
                }

                if (Sender.CommandName != "update")
                {
                    db.EndUser.Add(user);
                }
                db.SaveChanges();


                /* set profile picture - if user added */
                if (isProfilePictureSet.Value == "true")
                {
                    if (profilePictureFileUpload.HasFile)
                    {
                        var fileInfo = new FileInfo(profilePictureFileUpload.PostedFile.FileName);
                        var path     = "/resources/images/profile_pictures/custom/" + user.id + fileInfo.Extension;
                        profilePictureFileUpload.PostedFile.SaveAs(Server.MapPath("~" + path));
                        user.profilePicturePath = path;
                    }

                    db.SaveChanges();
                }

                if (teacher.Checked)
                {
                    var _teacher = db.Teacher.FirstOrDefault(t => t.userId == user.id);

                    if (_teacher == null)
                    {
                        _teacher = new Teacher();
                    }

                    _teacher.speciality = speciality.Text;
                    _teacher.userId     = user.id;

                    if (Sender.CommandName != "update")
                    {
                        db.Teacher.Add(_teacher);
                    }

                    db.SaveChanges();
                }
                else
                {
                    var _student = db.Student.FirstOrDefault(s => s.userId == user.id);

                    if (_student == null)
                    {
                        _student = new Student();
                    }

                    _student.userId = user.id;

                    if (Sender.CommandName != "update")
                    {
                        db.Student.Add(_student);
                    }

                    db.SaveChanges();
                }
            }
            catch (Exception err)
            {
                var _message = "Something went wrong!";
                signupErrors.InnerText = _message;
                signupErrors.Visible   = true;
                return;
            }

            if (Sender.CommandName == "update")
            {
                Session["userId"]         = user.id.ToString();
                Session["userType"]       = user.type;
                Session["firstName"]      = user.firstName;
                Session["profilePicture"] = user.profilePicturePath;

                Response.Cookies["login"]["userId"]         = user.profilePicturePath;
                Response.Cookies["login"]["firstName"]      = user.profilePicturePath;
                Response.Cookies["login"]["userType"]       = user.profilePicturePath;
                Response.Cookies["login"]["profilePicture"] = user.profilePicturePath;

                Response.Redirect("/profile/profile.aspx?updated=true");
            }

            var message = "Your account has been successfully created!";

            message += "Please " + loginLink + " to continue.";
            signupMessages.Visible   = true;
            signupMessages.InnerHtml = message;
        }