Hi there ladies and gentlemen!
I have this code that works in ASP.NET 1.1 but I could not find its equivalent in v 2.0. I had expected it to work having the usual backward compatibility preached by microsoft in mind. But I am yet to succeed. The code 'translation' worked until the need to program in the global.asax file.

The problem is about role based authentication of users using 'forms' authentication. In v1.1 the portion where the need arises to construct GenericPrincipal and formsIdentity objects works well but I cannot find the reference to the GenericPrincipal objects in v2.0

This is version 1.1 code snippets

//included at the top of the global.asax file
using System.Web.Security;
using System.Security.Principal;

In the Application_AuthenticateRequest event handler I add the following code

// Extract the forms authentication cookie
string cookieName = FormsAuthentication.FormsCookieName;
HttpCookie authCookie = Context.Request.Cookies[cookieName];

if(null == authCookie)
{
  // There is no authentication cookie.
  return;
} 
  
FormsAuthenticationTicket authTicket = null;
try
{
  authTicket = FormsAuthentication.Decrypt(authCookie.Value);
}
catch(Exception ex)
{
  return;
}

if (null == authTicket)
{
  // Cookie failed to decrypt.
  return; 
} 
  
// When the ticket was created, the UserData property was assigned a
// pipe delimited string of role names.
string[] roles = authTicket.UserData.Split(new char[]{'|'});
  
// Create an Identity object
FormsIdentity id = new FormsIdentity( authTicket ); 

// This principal will flow throughout the request.
GenericPrincipal principal = new GenericPrincipal(id, roles);
// Attach the new principal object to the current HttpContext object
Context.User = principal;

Version 2.0
All is well until you get to

//you cannot include at the top of the global.asax file
using System.Web.Security;
using System.Security.Principal;

// This principal will flow throughout the request - you cannot find the genericprincipal object!.
GenericPrincipal principal = new GenericPrincipal(id, roles);
// Attach the new principal object to the current HttpContext object
Context.User = principal;

OR
Maybe the question should be where or how do we include the following the in global.asax file in v2.0

//you cannot include at the top of the global.asax file
using System.Web.Security;
using System.Security.Principal;

Pls any help would be appreciated!

Looks like you have it right... System.Security.Principal.GenericPrincipal

What error are you getting?

Looks like you have it right... System.Security.Principal.GenericPrincipal

What error are you getting?

I am yet to get any error:) The point is that I cannot find or create the generic principal objects mainly because I cannot reference the objects by the usage of the "using" clause at the top of the global.asax file. So I repeat, How do I include this code or how do I make the generic principal objects available for use in the global.asax file.

I do hope I am clearer.
Thanks

Looks like you have it right... System.Security.Principal.GenericPrincipal

What error are you getting?

I am yet to get any error:) The point is that I cannot find or create the generic principal objects mainly because I cannot reference the objects by the usage of the "using" clause at the top of the global.asax file. So I repeat, How do I include this code or how do I make the generic principal objects available for use in the global.asax file.

I do hope I am clearer.
Thanks

Sorry, I misunderstood. You can always fully qualify the names (use System.Security.Principal.GenericPrincipal anywhere that you are currently using GenericPrincipal). Otherwise, this article explains why you can't use the "using" key and offers a workaround: http://rossnelson.blogspot.com/2005/11/fixing-globalasax-in-aspnet-20.html

I hope this helps!

Yes, This was what I did eventually to move ahead. Thanks all the same. It was quite nice of you...

And the blogpost you suggested? SUPER DUPER!!!!
Gracias A Plenty!!!!!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.