473,609 Members | 1,818 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Custom GenericIdentity class for more userdata

Hello

With trial and error I'm attempting to create an extended identity to store
some more data than just the Name, for example a Subscription and a
LastSearchPerfo rmed property...

Is this a good idea ? I'm coming from ASP and Session variables, but I
explicitly wanted to avoid that for .NET.

The problem I'm facing is that I don't find a good way to bring my source
data from the login routine to the AuthenticateReq uest event, as followed in
a little pseudocode which hopefully shows my thoughts and my errors...

[login.aspx]
Button_Click_Ev ent
< GetUserDataFrom Database >
....
FormsAuthentica tion.Redirect(s UserName, False)
End
[global.asax]
Application_Aut henticateReques t( s , e ){
If Request.IsAuthe nticated Then
Dim objIdentity As
myCustomIdentit yClass(Context. User.Identity.N ame)

'/// This is where I need to get the data from <
GetUserDataFrom Database >
'/// which is called in the button click event from the
unrelated page.
'/// How do I get that data here without having to call the
database for every Request ?
....
<assign roles & custom identity to Context.User>
End If
End
The main question is commented in that event,
I hope someone can help me to find the best way to do it, or just tell me
I'm completely on the wrong way to do this.

Thanks,

Beren
Nov 18 '05 #1
1 2629
Rocky Lhotka explains very clearly how to do all this in his book.
http://www.lhotka.net/ArticleIndex.a...ea=CSLA%20.NET

The basic idea is to create a custom Principal class which contains a
reference to the Identity class.
They each implement the appropriate interfaces.
(Rocky provides sample clases.)

Then you can modify your class to include other pieces of information (like
UserID, CompanyName, etc.).
Then just use AcquireRequestS tate to pull your Principal class out of
session at the beginning of each hit.
This way all of the data in it is available throughout the request.

When the user logs in - that is when the class authenticates the user
against a DB (or some other mechanism).
If the login succeeds, you store the Principal class in session for later
use then RedirectFromLog inPage.
=============== =============== =============== =============== =============== ===
Dim mUser As MyPrincipal
mUser.Login(Use rId, Password)
mUser = CType(Thread.Cu rrentPrincipal, MyPrincipal)

If mUser.Identity. IsAuthenticated = True Then
HttpContext.Cur rent.User = mUser
State.CSLA_Prin cipal = mUser
Web.Security.Fo rmsAuthenticati on.RedirectFrom LoginPage(txtUs erId.Text,
False)
Else
'do something about a failed login
End If
=============== =============== =============== =============== =============== ===

Here is the VB code in my Global.asax file:

Private Sub Global_AcquireR equestState(ByV al sender As Object, ByVal e As
System.EventArg s) Handles MyBase.AcquireR equestState

'See pages 509-510 for a lengthy explanation of this code
If Not State.CSLA_Prin cipal Is Nothing Then
Thread.CurrentP rincipal = State.CSLA_Prin cipal
HttpContext.Cur rent.User = State.CSLA_Prin cipal
Else
If Thread.CurrentP rincipal.Identi ty.IsAuthentica ted = True Then
Web.Security.Fo rmsAuthenticati on.SignOut()
Server.Transfer (Request.Applic ationPath + "/Login.aspx")
Else
'Anonymous User
MyPrincipal.Log inAnonymous()
State.CSLA_Prin cipal = CType(Thread.Cu rrentPrincipal, MyPrincipal)
HttpContext.Cur rent.User = State.CSLA_Prin cipal
End If
End If

End Sub

The only "odd" thing in there is the use of a State class for handling
Session variables using strong typing.
These two are equivalent statements:
1. Thread.CurrentP rincipal = State.CSLA_Prin cipal
2. Thread.CurrentP rincipal = Ctype(Session(" CSLA_Principal" ), MyPrincipal)

The first one has intellisense, is easier to read and avoids typos.

Also, I added a LoginAnonymous( ) method to my custom Principal class to
allow some BOs to hit the DB prior to the user logging in.
e.g the login page displays data from the DB so the BO needs to fetch it and
yet no one is logged in yet.

--
Joe Fallon


"Beren" <be***@angband. me> wrote in message
news:KP******** *************@p hobos.telenet-ops.be...
Hello

With trial and error I'm attempting to create an extended identity to
store some more data than just the Name, for example a Subscription and a
LastSearchPerfo rmed property...

Is this a good idea ? I'm coming from ASP and Session variables, but I
explicitly wanted to avoid that for .NET.

The problem I'm facing is that I don't find a good way to bring my source
data from the login routine to the AuthenticateReq uest event, as followed
in a little pseudocode which hopefully shows my thoughts and my errors...

[login.aspx]
Button_Click_Ev ent
< GetUserDataFrom Database >
....
FormsAuthentica tion.Redirect(s UserName, False)
End
[global.asax]
Application_Aut henticateReques t( s , e ){
If Request.IsAuthe nticated Then
Dim objIdentity As
myCustomIdentit yClass(Context. User.Identity.N ame)

'/// This is where I need to get the data from <
GetUserDataFrom Database >
'/// which is called in the button click event from the
unrelated page.
'/// How do I get that data here without having to call the
database for every Request ?
....
<assign roles & custom identity to Context.User>
End If
End
The main question is commented in that event,
I hope someone can help me to find the best way to do it, or just tell me
I'm completely on the wrong way to do this.

Thanks,

Beren

Nov 18 '05 #2

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

Similar topics

3
1465
by: Nick | last post by:
My client uses a SQL Database to store their usernames and passwords, and I do not believe they have AD...no big deal... I wrote a class to create a generic identity and generic principal so that I can use the .IsInRole function for some added security. I would like to do the same by applying an attribute to a method or class. The code I am including works from what I can see, but I am experiencing the following... 1) I cannot add the...
7
1935
by: Luc Tremblay | last post by:
Given the typical following code: void Listener::HandleEvent(const Event& event) { // handling code } In a "clean" fashion, how is it possible to add custom data (to be subsequently accessed) to the Event instance? By custom data i mean practically anything, from a class to a single int. Particularly to my case,
6
2685
by: Gunnar Beushausen | last post by:
Hi! I need a class to store the users data (ID, name etc.) that is accessible from anywhere. At application startup the class gets filled with its data about the user. But how can i access this data from all other classes? Normally to get access, i would have to say something like UserData *UD = new UserData; But this way a new class gets instantieted. What can i do to
6
4355
by: Tim Mulholland | last post by:
I have created my own IIdentity class (actually inherited from GenericIdentity) to contain lots of extra useful information to be passed around with the user's basic information. The class compiles fine, no problems there. My problem is that i'm not sure where to actually set this to be part of the HttpContext. I know that the IIdentity stuff is part of a principal (which in my case will be a GenericPrinciple i suppose since i'm using...
2
4705
by: lucd | last post by:
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
1
6317
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 soap header called by using a http module. The problem Im having is I cannot seem to get the event to raise to fire off my authenticate method in the global.asax. The module is plumbed in to my web.config file Code Below:-
3
1922
by: charles | last post by:
Hi, I am trying to port my ASP application to ASP.Net 2.0 My application is sold to large corporations that have many thousands of users. So I do not use Forms authentication. To make it more convenient for them I developed a custom hybrid model of authentication and authorization. You see, the site administrator is often a non-technical person and
1
4987
by: Jakob Lithner | last post by:
When I started a new ASP project I was eager to use the login facilities offered in Framework 2.0/VS 2005. I wanted: - A custom principal that could hold my integer UserID from the database - An easy way to classify different pages as either Admin, Member or Public, where login is necessary for Admin and Member but not for Public. My idea was to put the pages in different directories to easily keep my order. - An easy menu system that...
4
1988
by: crispin | last post by:
Hi Everyone, I was hoping one of you kind souls could help me with the following: I have a class method which initializes audio libraries written in C, shown below... where it says (RIGHT HERE) I am trying to create a pointer to the class MiniHost, am I doing it right? >>>>> void MiniHost::setupAudio() {
0
8133
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8573
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
7013
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6062
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5517
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4026
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4091
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1676
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1393
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.