コード例 #1
0
ファイル: Login.aspx.cs プロジェクト: farhad85/Iranegan
        protected void ImageButtonRegister_Click(object sender, ImageClickEventArgs e)
        {
            if (TextBoxEmail.Text.Length == 0)
            {
                LabelError.Visible = true;
                LabelError.Text = "پست الکترونیکی را وارد نمایید!";
            }
            else
            {
                string mail = TextBoxEmail.Text;
                string expression = @"^[a-z][a-z|0-9|]*([_][a-z|0-9]+)*([.][a-z|" + @"0-9]+([_][a-z|0-9]+)*)?@[a-z][a-z|0-9|]*\.([a-z]" + @"[a-z|0-9]*(\.[a-z][a-z|0-9]*)?)$";

                Match match = Regex.Match(mail, expression, RegexOptions.IgnoreCase);
                if (!match.Success)
                {
                    LabelError.Visible = true;
                    LabelError.Text = "پست الکترونیکی صحیح نمی باشد!";
                }
                else
                {
                    DataTable dt = new DataTable();
                    DataSet ds = new DataSet();
                    SqlConnection sqlConn = new SqlConnection(ConfigurationManager.ConnectionStrings["ProfilesConnectionString"].ConnectionString);
                    SqlDataAdapter sda = new SqlDataAdapter("sp_registerCheckEmail", sqlConn);
                    SqlCommand sqlCmd = new SqlCommand("sp_register", sqlConn);
                    SqlDataAdapter sda2 = new SqlDataAdapter("sp_userIdByEmail", sqlConn);

                    try
                    {
                        sda.SelectCommand.CommandType = CommandType.StoredProcedure;
                        sda.SelectCommand.Parameters.Add(new SqlParameter("@Email", SqlDbType.VarChar));
                        sda.SelectCommand.Parameters["@Email"].Value = TextBoxEmail.Text;
                        sda.Fill(ds);
                        dt = ds.Tables[0];

                        if (dt.Rows.Count > 0) //email registered before
                        {
                            LabelError.Visible = true;
                            LabelError.Text = "پست الکترونیکی وارد شده در سیستم وجود دارد!";
                            sqlConn.Close();
                            sqlConn.Dispose();
                            sda.Dispose();
                        }
                        else
                        {
                            if (TextBoxMobile.Text.Length == 0)
                            {
                                LabelError.Visible = true;
                                LabelError.Text = "تلفن همراه را وارد نمایید!";
                            }
                            else
                            {
                                if (TextBoxMobile.Text.Length < 10)
                                {
                                    LabelError.Visible = true;
                                    LabelError.Text = "تلفن همراه صحیح نمی باشد!";
                                }
                                else
                                {
                                    if (TextBoxPassword1.Text.Length == 0)
                                    {
                                        LabelError.Visible = true;
                                        LabelError.Text = "کلمه عبور را وارد نمایید!";
                                    }
                                    else
                                    {
                                        if (TextBoxPassword1.Text.Length < 4)
                                        {
                                            LabelError.Visible = true;
                                            LabelError.Text = "کلمه عبور حداقل می بایست 4 کاراکتر باشد!";
                                        }
                                        else
                                        {
                                            if (TextBoxPassword1.Text != TextBoxPassword2.Text)
                                            {
                                                LabelError.Visible = true;
                                                LabelError.Text = "کلمه عبور و تکرار کلمه عبور می بایست یکسان باشند!";
                                            }
                                            else
                                            {
                                                MD5CryptoServiceProvider md5Hasher = new MD5CryptoServiceProvider();
                                                byte[] hashedBytes;
                                                UTF8Encoding encoder = new UTF8Encoding();
                                                hashedBytes = md5Hasher.ComputeHash(encoder.GetBytes(TextBoxPassword1.Text));

                                                sqlCmd.CommandType = CommandType.StoredProcedure;
                                                sqlCmd.Parameters.Add("@Email", SqlDbType.NVarChar).Value = TextBoxEmail.Text;
                                                sqlCmd.Parameters.Add("@Mobile", SqlDbType.NVarChar).Value = TextBoxMobile.Text;
                                                sqlCmd.Parameters.Add("@Password", SqlDbType.Binary, 16).Value = hashedBytes;

                                                sqlConn.Open();
                                                sqlCmd.ExecuteNonQuery();

                                                DataTable dt2 = new DataTable();
                                                DataSet ds2 = new DataSet();

                                                sda2.SelectCommand.CommandType = CommandType.StoredProcedure;
                                                sda2.SelectCommand.Parameters.Add(new SqlParameter("@Email", SqlDbType.VarChar));
                                                sda2.SelectCommand.Parameters["@Email"].Value = TextBoxEmail.Text;
                                                sda2.Fill(ds2);
                                                dt2 = ds2.Tables[0];

                                                Session["UserId"] = Convert.ToInt32(dt2.Rows[0]["UserId"].ToString());

                                                Classes.Notifications no = new Classes.Notifications();
                                                no.addNotification(Convert.ToInt32(dt2.Rows[0]["UserId"].ToString()), 1, "", "");

                                                sqlConn.Close();
                                                sqlConn.Dispose();


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

                                                Classes.LoginSession ls = new Classes.LoginSession();
                                                ls.setLoginSession(Convert.ToInt32(dt2.Rows[0]["UserId"].ToString()), VerificationCode, Hours);

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

                                                Response.Redirect("~/Instruction");
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                    catch (Exception ex)
                    {

                    }
                    finally
                    {
                        sqlConn.Close();
                        sda.Dispose();
                        sda2.Dispose();
                        sqlCmd.Dispose();
                        sqlConn.Dispose();
                    }
                }
            }
            
        }
コード例 #2
0
ファイル: Login.aspx.cs プロジェクト: farhad85/Iranegan
        protected void ImageButtonLogin_Click(object sender, ImageClickEventArgs e)
        {
            if (TextBoxLoginEmail.Text.Length == 0 || TextBoxLoginPassword.Text.Length == 0)
            {
                LabelLoginError.Visible = true;
                LabelLoginError.Text = "پست الکترونیک یا کلمه عبور وارد شده اشتباه است!";
            }
            else
            {
                string mail = TextBoxLoginEmail.Text;
                string expression = @"^[a-z][a-z|0-9|]*([_][a-z|0-9]+)*([.][a-z|" + @"0-9]+([_][a-z|0-9]+)*)?@[a-z][a-z|0-9|]*\.([a-z]" + @"[a-z|0-9]*(\.[a-z][a-z|0-9]*)?)$";

                Match match = Regex.Match(mail, expression, RegexOptions.IgnoreCase);
                if (!match.Success)
                {
                    LabelLoginError.Visible = true;
                    LabelLoginError.Text = "پست الکترونیک یا کلمه عبور وارد شده اشتباه است!";
                }
                else
                {
                    MD5CryptoServiceProvider md5Hasher = new MD5CryptoServiceProvider();
                    Byte[] PassByte;
                    UTF8Encoding encoder = new UTF8Encoding();
                    PassByte = md5Hasher.ComputeHash(encoder.GetBytes(TextBoxLoginPassword.Text));

                    DataTable dt = new DataTable();
                    DataSet ds = new DataSet();
                    SqlConnection sqlConn = new SqlConnection(ConfigurationManager.ConnectionStrings["ProfilesConnectionString"].ConnectionString);
                    SqlDataAdapter sda = new SqlDataAdapter("sp_login", sqlConn);

                    try
                    {
                        sda.SelectCommand.CommandType = CommandType.StoredProcedure;
                        sda.SelectCommand.Parameters.Add("@Email", SqlDbType.VarChar).Value = TextBoxLoginEmail.Text;
                        sda.SelectCommand.Parameters.Add("@Password", SqlDbType.Binary).Value = PassByte;
                        sda.Fill(ds);
                        dt = ds.Tables[0];
                    }
                    catch (Exception ex)
                    {

                    }
                    finally
                    {
                        sqlConn.Close();
                        sda.Dispose();
                        sqlConn.Dispose();
                    }

                    if (dt.Rows.Count == 0)
                    {
                        LabelLoginError.Visible = true;
                        LabelLoginError.Text = "پست الکترونیک یا کلمه عبور وارد شده اشتباه است!";
                        sqlConn.Close();
                        sqlConn.Dispose();
                    }
                    else
                    {
                        Session["UserId"] = dt.Rows[0]["UserId"].ToString();

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

                        if (CheckBoxLoginRemember.Checked)
                        {
                            Hours = 168;
                        }

                        Classes.LoginSession ls = new Classes.LoginSession();
                        ls.setLoginSession(Convert.ToInt32(Session["UserId"]), VerificationCode, Hours);

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

                        Response.Redirect("~/Panel");
                    }
                }
            }
        }