Пример #1
0
        protected void ButtonLogin_Click(object sender, EventArgs e)
        {
            if (TextBoxUsername.Text.Length == 0 || TextBoxPassword.Text.Length == 0) //blank information
            {
                LabelError.Visible = true;
                LabelError.Text    = "Please enter the username and password!";
            }
            else
            {
                string username = TextBoxUsername.Text;
                Regex  regex    = new Regex(@"^([\w\.\-]+)@([\w\-]+)((\.(\w){2,3})+)$");
                Match  match    = regex.Match(username);
                int    mode     = 0;
                bool   isValid  = false;

                if (match.Success) //user entered email
                {
                    mode    = 1;
                    isValid = true;
                }
                else //user entered phone number
                {
                    Regex strPattern = new Regex("[0-9]*[_]*");

                    if (!strPattern.IsMatch(username))
                    {
                        mode    = 2;
                        isValid = true;
                    }
                }

                if (isValid)
                {
                    Classes.LoginSession ls = new Classes.LoginSession();
                    int userId = ls.login(mode, username, TextBoxPassword.Text);

                    if (userId == 0) // user information was not valid
                    {
                        LabelError.Visible = true;
                        LabelError.Text    = "You username and/or password is not valid!";
                    }
                    else if (userId == -1)
                    {
                        LabelError.Visible = true;
                        LabelError.Text    = "Access to user's panel is not allowed!";
                    }
                    else // user information was valid
                    {
                        Session["UserId"] = userId.ToString();

                        int    Hours;
                        string VerificationCode = Convert.ToString(Guid.NewGuid());

                        if (CheckBoxRemember.Checked) // user want the system to remember him/her
                        {
                            Hours = Convert.ToInt32(ConfigurationManager.AppSettings["LoginHoursLong"].ToString());
                        }
                        else
                        {
                            Hours = Convert.ToInt32(ConfigurationManager.AppSettings["LoginHoursShort"].ToString());
                        }

                        // set login information
                        ls.setLoginSession(Convert.ToInt32(Session["UserId"]), VerificationCode, Hours);

                        // create the cookies
                        HttpCookie _userInfoCookies = new HttpCookie("VC");
                        _userInfoCookies["VC"]   = VerificationCode;
                        _userInfoCookies.Expires = DateTime.Now.AddHours(Hours);
                        Response.Cookies.Add(_userInfoCookies);

                        // redirect user
                        try
                        {
                            if (Page.RouteData.Values["ItemId"].ToString() != "") // redirect user to a page with item id
                            {
                                Response.Redirect("~/" + Page.RouteData.Values["Page"].ToString() + "/" + Page.RouteData.Values["ItemId"].ToString());
                            }
                        }
                        catch (Exception ex)
                        {
                        }
                        finally
                        {
                        }
                        try
                        {
                            if (Page.RouteData.Values["Page"].ToString() != "") // redirect user to a page
                            {
                                Response.Redirect("~/" + Page.RouteData.Values["Page"].ToString());
                            }
                        }
                        catch (Exception ex)
                        {
                        }
                        finally
                        {
                        }
                        // redirect the user to his/her panel
                        Response.Redirect("~/Explore");
                    }
                }
                else
                {
                    LabelError.Visible = true;
                    LabelError.Text    = "Wrong information!";
                }
            }
        }
Пример #2
0
        protected void ButtonLogin_Click(object sender, EventArgs e)
        {
            if (TextBoxUsername.Text.Length == 0 || TextBoxPassword.Text.Length == 0) //blank information
            {
                LabelError.Visible = true;
                LabelError.Text = "Please enter the username and password!";
            }
            else
            {
                string username = TextBoxUsername.Text;
                Regex regex = new Regex(@"^([\w\.\-]+)@([\w\-]+)((\.(\w){2,3})+)$");
                Match match = regex.Match(username);
                int mode = 0;
                bool isValid = false;

                if (match.Success) //user entered email
                {
                    mode = 1;
                    isValid = true;
                }
                else //user entered phone number
                {
                    Regex strPattern = new Regex("[0-9]*[_]*");

                    if (!strPattern.IsMatch(username))
                    {
                        mode = 2;
                        isValid = true;
                    }
                }

                if (isValid)
                {
                    Classes.LoginSession ls = new Classes.LoginSession();
                    int userId = ls.login(mode, username, TextBoxPassword.Text);

                    if (userId == 0) // user information was not valid
                    {
                        LabelError.Visible = true;
                        LabelError.Text = "You username and/or password is not valid!";
                    }
                    else if (userId == -1)
                    {
                        LabelError.Visible = true;
                        LabelError.Text = "Access to user's panel is not allowed!";
                    }
                    else // user information was valid
                    {
                        Session["UserId"] = userId.ToString();

                        int Hours;
                        string VerificationCode = Convert.ToString(Guid.NewGuid());

                        if (CheckBoxRemember.Checked) // user want the system to remember him/her
                        {
                            Hours = Convert.ToInt32(ConfigurationManager.AppSettings["LoginHoursLong"].ToString());
                        }
                        else
                        {
                            Hours = Convert.ToInt32(ConfigurationManager.AppSettings["LoginHoursShort"].ToString());
                        }

                        // set login information
                        ls.setLoginSession(Convert.ToInt32(Session["UserId"]), VerificationCode, Hours);

                        // create the cookies
                        HttpCookie _userInfoCookies = new HttpCookie("VC");
                        _userInfoCookies["VC"] = VerificationCode;
                        _userInfoCookies.Expires = DateTime.Now.AddHours(Hours);
                        Response.Cookies.Add(_userInfoCookies);

                        // redirect user
                        try
                        {
                            if (Page.RouteData.Values["ItemId"].ToString() != "") // redirect user to a page with item id
                            {
                                Response.Redirect("~/" + Page.RouteData.Values["Page"].ToString() + "/" + Page.RouteData.Values["ItemId"].ToString());
                            }
                        }
                        catch (Exception ex)
                        {

                        }
                        finally
                        {

                        }
                        try
                        {
                            if (Page.RouteData.Values["Page"].ToString() != "") // redirect user to a page
                            {
                                Response.Redirect("~/" + Page.RouteData.Values["Page"].ToString());
                            }
                        }
                        catch (Exception ex)
                        {

                        }
                        finally
                        {

                        }
                        // redirect the user to his/her panel
                        Response.Redirect("~/Explore");
                    }
                }
                else
                {
                    LabelError.Visible = true;
                    LabelError.Text = "Wrong information!";
                }
            }
        }