locked
How to authenticate user name and password from sql RRS feed

  • Question

  • User1864490474 posted

    Hi,

    I'm really newbie wtih classic asp. I wonder how could I use classic asp to authenticate username and password from sql database? I know how to select the data from database but I really confused about how to vaild the data dynamically. Anyway, I'm also a bit confused about what operations do I need to do to enable the authenticated page in IIS. Any suggestion will be appreciate. Thanks in advanced.

    Monday, May 15, 2017 4:32 PM

Answers

  • User-460007017 posted

    Hi JeffryRock,

    Here is the sample to authenticate password with database:

    <%
    
      function checkPwd(id,psd) 
    
      dim conn,param,rs
    
      set conn=server.createobject("adodb.connection")
      param="driver={microsoft access driver (*.mdb)}"   conn.open param & ";dbq=" & server.mappath("psd.mdb") 
      sql="select*from psd where id='" & id & "' and psd='" & psd & "'" 
      set rs=conn.execute(sql) 
      if rs.eof then
      checkpwd=false
      else
      checkpwd=true
      end if
        end function 
      %>
    

    If you need to achieve the same requirement in asp.net, it could looks like:

    protected void Button1_Click(object sender, EventArgs e)
        {
            string connString = Convert.ToString(ConfigurationManager.ConnectionStrings["sqlconn"]);
            SqlConnection conn = new SqlConnection(connString);
            conn.Open();
            string strsql = "select * from stuent where StuName='" + TextBox1.Text + "'and Stupassword='" + TextBox2.Text + "'";
            SqlCommand cmd = new SqlCommand(strsql, conn);
            SqlDataReader dr = cmd.ExecuteReader();
            if (dr.Read())
            {
                Session["StuName"] = dr["StuName"];
                Session["Stupassword"] = dr["Stupassword"];
                if (TextBox1.Text == Session["StuName"].ToString() && TextBox2.Text == Session["Stupassword"].ToString())
                {
                    Response.Redirect("Default2.aspx");
                }
            }
            else
            {
                Label1.Text = "login failed!";
            }
        }搜索
    web.config
    <add name="sqlconn" connectionString="Data Source=.;Integrated Security=SSPI;Initial Catalog=xuesheng;" providerName="System.Data.SqlClient"/>

    If you need to use the classic page to autehtnticated user, please enable the from authentication/anonymous authentication and set the login page in form authentication->edit..->login URL. Then add the authorization rule to allow authenticated user and deny the anonymous rule.

    Best Regards,

    Yuk Ding

    • Marked as answer by Anonymous Tuesday, September 28, 2021 12:00 AM
    Tuesday, May 16, 2017 6:32 AM

All replies

  • User690216013 posted

    In 2017 you should pick up a modern web technology instead of this.

    Tuesday, May 16, 2017 3:18 AM
  • User-460007017 posted

    Hi JeffryRock,

    Here is the sample to authenticate password with database:

    <%
    
      function checkPwd(id,psd) 
    
      dim conn,param,rs
    
      set conn=server.createobject("adodb.connection")
      param="driver={microsoft access driver (*.mdb)}"   conn.open param & ";dbq=" & server.mappath("psd.mdb") 
      sql="select*from psd where id='" & id & "' and psd='" & psd & "'" 
      set rs=conn.execute(sql) 
      if rs.eof then
      checkpwd=false
      else
      checkpwd=true
      end if
        end function 
      %>
    

    If you need to achieve the same requirement in asp.net, it could looks like:

    protected void Button1_Click(object sender, EventArgs e)
        {
            string connString = Convert.ToString(ConfigurationManager.ConnectionStrings["sqlconn"]);
            SqlConnection conn = new SqlConnection(connString);
            conn.Open();
            string strsql = "select * from stuent where StuName='" + TextBox1.Text + "'and Stupassword='" + TextBox2.Text + "'";
            SqlCommand cmd = new SqlCommand(strsql, conn);
            SqlDataReader dr = cmd.ExecuteReader();
            if (dr.Read())
            {
                Session["StuName"] = dr["StuName"];
                Session["Stupassword"] = dr["Stupassword"];
                if (TextBox1.Text == Session["StuName"].ToString() && TextBox2.Text == Session["Stupassword"].ToString())
                {
                    Response.Redirect("Default2.aspx");
                }
            }
            else
            {
                Label1.Text = "login failed!";
            }
        }搜索
    web.config
    <add name="sqlconn" connectionString="Data Source=.;Integrated Security=SSPI;Initial Catalog=xuesheng;" providerName="System.Data.SqlClient"/>

    If you need to use the classic page to autehtnticated user, please enable the from authentication/anonymous authentication and set the login page in form authentication->edit..->login URL. Then add the authorization rule to allow authenticated user and deny the anonymous rule.

    Best Regards,

    Yuk Ding

    • Marked as answer by Anonymous Tuesday, September 28, 2021 12:00 AM
    Tuesday, May 16, 2017 6:32 AM