Click to See Complete Forum and Search --> : Login Check


speranza
09-27-2007, 08:29 AM
Hi,

i created my own table.it includes usr_name,usr_surname,usr_email,usr_password

and i have login form.it includes 2 texboxes.1 for login(email) and the other for password.How can i query the user on database if user on db and his password matches with his username.dont advice me to try asp.net membership please..i want to create my own.i am using sql,asp.net codebehind:vb.
if you give example with stored procedure it can be more useful for me.

thanks,

Emefa
09-27-2007, 04:48 PM
Well, I have some examples from one of my projects here to share.

Login.aspx

<%@ Page Language="VB" explicit="true" %>
<script language="VB" runat="Server">

Dim strLoginError As String
Dim strError As Object


</script>
<%strLoginError = Request.Item("Error")
%>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>Login page</title>



</head>
<body>
<form action="login_process.aspx" method="post" name="login" ID="Form1">
<center>
<p>&nbsp;</p>
<table width="465" border="0" cellpadding="0" cellspacing="0" class="bigregboxdiv">
<tr>
<td width="383" align="center">
<p class="quote">
&nbsp;</p>
<table width="362" border="0" align="center" cellpadding="0" cellspacing="0" class="regboxdiv" ID="Table3">

<%If strLoginError = "1" Then%>
<tr>
<td colspan="2" align="center"><font face="arial" size="1" color="red"><strong>Your login was unsuccessful.<br />
Please try again.</td>
</tr>
<%ElseIf RTrim(strLoginError) = "login" Then %>
<tr>
<td colspan="2" align="center"><font face="arial" size="1" color="red"><strong>Your session has timed out.<br />
Please login again.</td>
</tr>
<%End If%>
<tr>
<td colspan="2" align="center">&nbsp;</td>
</tr>
<tr>
<td align="center"><font size="1" face="arial" class="regfont"><strong>Email</strong></font></td>
<td align="left"><input type="text" size="40" name="username" id="Text2"></td>
</tr>
<tr>
<td colspan="2" align="center">&nbsp;</td>
</tr>
<tr>
<td width="61" align="center"><font size="1" face="arial" class="regfont"><strong>Password</strong></font></td>
<td width="299" align="left"><input type="password" size="30" name="password" id="Password2"></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" value="Login !" id="Submit2" name="Submit2" ></td>
</tr>
</table>
<p><br>
<span class="regfont"></span></p>
<p>&nbsp;</p></td>
</tr>
</table>
</center>

</form>

</body>
</html>



ProcessLogin.aspx

<%@ Page Language="VB" explicit="true" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<%response.Buffer = True
'Get username and password from login form
strUserName = Request.Form.Item("username")
strPassword = Request.Form.Item("Password")
LoginCheck()

%>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Login Process</title>

<style type="text/css">
.button{
background:#FFFFFF;
color:#FFFFFF;
}
</style>
</head>
<body>

<script language="VB" runat="Server">

' Here, you connect to the database and check user credentials
Dim strUserName As String
Dim strPassword As String
Private Sub LoginCheck()

Dim myConnection As SqlConnection
Dim myCommand As SqlCommand
myConnection = New SqlConnection(System.Configuration.ConfigurationManager.AppSettings("Your Connection Setting"))
myCommand = New SqlCommand("Your Stored Procedure", myConnection)
' Stored Procedure could read "Select * from users where email = @email and password =@password
myCommand.CommandType = CommandType.StoredProcedure
' Username
Dim Username As New SqlParameter("@Email", SqlDbType.VarChar, 50)
Username.Value = strUserName
myCommand.Parameters.Add(Username)
' Password
Dim Password As New SqlParameter("@Password", SqlDbType.VarChar, 50)
Password.Value = strPassword
myCommand.Parameters.Add(Password)

myConnection.Open()
Dim myDataReader As SqlDataReader = myCommand.ExecuteReader()
myDataReader.Read()
If myDataReader.HasRows Then
Session("ID") = myDataReader("intID")
Session("Firstname") = myDataReader("vchFirstName")
Session("Lastname") = myDataReader("vchLastname")
Session("Address") = myDataReader("vchAddress")
Session("City") = myDataReader("City")
Session("Country") = myDataReader("Country")
Session("Email") = myDataReader("Email")
Session("Gender") = myDataReader("chGender")

' Upon Verification of user information
Response.Redirect("Your Home Page")
Else
' Just a regular error check
Response.Redirect("login.aspx?error=1")
End If
myConnection.Close()


End Sub

</script>
</body>
</html>


CheckAccess.aspx You will do an include in all your pages with Checkaccess.aspx in order to make sure that the pages are secured


<script language="VB" runat="Server">

Dim Firstname, Lastname, Gender, Address, City
Dim Country, Email


</script>
<%
Response.Buffer = True

If Len(Session("ID")) = 0 Then

'If a user loses session, take them back to the login page to login again
Response.Redirect(("Login.aspx"))
Response.End()
Else

Firstname = IIf(IsDBNull(Session("vchFirstName")), Nothing, Session("vchFirstName"))
Lastname = IIf(IsDBNull(Session("vchLastname")), Nothing, Session("vchLastname"))
Gender = IIf(IsDBNull(Session("chGender")), Nothing, Session("chGender"))
Address = IIf(IsDBNull(Session("vchAddress")), Nothing, Session("vchAddress"))
City = IIf(IsDBNull(Session("City")), Nothing, Session("City"))
Country = IIf(IsDBNull(Session("Country")), Nothing, Session("Country"))
Email = IIf(IsDBNull(Session("Email")), Nothing, Session("Email"))
End If
%>

speranza
09-27-2007, 05:24 PM
this is what i try to find since 3 days..great work...thanks alot...

Hack
09-28-2007, 09:26 AM
So, is your problem resolved?

speranza
09-28-2007, 03:52 PM
yes my problem solved.thanks.