473,379 Members | 1,245 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,379 software developers and data experts.

Form authentication & Custom Principal implementation

Hello,
I am currently playing with form authentication & role based
security on a web application.

As seen in the starter kit Time tracker, I setup a custom identity
class (CustomPrincipal) because i wanted some extra info about the
current user,
i need this extra information to be available in pages without having
to query the database at each page request.

I followed the above samples, and setup the following:

I created a CustomPrincipal class with extra properties (code
shortened for readability)
Public Class CustomPrincipal
Implements IPrincipal
...
Public Sub New(ByVal identity As IIdentity, ByVal
Roles() As String)
Public ReadOnly Property Identity() As IIdentity
Implements IPrincipal.Identity
Public Property Name() As String
Public Property Roles() As String()
Public Property UserID() As Integer
Public Property FullName() As String
...
End Class
When the user authenticate through the login form,
i save the authentication cookie, where i included some extra user
info with the roles in the "userdata" field (string delimited)

I can then get this userdata information back in the global.asax
Application_AuthenticateRequest event,

[code:1:6c073b2f24]Dim authTicket As FormsAuthenticationTicket =
FormsAuthentication.Decrypt(authCookie.Value)
Dim UserInformation As String() =
authTicket.userData.Split(";")
Dim roles As String() =
UserInformation(0).Split("|")
Dim id As FormsIdentity = new FormsIdentity(authTicket )
[/code:1:6c073b2f24]

create a CustomPrincipal and assign it to the to the current request

[code:1:6c073b2f24]Dim myPrincipal As New CustomPrincipal (id,
roles)
myPrincipal.UserID =
Ctype(UserInformation(1),integer)
myPrincipal.FullName =
Ctype(UserInformation(2),String)
myPrincipal.ParentCompany =
Ctype(UserInformation(3),Integer)
...
Context.User = myPrincipal[/code:1:6c073b2f24]

It is working very well, but by doing so, all information is stored in
the authcookie... where the size is very limited...

My question is what about the following
approach:

I would like to use a UserInfo class

[code:1:6c073b2f24]Public Class UserInfo

Public UserID As Integer = 0
Public UserLevel As Integer = 0
Public Firstname As String = ""
Public Lastname As String = ""
Public Email As String = ""
Public ParentCompany As Integer = 0
.... and many other properties

End Class[/code:1:6c073b2f24]
Build my CustomPrincipal with it

[code:1:6c073b2f24]Public Class CustomPrincipal
Implements IPrincipal
Protected _Identity As IIdentity
....

Public Sub New(ByVal identity As IIdentity, ByVal
Roles() As String)
_Identity = identity
_arUserRoles = Roles
End Sub

Public ReadOnly Property Identity() As IIdentity Implements
IPrincipal.Identity
Get
Return _Identity
End Get
End Property

Public ReadOnly Property IdentityInfo As UserInfo
Get
Return User.GetInfo(Me.Identity.Name)
End Get
End Property

End Class
Class User

Public shared Function GetInfo(byVal username as string) As
UserInfo

Dim _UserInfo As userInfo

If Session("UserInfo") Is Nothing Then

' build objUserInfo from database
' save the objUserInfo
in Session

Session("UserInfo") = objUserInfo
Else
_UserInfo =
Ctype(Session("UserInfo"),UserInfo)
End If

Return _UserInfo

End Function

End Class[/code:1:6c073b2f24]

So i could store many more information than in the cookie and access
it in all pages through my CustomPrincipal.IdentityInfo
[b:6c073b2f24]What would be the pros & cons of
doing this way ?
Why are all samples relying only on the authentication cookie and not
on session variables to store the identity userdata
?[/b:6c073b2f24]

many many thanks,

Luc

ps: still at the beginning of learning asp.net, so please forgive
errors ;-)

*-----------------------*
Posted at:
www.GroupSrv.com
*-----------------------*
Nov 18 '05 #1
2 4688
"lucd" <ld********@cpexpo-dot-com.no-spam.invalid> wrote in message
news:41**********@Usenet.com...
Hello,

.... So i could store many more information than in the cookie and access
it in all pages through my CustomPrincipal.IdentityInfo
[b:6c073b2f24]What would be the pros & cons of
doing this way ?
Why are all samples relying only on the authentication cookie and not
on session variables to store the identity userdata
?[/b:6c073b2f24]


I suppose that I'd use your technique if I had a lot of information about
the user to store. If it was frequently accessed, and faster to access from
Session than from the database or from Cache, then sure, session works.

But, just how much information about a user do you want to carry around to
every single page? What percentage of that information is used on a typical
page? If most pages use only 20% of the information, then perhaps that 20%
should stay in the IPrincipal, and the rest should be in Session state. This
isn't a big deal, just a separation of responsibilities. If 80% of that data
isn't being used most of the time, then most of your pages shouldn't care if
you change the way that data is represented.

John Saunders
Nov 18 '05 #2
Sounds logical, thanks john

*-----------------------*
Posted at:
www.GroupSrv.com
*-----------------------*
Nov 18 '05 #3

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

Similar topics

1
by: Mohit | last post by:
Hello Friends, Our department has created a few (6) Web Applications and all these applications share a common piece of code which does the "User Authentication". I would like to isolate the...
0
by: francois | last post by:
hello, I am using forms authentication and I would like that my authentication cookie expires after let say 1 minutes (just for the exemple). When I log in in my longon page, the user has to...
3
by: David B. Bitton | last post by:
For some odd reason, despite the fact that I assign my own custom IPrincipal to the HttpContext.User property in an HttpApplication.AuthenticateRequest event handler inside of an IHttpModule,...
1
by: David Kyle | last post by:
Hello Guys (and Girls), I've developed a few web applications now all using a SQL Server database for a backend. Up until now I have tried multiple approaches to Authorization and...
2
by: CodeCowboy | last post by:
I'm sure some of you have done this before and I've been perusing through the forum trying to find some uncomplicated solution. I am trying to extend the existing user.identity object. I would...
6
by: Russell | last post by:
Hello there, keywords so you can find this message: russell mccloy server forms authentication InvalidCastException not working after login logon We have an issue with Forms Authentication. I...
5
by: Jon Skeet [C# MVP] | last post by:
I've run against a problem which I'm *sure* must be easy to solve - but I'm blowed if I can find the answer :( I have a web service which I want to require authentication. I need to authenticate...
1
by: Stu | last post by:
Hi, Im using vis studio 2003 and I think wse is out of the question as clients could be using java which doesnt support it. So I managed to find some code which allows you to develop a custom...
0
by: Benjamin Gufler | last post by:
Hello NG, I'm experiencing problems in configuring DB2 v9.1 on Linux (RedHat AS4) to use Kerberos authentication against an AD (W2K3 R2). IBM Network Authentication Service is installed and...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...

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.