473,406 Members | 2,345 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Authentication in Asp.Net 2.0

Hello,

I am working on my first Asp.Net 2.0 web site using VS2005.
I know about the login controls on Asp.Net 2.0 but they seem really
strict.
I would prefer to create my own forms and using the new Asp.Net 2.0
classes.

On this web site I need to have authentication for users.
I also need to set an access level for 4 types of users.
And I need to have an option such as "Remember Me".

Can someone tell me how to do this?

I have my Asp.Net 1.1 authentication code but I am having a few problems
in converting it to Asp.Net 2.0.

Thank You Very Much,
Miguel

My Asp.Net 1.1 Authentication Code is:

' Run Validation and Login
If Form_Validation() = True Then

' Forms Authentication Initialization
FormsAuthentication.Initialize()

' Set Connection
Dim connectionString As String =
System.Configuration.ConfigurationSettings.AppSett ings("connectionString")
Dim dbConnection As System.Data.IDbConnection = New
System.Data.OleDb.OleDbConnection(connectionString )

' Set Query
Dim queryString As String = "SELECT [t_4web_users].[access_level]
FROM [t_4web_users] WHERE (([t_4web_users].[username] = @page) AND
([t_4web_users].[password] = @password))"

' Set Command
Dim dbCommand As System.Data.IDbCommand = New
System.Data.OleDb.OleDbCommand
dbCommand.CommandText = queryString
dbCommand.Connection = dbConnection

' Add Parameters
With dbCommand.Parameters
.Add(New OleDbParameter("@username", tbusername.Text))
.Add(New OleDbParameter("@password",
FormsAuthentication.HashPasswordForStoringInConfig File(tbpassword.Text,
"md5")))
End With

' Execute the Command
dbConnection.Open()
Dim reader As OleDbDataReader = dbCommand.ExecuteReader
If reader.Read Then
Dim ticket As FormsAuthenticationTicket = New
FormsAuthenticationTicket(1, tbusername.Text, DateTime.Now,
DateTime.Now.AddMinutes(30), True, reader.GetString(0),
FormsAuthentication.FormsCookiePath)
Dim hash As String = FormsAuthentication.Encrypt(ticket)
Dim cookie As HttpCookie = New
HttpCookie(FormsAuthentication.FormsCookieName, hash)
If ticket.IsPersistent Then
cookie.Expires = ticket.Expiration
End If
Response.Cookies.Add(cookie)
Dim returnUrl As String = Request.QueryString("ReturnUrl")
If returnUrl Is Nothing Then
returnUrl = "index4web.aspx"
End If
Login_Error_Message("none")
Response.Redirect(returnUrl)
Else
Login_Error_Message("loginaccessdenied")
End If
reader.Close()
dbConnection.Close()

Else
End If

Dec 16 '05 #1
1 1142
Really Strict? You can manage how strict or "laid back" your authentication
process is simply by managing the entries in the web.config file.

Jeff

"Miguel Dias Moura" wrote:
Hello,

I am working on my first Asp.Net 2.0 web site using VS2005.
I know about the login controls on Asp.Net 2.0 but they seem really
strict.
I would prefer to create my own forms and using the new Asp.Net 2.0
classes.

On this web site I need to have authentication for users.
I also need to set an access level for 4 types of users.
And I need to have an option such as "Remember Me".

Can someone tell me how to do this?

I have my Asp.Net 1.1 authentication code but I am having a few problems
in converting it to Asp.Net 2.0.

Thank You Very Much,
Miguel

My Asp.Net 1.1 Authentication Code is:

' Run Validation and Login
If Form_Validation() = True Then

' Forms Authentication Initialization
FormsAuthentication.Initialize()

' Set Connection
Dim connectionString As String =
System.Configuration.ConfigurationSettings.AppSett ings("connectionString")
Dim dbConnection As System.Data.IDbConnection = New
System.Data.OleDb.OleDbConnection(connectionString )

' Set Query
Dim queryString As String = "SELECT [t_4web_users].[access_level]
FROM [t_4web_users] WHERE (([t_4web_users].[username] = @page) AND
([t_4web_users].[password] = @password))"

' Set Command
Dim dbCommand As System.Data.IDbCommand = New
System.Data.OleDb.OleDbCommand
dbCommand.CommandText = queryString
dbCommand.Connection = dbConnection

' Add Parameters
With dbCommand.Parameters
.Add(New OleDbParameter("@username", tbusername.Text))
.Add(New OleDbParameter("@password",
FormsAuthentication.HashPasswordForStoringInConfig File(tbpassword.Text,
"md5")))
End With

' Execute the Command
dbConnection.Open()
Dim reader As OleDbDataReader = dbCommand.ExecuteReader
If reader.Read Then
Dim ticket As FormsAuthenticationTicket = New
FormsAuthenticationTicket(1, tbusername.Text, DateTime.Now,
DateTime.Now.AddMinutes(30), True, reader.GetString(0),
FormsAuthentication.FormsCookiePath)
Dim hash As String = FormsAuthentication.Encrypt(ticket)
Dim cookie As HttpCookie = New
HttpCookie(FormsAuthentication.FormsCookieName, hash)
If ticket.IsPersistent Then
cookie.Expires = ticket.Expiration
End If
Response.Cookies.Add(cookie)
Dim returnUrl As String = Request.QueryString("ReturnUrl")
If returnUrl Is Nothing Then
returnUrl = "index4web.aspx"
End If
Login_Error_Message("none")
Response.Redirect(returnUrl)
Else
Login_Error_Message("loginaccessdenied")
End If
reader.Close()
dbConnection.Close()

Else
End If

Dec 16 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Michael Foord | last post by:
#!/usr/bin/python -u # 15-09-04 # v1.0.0 # auth_example.py # A simple script manually demonstrating basic authentication. # Copyright Michael Foord # Free to use, modify and relicense. #...
8
by: Bob Everland | last post by:
I have an application that is ISAPI and the only way to secure it is through NT permissions. I need to have a way to login to windows authentication so that when I get to the ISAPI application no...
6
by: Billy Jacobs | last post by:
I have a website which has both secure and non-secure pages. I want to uses forms authentication. How do I accomplish this? Originally I had my web.config file in the root with Forms...
9
by: Tom B | last post by:
In my web.config file I've specified Windows for the authentication, in IIS I've set it to Integrated Authentication. But my SQL connection is still showing Anonymous. Is there somewhere else I...
0
by: Anonieko Ramos | last post by:
ASP.NET Forms Authentication Best Practices Dr. Dobb's Journal February 2004 Protecting user information is critical By Douglas Reilly Douglas is the author of Designing Microsoft ASP.NET...
4
by: Andrew | last post by:
Hey all, I would like to preface my question by stating I am still learning ASP.net and while I am confident in the basics and foundation, the more advanced stuff is still a challenge. Ok....
0
by: Albertas | last post by:
What I'm doing wrong that I can't make my authentication to work. Here is the situation: I'm hosting a Web Service from a Windows forms application, using .NET Framework 3.0 WCF. And I want to...
18
by: troywalker | last post by:
I am new to LDAP and Directory Services, and I have a project that requires me to authenticate users against a Sun Java System Directory Server in order to access the application. I have found...
2
by: Frank Swarbrick | last post by:
I am trying to understand "client authentication" works. My environment is DB2/UDB LUW 8.2 on zSeries SLES9 as the database server and DB2 for VSE 7.4 as the client. We currently have DB2/LUW set...
5
by: Rory Becker | last post by:
Having now created a Custom MembershipProvider that seems to work correctly with my Logon and ChangePassword controls, I am, as they say, a happy bunny. The next stange is to move on to the...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.