Help with Authentication Cookie

S

Stuart Shay

Hello All

I am trying to convert a code sample that I have working in VB.NET to C#

The Problem is that in the Application_AuthenticateRequest Event in
Global.asax.cs the cookie is not being evalated , The Sample I have working
is from

http://www.codeproject.com/dotnet/SecurityModelDotNet.asp

Thanks
Stuart


----------------------------------------------------------------------------------
Below is a sample of my C# Code

protected void WindowsAuthentication_OnAuthenticate(object
sender,WindowsAuthenticationEventArgs e)
{
//Check for the existence of the cookie. If it exists then Authentication
Ticket has
//already been created.

if(null == Context.Request.Cookies["authCookie"])
{

//Get User ID from Windows Authenticated Event
string userId = (UserGroupInfo.LoginIdStripDomain(e.Identity.Name));

//Get User Role List
//string roleList = "ADMIN|POWER_USER|USER";

// Create a authentication ticket w/Role List.
FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket(1,
e.Identity.Name,DateTime.Now, DateTime.Now.AddMinutes(60),
false, roleList);

// Encrpt the ticket before setting the cookie value
string encryptedTicket = FormsAuthentication.Encrypt(authTicket);

// Create a cookie and add the encrypted ticket to the cookie as data.
HttpCookie authCookie = new
HttpCookie(FormsAuthentication.FormsCookieName,encryptedTicket);

// Add the cookie to the outgoing cookies collection.
Response.Cookies.Add(authCookie);
}
}

protected void Application_AuthenticateRequest(Object sender, EventArgs e)
{

string cookieName = FormsAuthentication.FormsCookieName;
HttpCookie authCookie = Context.Request.Cookies[cookieName];

if(!(null == authCookie))
{
//HttpCookie authCookie = Context.Request.Cookies["authCookie"];
FormsAuthenticationTicket authTicket =
FormsAuthentication.Decrypt(authCookie.Value);

// Create an Identity object
GenericIdentity userIdentity = new GenericIdentity(authTicket.Name);

string[] roles = authTicket.UserData.Split(new char[]{'|'});
GenericPrincipal principal = new GenericPrincipal(userIdentity, roles);

// Attach the new principal object to the current HttpContext object
HttpContext.Current.User = principal;
}
}
 

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,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top