求用户验证(登录)的c#.net代码

解决方案 »

  1.   

    private void Button1_Click(object sender, System.EventArgs e)
    {
      Cmd.CommandText="PRO_Reg_Login";
    Cmd.CommandType=CommandType.StoredProcedure;

    Cmd.Parameters.Add("@User",SqlDbType.NVarChar,255);
    Cmd.Parameters["@User"].Value=User.Text;
    Cmd.Parameters.Add("@Pass",SqlDbType.NVarChar,255);
    Cmd.Parameters["@Pass"].Value=Pass.Text;
    Cmd.Parameters.Add("@COUNTS",SqlDbType.Int,4);
    Cmd.Parameters["@COUNTS"].Direction=ParameterDirection.Output;
    try
    {
    Cmd.ExecuteNonQuery();
    }
    catch
    {
     Response.Write(DataBase.ErrorMessage("您没有通过验证","window.location.href='index.aspx'"));
    Response.End();
    }
              int con=Convert.ToInt32(  Cmd.Parameters["@COUNTS"].Value.ToString().Trim());
    if(con>0)
    {
    Session["good"]="good";
    Session["User"]=User.Text;
    list.Visible=false;
    reg.Visible=true;
    UserName.Text=User.Text; }
    else
    {
    Response.Write(DataBase.ErrorMessage("您没有通过验证","window.location.href='index.aspx'"));
    Response.End();
    }
    }
      

  2.   


    private void Button1_Click(object sender, System.EventArgs e)
    {
    string strConn="Server=TSJ;uid=sa;Pwd=sa;database=BBSv1";
    string strCmd="select count(*) from lyUser where(UserName like '"+TextBox1.Text+"'and Pwd like '"+TextBox2.Text+"')";
    SqlConnection myConnection=new SqlConnection();
    myConnection.ConnectionString=strConn;
    SqlCommand myCommand=new SqlCommand(strCmd,myConnection);
    myCommand.Connection.Open();
    //检索出的用户数
    int flag=(int)myCommand.ExecuteScalar();
    myCommand.Connection.Close();
    myConnection.Close();
    if(flag>0)//用户存在
    {
    Session["UserName"]=TextBox1.Text;
    Response.Redirect("Ex09_03.aspx");
    }
    else 
    {
    Label1.Text ="用户名和密码不正确!";
    TextBox1.Text="";
    TextBox2.Text="";
    }
    }
    将上边的数据库等进行适当修改就可以了!
      

  3.   

    private void bnOK_Click(object sender, System.EventArgs e)
    {
    //'如果是系统预设的管理者, 就不用再进数据库去检查身份
    if (txUserID.Text =="" & txPasswordCode.Text =="")
    {
        strUserID="Admin";
    bnClose=true;
    return;
    }
    //检查用户代号与密码的SQL指令
    cmdSQL.CommandText ="Select UserId From Users"+"where UserID='"+txUserID.Text+ "'"+"And PasswordCode='"+txPasswordCode.Text+"'";
                cnKS.Open ();
    //开启数据库连接
    try
    {
    strUserID=((string)(cmdSQL.ExecuteScalar() ));//将查询结果丢给strUserID
    if(strUserID=="")//如果结果是空白的, 代表找不到该用户,至于是密码错误或者是用户错误则不管
    {
    MessageBox.Show ("错误","失败",MessageBoxButtons.OK ,MessageBoxIcon.Error );
    bnClose=false;//身份错误不可按OK离开
    }
    else
    {
    bnClose=true;//找到, 可以离开
    strUserID=txUserID.Text ;
    }
    }
    finally
    {
    cnKS.Close ();//关毕数据库连接
    }
    }
      

  4.   

    Winform的登录按钮事件:
    private void button1_Click(object sender, System.EventArgs e)
    {
     
    OleDbConnection con=new OleDbConnection ("Provider=Microsoft.Jet.OleDb.4.0;Data Source="+Application.StartupPath +@"\sdms.mdb");
    string sql="select * from login where uname='"+txtUname.Text .Replace ("'","''")+"' and pwd='"+txtPwd.Text .Replace ("'","''")+"' and Authority='"+cbxAuthority.Text+"'" ;
    try
    {
    con.Open ();
    OleDbCommand cmd=new OleDbCommand(sql,con);
      dr=cmd.ExecuteReader ();
       
    if(dr.Read ())
    {

    if(this.cbxAuthority .Text =="操作员")
    {

    GlobalValue.Name="操作员";
    }
    else
    {
    GlobalValue.Name="管理员";
    }
    this.Hide ();
    Form1 main=new Form1 ();
    main.ShowDialog ();
    }
    else
    {
    MessageBox.Show ("请填写正确的用户名、密码、用户角色","警告",MessageBoxButtons.OK ,MessageBoxIcon.Warning );
    }
    }
    catch(Exception err)
    {
    throw err;
    }
    finally
    {
    con.Close ();
        dr.Close ();
    }
                

     
    }
      

  5.   

    System.Web.Security.FormsAuthentication.SetAuthCookie(UserRoles,false);//用forms验证