How to create a login page?

Discussion in 'ASP.NET' started by halimunan, Sep 4, 2007.

  1. halimunan

    halimunan New Member

    Joined:
    Jun 14, 2007
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    hi,

    i want to do a login page.
    i`m using C#, ASP.net and Midrosoft SQL.
    i also am using Visual Studio 2005 which has the login control included.

    the question is..
    how do i do that?
    i created a database named "Testing" and the tables inside are "Name" with "UserId" and "Password" columns..

    can anyone guide me from creating the database to the launching steps?

    i read a few toturials but they are for the sql + asp literate ppl. i`m not that good in both. just learning..

    Code:
    string strConnection = "data source=localhost;database=SQL2K5;uid=sa;pwd=;";

    is there anyway not to reveal that in source code?
     
  2. jwshepherd

    jwshepherd New Member

    Joined:
    Aug 13, 2007
    Messages:
    135
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    VP Technology Corporation
    Location:
    Texas
    Home Page:
    http://www.officialhackers.com
    This is what I Use , you can change the ado connection code to be sql fairly easily without changing anythin else.

    Code:
    conn.open = "Provider=SQLOLEDB.1;User ID=sa;password=mypassword;Initial Catalog=MyDatabase;Data Source = MySQLServer;Use Procedure for Prepare=1;Auto Translate=True;Packet Size=4096"
    
    login.asp:
    Code:
    <%
    
    oldPath=Request.QueryString("HTTP_REFERER")
    if oldPath="" then oldPath="default.asp"
    Dim conn,rs,mysql,dbpath
    mysql="Select logo from Company"
    Set conn = Server.CreateObject("ADODB.Connection")
    Conn.Open "Data Source=" & Server.Mappath("db.mdb") & ";Provider=Microsoft.Jet.OLEDB.4.0;"
    Set rs = Server.CreateObject("ADODB.Recordset")
    Set rp = Server.CreateObject("ADODB.Recordset")
    
    rs.Open Mysql,conn,3,3,1
    MyLogo=rs.Fields(0).Value 
    'MyScript="default.asp"
    rs.Close 
    Response.Write "<HTML>"
    response.write "<HEAD><CENTER>"
    Response.Write session("username")
    if len(Request.QueryString("username")) < 1 then
    %>
    <h2>Please log in to continue<BR></h2>
    <form action="" method=GET>
    <%if Request.QueryString("msg") <> "" then Response.Write "<font color=red>" & Request.QueryString("msg")%></font><BR>
    Username: <INPUT type="text" id=text1 name=username><BR>
    Password: <INPUT type="password" id=password1 name=password><BR>
    <INPUT type="submit" value="Submit" id=submit1 name=submit1>
    </form></center>
    <%
    else
    rs.Open "Select password from Users where username='" & lcase(Request.QueryString("username")) & "';"
    if rs.RecordCount =0 then 
    Response.Write "Invalid Username!"
    Response.Redirect("login.asp?msg=Invalid Username! Try Again.")
    else
    if lcase(rs.Fields(0).Value) = lcase(Request.QueryString("password")) then 
    session("AUTH")=True
    Response.Write "all ok"
    session("username")=lcase(Request.QueryString("username"))
    myfrom=session("username")
    rp.Open "Select id from tech where fname='" & session("username") & "';",conn,3,3,1
    session("tech")=rp.Fields(0).Value 
    rp.Close 
    
    
    rp.open "Select * from message where myto='" & myfrom & "' and active <>0",conn,3,3,1
    MyNum=rp.RecordCount
    rp.Close 
    if Mynum > 0 then 
    Response.Redirect("readmsg.asp")
    else
    Response.Redirect(Session("PAGENAME"))
    end if
    
    
    
    Response.Redirect(Session("PAGENAME"))
    end if
    
    end if
    end if
    'Response.Write err.number,err.Description,err.Source 
    %>
    
    
    Then in each page add:
    Code:
    if len(session("username"))< 1 then
    session("PAGENAME")=Request.ServerVariables("SCRIPT_NAME") & "?" & MyString
    Response.Redirect("login.asp")
    end if
    That will check each loaded page for the correct login
     
  3. halimunan

    halimunan New Member

    Joined:
    Jun 14, 2007
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    erm.. i still dont get it..

    what i understand is..

    1. create a database with user login information
    2. create login.aspx and default.aspx
    3. in the login.aspx, put login control. this is where i am blurred. i dont know the code to connect from this page to SQL, to pull the data..

    i want to see how the code will be in login.aspx.cs

    Code:
    using System;
    using System.Data;
    using System.Configuration;
    using System.Collections;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    using System.Data.SqlClient;
    
    namespace WebApplication1
    {
        public partial class _Default : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
    
            }
    
            protected void Login1_Authenticate(object sender, AuthenticateEventArgs e)
    {
    bool Authenticated = false;
    Authenticated = ValidateLogIn(Login1.UserName, Login1.Password);
    if (Authenticated == true)
    {
    Response.Redirect("Home.aspx");
    }
    
    }
    
    private bool ValidateLogIn(string UserName, string Password)
    {
    bool blnRetVal = false;
    string strConnection = "data source=localhost;database=SQL2K5;uid=sa;pwd=;";
    SqlConnection Connection = new SqlConnection(strConnection);
    String strSQL = "Select UserId,Password From Authenticate where UserId =+”’John’” + and Password =+”’Password’”;";
    SqlCommand command = new SqlCommand(strSQL, Connection);
    SqlDataReader dr;
    Connection.Open();
    dr = command.ExecuteReader();
    while (dr.Read())
    {
    if ((UserName == dr[0].ToString()) & (Password == dr[1].ToString()))
    {
    blnRetVal = true;
    }
    
    }
    dr.Close();
    return blnRetVal;
    
    }
        }
    }
    
     
  4. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
  5. jwshepherd

    jwshepherd New Member

    Joined:
    Aug 13, 2007
    Messages:
    135
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    VP Technology Corporation
    Location:
    Texas
    Home Page:
    http://www.officialhackers.com
    you also might want to change the form method from GET ip POST if using it on a live system.
     
  6. venkatesanj@hcl.in

    venkatesanj@hcl.in New Member

    Joined:
    Oct 19, 2007
    Messages:
    24
    Likes Received:
    1
    Trophy Points:
    0
    Make a try with this code

    Code:
    String strSQL = "Select Count(*) From Authenticate where UserId =+”’John’” + and Password =+”’Password’”;";
    SqlCommand command = new SqlCommand(strSQL, Connection);
    
    Connection.Open();
    int a = command.ExecuteScaler();
    if(a >0)
      blnRetVal = true;
    return blnRetVal;
    
    Its not adivasable to use the query instead try to use Stored procedure
    Regards,
    Venkatesan Prabu. J
     
    Last edited by a moderator: Oct 31, 2007
  7. mialuzzatto

    mialuzzatto New Member

    Joined:
    Aug 5, 2015
    Messages:
    122
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Work at https://www.idevelopersquare.com
    Location:
    Waltham, MA, USA
    Home Page:
    https://www.idevelopersquare.com
    Hello,

    In the connectivity.php webpage, first of we create a connection between PHP and MySQL. And then we used SELECT query to select two things from the database, which is userName and Pass. When we used SELECT query, you can see the query we have used “*” its mean that we selected all data from the database for the login. But in the same query we also used WHERE clause to compare that all data which are fetched with the sign-in.html. From sign-in html two things are coming to connectivity.php page, which is username and password. Then we used an there function of PHP programming Language that is mysql_fetch_array(). This will fetch all data according to the SELECT query. And in last we used if statement to know the condition of query. Because sometimes it could be happen the user and password is not present in the database. For this purpose to check the query is executed and what it gives us result in if statement. For calling the function we used and other If statement to get know that the user as pressed the Log-in button or not, if he/she has pressed so the function will be call and get sign-in.
     
  8. alia123

    alia123 New Member

    Joined:
    Jan 8, 2016
    Messages:
    65
    Likes Received:
    5
    Trophy Points:
    0
    HEY,I’m sharing a code,try this code:
    HTML:
    <html>
        <head>
            <title>Login page</title>
        </head>
        <body>
            <h1>Simple Login Page</h1>
            <form name="login">
                Username<input type="text" name="userid"/>
                Password<input type="password" name="pswrd"/>
                <input type="button" onclick="check(this.form)" value="Login"/>
                <input type="reset" value="Cancel"/>
            </form>
            <script language="javascript">
                function check(form) { /*function to check userid & password*/
                    /*the following code checkes whether the entered userid and password are matching*/
                    if(form.userid.value == "myuserid" && form.pswrd.value == "mypswrd") {
                        window.open('target.html')/*opens the target page while Id & password matches*/
                    }
                    else {
                        alert("Error Password or Username")/*displays error message*/
                    }
                }
            </script>
        </body>
    </html>
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice