Cache Dependent Key/Encryption

A

A. Elamiri

I would like to store some Role Information in a cookie since I cannot use
Session in the AuthenticateRequest method.

I thought of encrypting the cookie using Rijndael Algo. for provider. I
would generate a 16 character key store it as a Cached object and replace it
every 20-30 minutes, if the cookie data does not decrypt then simply reload
it because I would assume that key expired.

Is this a secure way of doing it?
 
H

Hernan de Lahitte

Perhaps you might use the Forms Authentication cookie/ticket management that
is very similar that the one you 'd described but you don't need to warry
about keymanagement and expiration issues. You have an example of this here:

On your login page after the validation step, you get the roles info
(string[] roles) and create/encrypt the cookie like this:

HttpCookie cookie = FormsAuthentication.GetAuthCookie( UserId.Text, false );
FormsAuthenticationTicket ticket = FormsAuthentication.Decrypt(
cookie.Value );

// Store roles inside the Forms cookie.
FormsAuthenticationTicket newticket = new FormsAuthenticationTicket(
ticket.Version,
ticket.Name,
ticket.IssueDate,
ticket.Expiration,
ticket.IsPersistent,
String.Join( "|", roles),
ticket.CookiePath);

cookie.Value = FormsAuthentication.Encrypt(newticket);
Context.Response.Cookies.Set(cookie);
Response.Redirect( FormsAuthentication.GetRedirectUrl( newticket.Name,
newticket.IsPersistent ) );

On the Application_AuthenticateRequest you put this code to load you
Principal object.

if (Context.Request.IsAuthenticated)
{
// retrieve user's identity from httpcontext user
FormsIdentity ident = (FormsIdentity)Context.User.Identity;
// retrieve roles from the authentication ticket userdata field
string[] arrRoles = ident.Ticket.UserData.Split(new char[] {'|'});
// create principal and attach to user
Context.User = new System.Security.Principal.GenericPrincipal(ident,
arrRoles);
}

I hope this help.

Regards,

Hernan de Lahitte
Lagash Systems S.A.
http://weblogs.asp.net/hernandl


This posting is provided "AS IS" with no warranties, and confers no rights.
 
A

A. Elamiri

Thanks!! I'll try that out

--
Abdellah Elamiri
..net Developer
Efficacy through simplicity
Hernan de Lahitte said:
Perhaps you might use the Forms Authentication cookie/ticket management that
is very similar that the one you 'd described but you don't need to warry
about keymanagement and expiration issues. You have an example of this here:

On your login page after the validation step, you get the roles info
(string[] roles) and create/encrypt the cookie like this:

HttpCookie cookie = FormsAuthentication.GetAuthCookie( UserId.Text, false );
FormsAuthenticationTicket ticket = FormsAuthentication.Decrypt(
cookie.Value );

// Store roles inside the Forms cookie.
FormsAuthenticationTicket newticket = new FormsAuthenticationTicket(
ticket.Version,
ticket.Name,
ticket.IssueDate,
ticket.Expiration,
ticket.IsPersistent,
String.Join( "|", roles),
ticket.CookiePath);

cookie.Value = FormsAuthentication.Encrypt(newticket);
Context.Response.Cookies.Set(cookie);
Response.Redirect( FormsAuthentication.GetRedirectUrl( newticket.Name,
newticket.IsPersistent ) );

On the Application_AuthenticateRequest you put this code to load you
Principal object.

if (Context.Request.IsAuthenticated)
{
// retrieve user's identity from httpcontext user
FormsIdentity ident = (FormsIdentity)Context.User.Identity;
// retrieve roles from the authentication ticket userdata field
string[] arrRoles = ident.Ticket.UserData.Split(new char[] {'|'});
// create principal and attach to user
Context.User = new System.Security.Principal.GenericPrincipal(ident,
arrRoles);
}

I hope this help.

Regards,

Hernan de Lahitte
Lagash Systems S.A.
http://weblogs.asp.net/hernandl


This posting is provided "AS IS" with no warranties, and confers no rights.

A. Elamiri said:
I would like to store some Role Information in a cookie since I cannot use
Session in the AuthenticateRequest method.

I thought of encrypting the cookie using Rijndael Algo. for provider. I
would generate a 16 character key store it as a Cached object and
replace
it
every 20-30 minutes, if the cookie data does not decrypt then simply reload
it because I would assume that key expired.

Is this a secure way of doing it?
 

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

Similar Threads


Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,817
Latest member
DicWeils

Latest Threads

Top