Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Secure pages from login script

Status
Not open for further replies.
Joined
Feb 7, 2011
Messages
2
Location
GB
Hi, I am a newbie to asp.net. I have created a simple login page with the following script behind a login form displaying a username, password and button. However, the login script works successfully but how would you protect secure the page you are trying to protect with the login. For example, I know that you have to put session at the top of the page button I cannot get this to work correctly, please help>

Imports System.Data.SqlClient

Imports System.Data



Partial Class Michelle_Login_Login2

Inherits System.Web.UI.Page



Sub LoginUser()



Dim connection As New SqlConnection

Dim adapter As New SqlDataAdapter

Dim userdataset As New DataSet

Dim command As New SqlCommand



connection.ConnectionString() = "Data Source=LRDSRSDATA;Initial Catalog=DAVE;User ID=sa;Password=perforat10N"

command.CommandText = "SELECT * FROM Username WHERE Username ='" & txtUsername.Text & "' AND Password ='" & txtPassword.Text & "'"

connection.Open()

command.Connection = connection

adapter.SelectCommand = command

adapter.Fill(userdataset, "0")

txtUsername.DataBind()

txtPassword.DataBind()



Dim usercount = userdataset.Tables(0).Rows.Count

If usercount > 0 Then

Session("Username") = txtUsername.Text

Response.Redirect("Success.aspx")



Else

'Response.Redirect("Failure.aspx")

'MsgBox("Enter Username and password")

lblStatus.Text = "Enter Correct Username and password"

End If



End Sub



Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click

LoginUser()

End Sub

End Class
 
asp.net comes with authentication built in. You will want to enable forms authentication. There are many examples online.

Jason Meckley
Programmer

faq855-7190
faq732-7259
 
Sorry it needs to validate against the database. I do not want to use forms authentication. Just a simple login screen from sql database and then it redirects to asp.net page.
 
if Session("Username") = "" then go to the login page.

If you are using a masterpage, add this check there.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top