Cookie and FormsAuthenticationTicket Question...

K

Kiran B.

New to .net... I am wondering if I have a user name as userone and this
userone has sepcial sales id 201, how can i associate both userone and 201
in a cookie and access it later on. I can access user name using...

Dim authTicket As FormsAuthenticationTicket = New
FormsAuthenticationTicket(1, txtUserName.Text, DateTime.Now,
DateTime.Now.AddMinutes(60), isCookiePersistent, groups)

It works fine. Now I need a way to associate sales id as they log in and
should be accessible through out their session.



Thanks
 
C

Curt_C [MVP]

Just put it in a Session object item
(C# version)

Write:
Session["SalesID"] = "201";

Read:
string SalesID = Session["SalesID"].ToString();
 
K

Kiran B.

Thanks, i forgot to mention, we authenticate user with active directory
using form.

Thanks
 
J

John Saunders

Kiran B. said:
New to .net... I am wondering if I have a user name as userone and this
userone has sepcial sales id 201, how can i associate both userone and 201
in a cookie and access it later on. I can access user name using...

Dim authTicket As FormsAuthenticationTicket = New
FormsAuthenticationTicket(1, txtUserName.Text, DateTime.Now,
DateTime.Now.AddMinutes(60), isCookiePersistent, groups)

It works fine. Now I need a way to associate sales id as they log in and
should be accessible through out their session.

Assuming that "groups" is the user data for the ticket, I'd put the sales id
in there as well. In your global.asax, when you're interpreting the user
data, you would extract the sales id from the ticket.

Now, you want to put it in Session, and I suppose you could do that.
Personally, I wind up defining a new class implementing IPrincipal and
adding a SalesId property. That way, the SalesId can be accessed from
anywhere, and can even be passed to components which don't know about
Session state.

John Saunders
 
K

Kiran B.

tHANKS JOHN, WOULD EXPLAIN IT IN DETAIL?
John Saunders said:
Assuming that "groups" is the user data for the ticket, I'd put the sales
id in there as well. In your global.asax, when you're interpreting the
user data, you would extract the sales id from the ticket.

Now, you want to put it in Session, and I suppose you could do that.
Personally, I wind up defining a new class implementing IPrincipal and
adding a SalesId property. That way, the SalesId can be accessed from
anywhere, and can even be passed to components which don't know about
Session state.

John Saunders
 
J

John Saunders

Kiran B. said:
tHANKS JOHN, WOULD EXPLAIN IT IN DETAIL?

Answer inline:

Dim salesId As Integer ' = Whatever
Dim userData As String = salesId.ToString() & "|" & groups
Dim authTicket As FormsAuthenticationTicket = New
FormsAuthenticationTicket(1, txtUserName.Text, DateTime.Now,
DateTime.Now.AddMinutes(60), isCookiePersistent, userData )

Public Class MyPrincipal
Inherits System.Security.Principal.GenericPrincipal

Private _salesID As Integer

Public Sub New(ByVal identity As System.Security.Principal.IIdentity,
ByVal roles As String(), ByVal salesID As Integer)
MyBase.New(identity, roles)
_salesID = salesID
End Sub

Public ReadOnly Property SalesId() As Integer
Get
Return _salesID
End Get
End Property
End Class


Then, in Application_AuthenticateRequest in Global.asax.vb, after you
decrypt the Forms Authentication Ticket:

Dim userData As String = ticket.UserData
Dim userDataFields As String() = userData.Split("|"c)
Dim salesId As Integer = Integer.Parse(userDataFields(0))
Dim roles As String() = userDataField(1).Split(","c)
'
Dim principal As New MyPrincipal(Request.User.Identity, roles, salesId)
Request.User= principal

Then, on every authenticated page, you can use:

Dim principal As MyPrincipal = DirectCast(User, MyPrincipal)
' you can now access principal.SalesID

I hope that helps,
John Saunders
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top