0

Environment: MVC3 SqlMemberShipProvider

I added an empty

public void Application_AuthenticateRequest(object sender, EventArgs e) {}

in the MvcApplication class in Global.asax.cs and put a breakpoint there. It was fired on every page access. The sender is of type MvcApplication, which has a User property and a Context property.

If I am not logged in, User and Context.User are null.

If I am logged in, User and Context.User are set to a GenericPrincipal containing my logged in name.

So it seems that SqlMemberShipProvider (if not who) is setting the User object. May I know how/where this is done?

Thanks.

Old Geezer
  • 14,854
  • 31
  • 111
  • 198

1 Answers1

0

I guess you are using Membership together with FormsAuthentication? Maybe together with login control? If you are using FormsAuthentication then it is FormsAuthenticationModule that reads the identity cookie from the browser and then constructs the IPrincipal object from that cookie.

mortb
  • 9,361
  • 3
  • 26
  • 44
  • Yes to all your questions. The real question I wanted to ask was how does FormsAuthenticationModule access the MvcApplication object in order to set the IPrincipal. – Old Geezer Mar 30 '12 at 14:56
  • Using dotPeek (http://www.jetbrains.com/decompiler/) look for OnAuthenticate in FormsAuthenticationModule. What it does is basically HttpContext.Current.Principal = new Principal(...) – mortb Mar 30 '12 at 15:01
  • Sorry, should be HttpContext.User = new Principal(...) I haven't tried, but maybe you could assign your own value to HttpContext.Current.User in Application_AuthenticateRequest otherwize you may write your own module to handle authenticaiton – mortb Mar 30 '12 at 15:10
  • Found it! The HttpApplication object is passed to FormsAuthenticationMoudle.Init and also FormsAuthenticationEventArgs for various events contains the User object. – Old Geezer Mar 31 '12 at 00:26