473,386 Members | 1,803 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Problem setting Principal in context inside of HttpModule

I have an HttpModule that captures the
FormsAuthenticationModule.Authenticate event, and replaces the
HttpContext principal with a custom principal. The code is
straightforward enough, yet when an ASPX page tries to get this custom
principal it fails - the object I inserted is not there. I've tried
having this module declaration at both the top & bottom of the list in
my web.config... What am I missing?

The code:

public class GCTPrincipalInContext : IHttpModule
{
#region IHttpModule Members

public void Init(HttpApplication context)
{
FormsAuthenticationModule faModule =
(FormsAuthenticationModule)context.Modules["FormsAuthentication"];
faModule.Authenticate += new
FormsAuthenticationEventHandler(faModule_Authentic ate);
}

void faModule_Authenticate(object sender, FormsAuthenticationEventArgs
e)
{
if
(HttpContext.Current.Request.Cookies[FormsAuthentication.FormsCookieName]
!= null)
{
FormsAuthenticationTicket ticket =
FormsAuthentication.Decrypt(HttpContext.Current.Re quest.Cookies[FormsAuthentication.FormsCookieName].Value);

User gctUser =
DataRepository.UserProvider.GetByASPUserId(
(Guid)Membership.GetUser(ticket.Name).ProviderUser Key
)[0];

// I've tried setting the HttpContext.Current.User directly, and I
get
// the same result - not my identity in context.
e.User = new GCTPrincipal(new FormsIdentity(ticket), gctUser);
}
}

public void Dispose()
{
return;
}

#endregion
}

Jul 16 '06 #1
1 2566
Hello Greg,

I think you're capturing hte wrong event to do your work in. Instead of
capturing your module's Authenticate requestion, capture the HttpApplicaiton's
AuthenticateRequest:

public class GCTPrincipalInContext : IHttpModule
{
#region IHttpModule Members
public void Init(HttpApplication context)
{
context.AuthenticateRequest +=
new EventHandler(faModule_OnAuthenticateRequest);
}
// ...
}

HTH

Thanks,
Shawn Wildermuth
Speaker, Author and C# MVP
I have an HttpModule that captures the
FormsAuthenticationModule.Authenticate event, and replaces the
HttpContext principal with a custom principal. The code is
straightforward enough, yet when an ASPX page tries to get this custom
principal it fails - the object I inserted is not there. I've tried
having this module declaration at both the top & bottom of the list in
my web.config... What am I missing?

The code:

public class GCTPrincipalInContext : IHttpModule
{
#region IHttpModule Members
public void Init(HttpApplication context)
{
FormsAuthenticationModule faModule =
(FormsAuthenticationModule)context.Modules["FormsAuthentication"];
faModule.Authenticate += new
FormsAuthenticationEventHandler(faModule_Authentic ate);
}
void faModule_Authenticate(object sender,
FormsAuthenticationEventArgs
e)
{
if
(HttpContext.Current.Request.Cookies[FormsAuthentication.FormsCookieNa
me]
!= null)
{
FormsAuthenticationTicket ticket =

FormsAuthentication.Decrypt(HttpContext.Current.Re quest.Cookies[FormsA
uthentication.FormsCookieName].Value);
User gctUser =
DataRepository.UserProvider.GetByASPUserId(
(Guid)Membership.GetUser(ticket.Name).ProviderUser Key
)[0];
// I've tried setting the HttpContext.Current.User directly, and I
get
// the same result - not my identity in context.
e.User = new GCTPrincipal(new FormsIdentity(ticket), gctUser);
}
}
public void Dispose()
{
return;
}
#endregion
}

Jul 16 '06 #2

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

Similar topics

4
by: hazz | last post by:
If I successfully run a VS.NET app which includes the following; ************************** APP 1 **************************** m_iIdnt = new...
11
by: VB Programmer | last post by:
PLEASE HELP.... I'm having trouble. In my login form after I've verified the username/password are valid I do this: Select Case iMyPrivilege Case 0 Dim arrRoles() As String = {"guest"}...
0
by: francois | last post by:
hello, I am using forms authentication and I would like that my authentication cookie expires after let say 1 minutes (just for the exemple). When I log in in my longon page, the user has to...
6
by: Craig Buchanan | last post by:
I have an object, named SiteIdentity, that implements IIdentity. I have added additional properties to this class, Email for instance. I am using forms authentication. In the code behind class...
3
by: David B. Bitton | last post by:
For some odd reason, despite the fact that I assign my own custom IPrincipal to the HttpContext.User property in an HttpApplication.AuthenticateRequest event handler inside of an IHttpModule,...
4
by: Jeff B | last post by:
I am having a very perplexing problem with setting the user's roles. I have tried to figure this out for 2 days now. When the user logs in to the site, I retrieve the roles from the database and...
3
by: JMUApache | last post by:
Hi: I have got a problem with FromsAuthentication for many days. I use "Forms" Authentication in my ASP.NET Web Froms, and I find that I can't singout.... Some Code Here: //In my...
16
by: Victor | last post by:
I have a strange problem in my website. I configured my website to run under 2 worker processes. (web garden enabled). and I stored my user information in the current httpcontext(like...
2
by: helveticus | last post by:
I'm having difficulties setting the expires header for images and css files in my compression HttpModule. Compression is carried out by context_BeginRequest and works fine (code not shown)....
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.