ASP.NET MVC Redirects Dilemma to Application_AuthenticateRequest

I want my MVC application to check (for each request) if the current user has a profile. If no profile is found, I want to redirect them to the Profile page to send them.

    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 turned on IPrincipal to enable the "User.HasProfile ()" method to check if the user has a profile. It works, but the problem is that Application_AuthenticateRequest is called for every single request, including javascript, css, etc.

In addition, it creates a redirect loop when I try to execute Response.RedirectToRoute ("Profile").

The only way I found 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(".")

, ( ), URL ( javascript css ). ? , , ?

+3
1

, , , Handler , .

void LogRequestInfo(object sender, EventArgs e)
{

        HttpApplication app = (HttpApplication)sender;
        HttpContext ctx = app.Context;

        // We don't want to spend resources logging static file requests.  This code was added when we moved
        // to Integrated mode in IIS.
        if (ctx.Handler == null)
            return;

// More code below here...

}
+3

Source: https://habr.com/ru/post/1760379/


All Articles