Updated : Simple ASP.Net Login Page

Please support our ASP.NET advertiser: Intel Parallel Studio Home
Reply

Join Date: Feb 2003
Posts: 793
Reputation: Paladine has a spectacular aura about Paladine has a spectacular aura about Paladine has a spectacular aura about 
Solved Threads: 26
Team Colleague
Paladine's Avatar
Paladine Paladine is offline Offline
Master Poster

Re: Updated : Simple ASP.Net Login Page

 
0
  #131
Apr 21st, 2007
Hey Benbujwah,

If you did do the CUT and PASTE with Visual Web Developer Express, then that is the cause of the error. This code is for .NET 1.0 or 1.1 ONLY and not .NET 2.0 (which is what Web Developer Express is based on).

I haven't provided the .NET 2.0 version simply because with all the start page, master.page, etc configurations and steps, you can do this pretty much with no code typing at all. Best to go to the tutorial section of Visual Web Developer home page and watch the training videos. Rather difficult to convert the how to video presentation to text how to.

Hope this helps.


Originally Posted by benbujwah View Post
Paladine, thanks for your help so far. As of now, my login pages load in Visual QWeb Developer Express (Only validation works, and when i login with user id and pw, the page just reloads with the pw text field blank again. I dont get an error connecting to database or anything). However, when I check it out on the server, i get a big configration error. It is something like :

Configuration Error

Description: An error occurred during the processing of a configuration file required to service this request. Please review the specific error details below and modify your configuration file appropriately.

Parser Error Message: It is an error to use a section registered as allowDefinition='MachineToApplication' beyond application level. This error can be caused by a virtual directory not being configured as an application in IIS.

Source Error:

Line 6: Line 7: <system.web>Line 8: <roleManager enabled="true" />Line 9: <authentication mode="Forms" />Line 10: <compilation defaultLanguage="vb" debug="true" />

Below is my full web.config file. (not as same as yours, but does it need to be just with the source file location changed?)


<configuration>
<appSettings>
<addkey="strnConn"value="PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA SOURCE=D:\Inetpub\wwwroot\students\teamg\IUser\teamg.mdb;User ID=Admin;Password=;" />
</appSettings>
<system.web>
<roleManagerenabled="False" />
<authenticationmode="Forms" />
<compilationdefaultLanguage="vb"debug="true" />
<customErrorsmode="Off" />
<traceenabled="true"requestLimit="10"pageOutput="true"traceMode="SortByTime"localOnly="false" />
</system.web>
<connectionStrings>
<addname="myConnection"connectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\Inetpub\wwwroot\students\teamg\IUser\teamg.mdb;Persist Security Info=True"

providerName="System.Data.OleDb"/>
</connectionStrings>
</configuration>



I have used your query for MS access, your same form code, and same functions in my login.aspx.vb file, (mostly cut and paste ). Im not sure what is causing this error?

Also, I would like to redirect a validaded user from my Acess Database to my menu.aspx page Any help would be appreciated .
Assistant Manager, Pharmacy Informatics
Wordpress Learning Blog
Updated : ASP.Net Login Code
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 8
Reputation: geo039 is an unknown quantity at this point 
Solved Threads: 0
geo039 geo039 is offline Offline
Newbie Poster

Re: Updated : Simple ASP.Net Login Page

 
0
  #132
May 14th, 2007
I followed this tutorial to the t, I'm not sure why I keep getting "Invalid Log In" I'm not using the log in attempt part yet. Any suggestions?

  1.  
  2. Imports System.Web.Security ' ||||| Required Class for Authentication
  3. Imports System.Data ' ||||| DB Accessing Import
  4. Imports System.Data.OleDb ' |||||| Access Database Required Import!
  5. Imports System.Configuration ' |||||| Required for Web.Config appSettings |||||
  6. Partial Class Login
  7. Inherits System.Web.UI.Page
  8. Function DBConnection(ByVal strUserName As String, ByVal strPassword As String) As Boolean
  9. '<sumamry>
  10. ' ||||| Declare Required Variables
  11. ' ||||| Access appSettings of Web.Config for Connection String (Constant)
  12. '</summary>
  13. ' ||||| First is the Connection Object for an Access DB
  14. Dim MyConn As OleDbConnection = New OleDbConnection(System.Configuration.ConfigurationManager.AppSettings("strConn"))
  15.  
  16. '<sumamry>
  17. ' ||||| Create a OleDb Command Object
  18. ' ||||| Pass in Stored procedure
  19. ' ||||| Set CommandType to Stored Procedure
  20. '</summary>
  21. ' ||||| To Access a Stored Procedure in Access - Requires a Command Object
  22. Dim MyCmd As New OleDbCommand("sp_ValidateUser", MyConn)
  23.  
  24. ' ||||| Create Parameter Objects for values passed in
  25. Dim objParam1, objParam2 As OleDbParameter
  26. '<sumamry>
  27. ' ||||| Add the parameters to the parameters collection of the
  28. ' ||||| command object, and set their datatypes (OleDbType in this case)
  29. '</summary>
  30. objParam1 = MyCmd.Parameters.Add("@UserName", OleDbType.Char)
  31. objParam2 = MyCmd.Parameters.Add("@Password", OleDbType.Char)
  32. '' ||||| Set the direction of the parameters...input, output, etc
  33. objParam1.Direction = ParameterDirection.Input
  34. objParam2.Direction = ParameterDirection.Input
  35. '' ||||| Set the value(s) of the parameters to the passed in values
  36. objParam1.Value = strUserName
  37. objParam2.Value = strPassword
  38. ' ||||| Try, catch block!
  39. Try
  40. ' ||||| Check if Connection to DB is already open, if not, then open a connection
  41. If MyConn.State = ConnectionState.Closed Then
  42. ' ||||| DB not already Open...so open it
  43. MyConn.Open()
  44. End If
  45. ' ||||| Create OleDb Data Reader
  46. Dim objReader As OleDbDataReader
  47. objReader = MyCmd.ExecuteReader(CommandBehavior.CloseConnection)
  48. ' ||||| Close the Reader and the Connection Closes with it
  49. While objReader.Read()
  50. If CStr(objReader.GetValue(0)) <> "1" Then
  51. lblMessage.Text = "Invalid Login!"
  52. Else
  53. objReader.Close() ' ||||| Close the Connections & Reader
  54. Return True
  55. End If
  56. End While
  57. Catch ex As Exception
  58. lblMessage.Text = "Error Connecting to Database!"
  59. End Try
  60.  
  61. End Function
  62.  
  63.  
  64.  
  65. Private Sub cmdSubmit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSubmit.Click
  66. If Page.IsValid Then ' ||||| Meaning the Control Validation was successful!
  67. ' ||||| Connect to Database for User Validation |||||
  68. If DBConnection(txtUserName.Text.Trim(), txtPassword.Text.Trim()) Then
  69. FormsAuthentication.RedirectFromLoginPage(txtUserName.Text, False) ' ||||| default.aspx Page!
  70. Else
  71. ' ||||| Credentials are Invalid
  72. lblMessage.Text = "Invalid Login!"
  73. ' ||||| Increment the LoginCount (attempts)
  74. 'Session("LoginCount") = CInt(Session("LoginCount")) + 1
  75. ' ||||| Determine the Number of Tries
  76. 'If Session("LoginCount").Equals(intMaxLoginAttempts) Then
  77. ' Response.Redirect("Denied.aspx")
  78. 'End If
  79. 'If CInt(Session("Num_of_Tries")) > 2 Then ' ||||| If Exceeds then Deny!
  80. ' Response.Redirect("Denied.aspx")
  81. 'End If
  82. End If
  83. End If
  84. End Sub
  85. End Class
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 1
Reputation: lhy89 is an unknown quantity at this point 
Solved Threads: 0
lhy89 lhy89 is offline Offline
Newbie Poster

Re: Updated : Simple ASP.Net Login Page

 
0
  #133
Jun 28th, 2007
hi,i'm newbie here.i'm doing sch project.
can you send me the code of how to create login function using visual studio 2005? because i have lots of errors now,so can you help?
thanks alot.
i have done my code halfway.

Imports System.Data
Imports System.Data.OleDb
Imports System.Configuration
Partial Class Custlogin
Inherits System.Web.UI.Page

Protected Sub btnSignin_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSignin.Click
Dim myConn As New OleDbConnection
Dim myCmd As New OleDbCommand

myConn.ConnectionString = _
ConfigurationManager.ConnectionStrings("CustomerConnectionString").ConnectionString
Dim cmd As String
Dim oleDbCommand As New OleDbCommand("SELECT [ID] FROM [TableName] WHERE [username] = ? AND [Password] = ?", New OleDbCommand(connectionString))

Dim param1 As New OleDbParameter("@username", "usernameHere")

Dim param2 As New OleDbParameter("@password", "passwordHere")

oleDbCommand.Parameters.Add(param1)

oleDbCommand.Parameters.Add(param2)



myCmd.Connection = myConn

myConn.Open()
myCmd.ExecuteNonQuery()
myConn.Close()

myCmd.Dispose()
myConn.Dispose()



If result Is DBNull.Value = False Then

'we have the correct user in the database

End If


End Sub

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

End Sub
End Class
Last edited by lhy89; Jun 28th, 2007 at 12:09 pm.
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 1
Reputation: jstarkey is an unknown quantity at this point 
Solved Threads: 0
jstarkey jstarkey is offline Offline
Newbie Poster

Re: Updated : Simple ASP.Net Login Page

 
0
  #134
Jun 29th, 2007
Yeah I had this problem too and it took me a bit to figure out that the code had a bug in it. When the code tries to verify connection to the database and it fails, it sets lblMessage.Text = "Error Connecting to Database!" Then when it returns to the previous sub it sets and of course the login fails, it changes lblMessage.Text = "Invalid Login!" on top of the bad connection error. I changed the following code to fix it. This way if it encounters the previous code it will not set it to something else.

Original Code:
' ||||| Credentials are Invalid
lblMessage.Text = "Invalid Login!"

Modified Code:
' ||||| Credentials are Invalid
If lblMessage.Text <> "Error Connecting to Database!" Then
lblMessage.Text = "Invalid Login!"
End If


Originally Posted by geo039 View Post
I followed this tutorial to the t, I'm not sure why I keep getting "Invalid Log In" I'm not using the log in attempt part yet. Any suggestions?

  1.  
  2. Imports System.Web.Security ' ||||| Required Class for Authentication
  3. Imports System.Data ' ||||| DB Accessing Import
  4. Imports System.Data.OleDb ' |||||| Access Database Required Import!
  5. Imports System.Configuration ' |||||| Required for Web.Config appSettings |||||
  6. Partial Class Login
  7. Inherits System.Web.UI.Page
  8. Function DBConnection(ByVal strUserName As String, ByVal strPassword As String) As Boolean
  9. '<sumamry>
  10. ' ||||| Declare Required Variables
  11. ' ||||| Access appSettings of Web.Config for Connection String (Constant)
  12. '</summary>
  13. ' ||||| First is the Connection Object for an Access DB
  14. Dim MyConn As OleDbConnection = New OleDbConnection(System.Configuration.ConfigurationManager.AppSettings("strConn"))
  15.  
  16. '<sumamry>
  17. ' ||||| Create a OleDb Command Object
  18. ' ||||| Pass in Stored procedure
  19. ' ||||| Set CommandType to Stored Procedure
  20. '</summary>
  21. ' ||||| To Access a Stored Procedure in Access - Requires a Command Object
  22. Dim MyCmd As New OleDbCommand("sp_ValidateUser", MyConn)
  23.  
  24. ' ||||| Create Parameter Objects for values passed in
  25. Dim objParam1, objParam2 As OleDbParameter
  26. '<sumamry>
  27. ' ||||| Add the parameters to the parameters collection of the
  28. ' ||||| command object, and set their datatypes (OleDbType in this case)
  29. '</summary>
  30. objParam1 = MyCmd.Parameters.Add("@UserName", OleDbType.Char)
  31. objParam2 = MyCmd.Parameters.Add("@Password", OleDbType.Char)
  32. '' ||||| Set the direction of the parameters...input, output, etc
  33. objParam1.Direction = ParameterDirection.Input
  34. objParam2.Direction = ParameterDirection.Input
  35. '' ||||| Set the value(s) of the parameters to the passed in values
  36. objParam1.Value = strUserName
  37. objParam2.Value = strPassword
  38. ' ||||| Try, catch block!
  39. Try
  40. ' ||||| Check if Connection to DB is already open, if not, then open a connection
  41. If MyConn.State = ConnectionState.Closed Then
  42. ' ||||| DB not already Open...so open it
  43. MyConn.Open()
  44. End If
  45. ' ||||| Create OleDb Data Reader
  46. Dim objReader As OleDbDataReader
  47. objReader = MyCmd.ExecuteReader(CommandBehavior.CloseConnection)
  48. ' ||||| Close the Reader and the Connection Closes with it
  49. While objReader.Read()
  50. If CStr(objReader.GetValue(0)) <> "1" Then
  51. lblMessage.Text = "Invalid Login!"
  52. Else
  53. objReader.Close() ' ||||| Close the Connections & Reader
  54. Return True
  55. End If
  56. End While
  57. Catch ex As Exception
  58. lblMessage.Text = "Error Connecting to Database!"
  59. End Try
  60.  
  61. End Function
  62.  
  63.  
  64.  
  65. Private Sub cmdSubmit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSubmit.Click
  66. If Page.IsValid Then ' ||||| Meaning the Control Validation was successful!
  67. ' ||||| Connect to Database for User Validation |||||
  68. If DBConnection(txtUserName.Text.Trim(), txtPassword.Text.Trim()) Then
  69. FormsAuthentication.RedirectFromLoginPage(txtUserName.Text, False) ' ||||| default.aspx Page!
  70. Else
  71. ' ||||| Credentials are Invalid
  72. lblMessage.Text = "Invalid Login!"
  73. ' ||||| Increment the LoginCount (attempts)
  74. 'Session("LoginCount") = CInt(Session("LoginCount")) + 1
  75. ' ||||| Determine the Number of Tries
  76. 'If Session("LoginCount").Equals(intMaxLoginAttempts) Then
  77. ' Response.Redirect("Denied.aspx")
  78. 'End If
  79. 'If CInt(Session("Num_of_Tries")) > 2 Then ' ||||| If Exceeds then Deny!
  80. ' Response.Redirect("Denied.aspx")
  81. 'End If
  82. End If
  83. End If
  84. End Sub
  85. End Class
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 1
Reputation: jhuerta is an unknown quantity at this point 
Solved Threads: 0
jhuerta jhuerta is offline Offline
Newbie Poster

Re: Updated : Simple ASP.Net Login Page

 
0
  #135
Aug 20th, 2007
Hi,
I have a question.
As I've been reading, you have a login page, that is set as default. So, when you go to the web page, this login page is the first one to load:

http://www.testpage.com will redirect to http://www.testpage.com/login.aspx

This page, will validate username and password. If it is successful, it will redirect you to Default.aspx: http://www.testpage.com/default.aspx

My question is ... what happens if you type straight in the url:
http://www.testpage.com/default.aspx ??

How do you prevent the user to access that page?
If there is no session or variable stored somewhere indicating user is logged in ... how you do allow/prevent access to default page? what about to other pages?

thanks,

Juan
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 645
Reputation: binoj_daniel is an unknown quantity at this point 
Solved Threads: 17
binoj_daniel's Avatar
binoj_daniel binoj_daniel is offline Offline
DaniWeb Expert

Re: Updated : Simple ASP.Net Login Page

 
0
  #136
Aug 28th, 2007
I have written a simple login app which will answer your question. Not matter what page you access it will redirect to the login page. So you can make all pages protected.
click here to read and download the sample app.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 1
Reputation: E_IT is an unknown quantity at this point 
Solved Threads: 0
E_IT E_IT is offline Offline
Newbie Poster

Re: Updated : Simple ASP.Net Login Page

 
0
  #137
Nov 20th, 2007
Originally Posted by kantong View Post
hi hi! thanks for the reply Paladine about the table issue. The next thing i want to do is compare a username to a username in the database.
From this line: FormsAuthentication.RedirectFromLoginPage(txtUserName.Text, False)
If i set to say: RedirectFromLoginPage(txtUserName.Text, True)
what does that do? and how can I compare this username in another page?
What I am trying to do is:
if the username = <temp username> then
load an empty form
else if username =<member's username> Then
Load form with member's details already there

im trying to create an update details page.
no idea how to access username thats being passed from the RedirectFromLoginPage...
cheers

Kevin
hi what is the answer to your question?i couldn't find it!!!!
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3
Reputation: mkhmer is an unknown quantity at this point 
Solved Threads: 0
mkhmer mkhmer is offline Offline
Newbie Poster

Re: Updated : Simple ASP.Net Login Page

 
0
  #138
Jan 10th, 2008
Hello!

  1. FormsAuthentication.RedirectFromLoginPage(txtUserName.Text, False)

What does that mean?

How do I verify if the user is logged in or not yet? then redirect for log in again.....

Regards,
Reply With Quote Quick reply to this message  
Join Date: Feb 2003
Posts: 793
Reputation: Paladine has a spectacular aura about Paladine has a spectacular aura about Paladine has a spectacular aura about 
Solved Threads: 26
Team Colleague
Paladine's Avatar
Paladine Paladine is offline Offline
Master Poster

Re: Updated : Simple ASP.Net Login Page

 
0
  #139
Jan 22nd, 2008
This is done in your code prior this statement. This is "validation" is done when you pass the username/password into the DBConnection Function:

  1. ...
  2. If DBConnection(txtUserName.Text.Trim(), txtPassword.Text.Trim()) Then
  3. ...
Assistant Manager, Pharmacy Informatics
Wordpress Learning Blog
Updated : ASP.Net Login Code
Reply With Quote Quick reply to this message  
Join Date: Feb 2003
Posts: 793
Reputation: Paladine has a spectacular aura about Paladine has a spectacular aura about Paladine has a spectacular aura about 
Solved Threads: 26
Team Colleague
Paladine's Avatar
Paladine Paladine is offline Offline
Master Poster

Re: Updated : Simple ASP.Net Login Page

 
0
  #140
Jan 22nd, 2008
Originally Posted by jhuerta View Post
Hi,
I have a question.
As I've been reading, you have a login page, that is set as default. So, when you go to the web page, this login page is the first one to load:

http://www.testpage.com will redirect to http://www.testpage.com/login.aspx

This page, will validate username and password. If it is successful, it will redirect you to Default.aspx: http://www.testpage.com/default.aspx

My question is ... what happens if you type straight in the url:
http://www.testpage.com/default.aspx ??

How do you prevent the user to access that page?
If there is no session or variable stored somewhere indicating user is logged in ... how you do allow/prevent access to default page? what about to other pages?

thanks,

Juan
The Answer to your very question about .. "How do I prevent direct access to the Default.aspx via URL entry..." is found in this very thread...

Ok, I have had a number of people ask me how to prevent access to say the default.aspx page via the direct url, and you can prevent this in a number of ways. I have mentioned the use of cookies, but in the following example I will use another method of Session variables......
Found here: http://www.daniweb.com/forums/thread19303-3.html


Hope that helps
Assistant Manager, Pharmacy Informatics
Wordpress Learning Blog
Updated : ASP.Net Login Code
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC