create a login

d2005

Active member
Joined
Aug 31, 2005
Messages
37
Location
ireland
Programming Experience
Beginner
i am trying to create a login, this may be very basic to most but any help would be appreciated,
dont worry my book is in the post.

is this gettin there, any further steps needed.

VB.NET:
 Imports System 

Imports System.data

Imports System.Data.SqlClient

Imports System.Web.Security ' ||||| Required Class for Authentication

Imports System.Data.OleDb ' |||||| Access Database Required Import!

Imports System.Configuration ' |||||| Required for Web.Config appSettings |||||

' ||||| Connection String - XML coded in Web.Config

' ||||| Remember to set the Security Settings 

Public Class login

Inherits System.Web.UI.Page

Dim oSQLConn As SqlConnection = New SqlConnection



 

Private dslogin As New DataSet

Protected WithEvents btn_login As System.Web.UI.WebControls.Button

Private dalogin As New SqlDataAdapter

#Region " Web Form Designer Generated Code "

'This call is required by the Web Form Designer.

<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

End Sub

Protected WithEvents txt_user As System.Web.UI.WebControls.TextBox

Protected WithEvents txt_pass As System.Web.UI.WebControls.TextBox

Protected WithEvents Label1 As System.Web.UI.WebControls.Label

Protected WithEvents Label2 As System.Web.UI.WebControls.Label

'NOTE: The following placeholder declaration is required by the Web Form Designer.

'Do not delete or move it.

Private designerPlaceholderDeclaration As System.Object

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init

'CODEGEN: This method call is required by the Web Form Designer

'Do not modify it using the code editor.

InitializeComponent()

End Sub

#End Region

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

'Put user code to initialize the page here

End Sub

 

Private Sub connect()

 

oSQLConn.ConnectionString = "Data Source=(local);" & _

"Initial Catalog=TaT;" & _

"Integrated Security=SSPI"

oSQLConn.Open()

 

End Sub

 



Private Sub btn_login_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_login.Click

If Page.IsValid Then

' ||||| Connect to Database for User Validation |||||

dalogin.SelectCommand = New SqlCommand("SELECT comp_username, comp_password from tb_comp_login ", oSQLConn)

dalogin.Fill(dslogin)

 

 

If oSQLConn(txt_user.Text, txt_pass.Text) Then

FormsAuthentication.RedirectFromLoginPage(txt_user.Text, False) ' ||||| default.aspx Page!

Else

' ||||| Credentials are Invalid

'lblMessage.Text = "Invalid Login!"

End If

End If

 

 

End Sub

End Class
 
Creating a basic login is actually much simpler than having to go through all the codes above. If you could just post where you get stucked then it might invite more people to help.
 
i need a connection to the database,
it says Conn - overload resolutiobn failed because 'new' accepts this number of arguments.

also i would need an example of connecting to the database from my config file.
i dont have any connection set up as yet this may be the problem.

what are the advantages of settin up a connection in the web.config file.

Thanks

VB.NET:
Private Function ValidateLogin(ByVal Username As String, ByVal Password As String) As Boolean
 
'Dim Conn As New SqlClient.SqlConnection("select comp_username, comp_password, from tb_comp_login", Conn)
'Dim SQL As String
'SQL = "SELECT Password "
'SQL &= "FROM Users "
'SQL &= "WHERE Username = @Username "
'Dim Comm As New SqlClient.SqlCommand(SQL, Conn)
'Dim Param As New SqlClient.SqlParameter("@Username", SqlDbType.VarChar)
'Param.Direction = ParameterDirection.Input
'Param.Value = Username
'Comm.Parameters.Add(Param)
'Dim da As New SqlClient.SqlDataAdapter(Comm)
'Dim dt As New DataTable
'da.Fill(dt)
'Conn.Close()
'Conn.Dispose()
'Dim Validated As Boolean
'If dt.Rows.Count > 0 Then
'Validated = (dt.Rows.Item(0).Item("Password").ToString = Username)
'End If
'Return Validated
 
d2005 download Visual Web Developer and you can create login page and login settings very easy...
Also you can create register page, forgot password and other thing about login forms...
 
Here is a sample of what I used for login. On top of that each page uses the user name to check a db table to see if the user has access to that page and if something goes to the page directly it checks if the cookie is empty and if so it redirects to login page.

Protected
Sub lButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lButton.Click
Dim objCookie As HttpCookie
objCookie =
New HttpCookie("Mycookie", logintxt.Text)
Response.Cookies(
"Mycookie").Expires = DateTime.Now.AddHours(4)
Response.Cookies.Add(objCookie)
Dim Conn As String = "server='host'; user id='usr';password='pass'; database='db'"
Dim AuthCheck As New MySql.Data.MySqlClient.MySqlConnection(Conn)
Dim AuthGo As New MySql.Data.MySqlClient.MySqlCommand("Auth", AuthCheck)
AuthGo.CommandType = Data.CommandType.Text
AuthGo.CommandText =
"SELECT * FROM `usrs` WHERE (`user` = '" + logintxt.Text + "') AND (`pass` ='" + passtxt.Text + "')"
Dim AuthGoAdapter As New MySql.Data.MySqlClient.MySqlDataAdapter(AuthGo)
Dim AuthSet As New Data.DataSet()
AuthGoAdapter.Fill(AuthSet)
If AuthSet.Tables(0).Rows.Count > 0 Then
Response.Redirect("main.aspx")
Else
Response.Redirect("Default.aspx")
End If
End Sub
 
the advantage of setting up connection string in web.config is that when you want tochange the connection string you need not change it in every page, you only need to change in web.config
 
Back
Top