I would like my MVC application to check (on every request) if the current user has a profile. If no profile is found, I want to redirect them to the "profile" page for them to submit one.
protected void Application_AuthenticateRequest()
{
if (HttpContext.Current.User != null)
{
// Redirect to profile page if the current user does not have a profile
if (!HttpContext.Current.User.HasProfile())
Response.RedirectToRoute("Profile");
}
}
I have extended the IPrincipal to include a method "User.HasProfile()" to check if the user has a profile. It works, but the problem is the that Application_AuthenticateRequest gets called for every single request, including javascript, css etc...
Moreover, it creates a redirect loop when I try to do Response.RedirectToRoute("Profile").
The only way I have found around this is to add the following to my IF statement before redirecting to the profile page:
!HttpContext.Current.User.HasProfile() && Request.Path != "/system/profile" && !Request.Path.Contains(".")
It checks if the current path is not the profile page (to prevent the redirect loop) and if there is a period in the URL (to allow javascript and css resources to continue to load). Is there a better way? How do you guys handle this?