Go4Expert

Go4Expert (http://www.go4expert.com/)
-   ASP.NET (http://www.go4expert.com/forums/asp-net/)
-   -   How to create a login page? (http://www.go4expert.com/forums/create-login-page-t6192/)

halimunan 4Sep2007 11:34

How to create a login page?
 
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?

jwshepherd 4Sep2007 12:12

Re: How to create a login page?
 
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

halimunan 5Sep2007 07:11

Re: How to create a login page?
 
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;

}
    }
}


shabbir 5Sep2007 08:09

Re: How to create a login page?
 
I am not sure but I think Simple Login in ASP using MS Access database should help to some extent.

jwshepherd 5Sep2007 08:33

Re: How to create a login page?
 
you also might want to change the form method from GET ip POST if using it on a live system.

venkatesanj@hcl.in 31Oct2007 17:27

Re: How to create a login page?
 
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


All times are GMT +5.5. The time now is 04:53.