views:

28

answers:

0

I made an OnAuthentication method in MVC app.

public class MyApplication: HttpApplication {
    private static IWindsorContainer container;

    protected void Application_Start()     {
        container = new WindsorContainer();
        Container.Register(
            Component.For<IUserRepository>().ImplementedBy<UserRepository>()
        );
    }

    private void OnAuthentication(object sender, EventArgs e) {
        if (Context.User.Identity.IsAuthenticated)
        {
            var userRepos = container.Resolve<IUserRepository>();
            var user = UserRepos.GetUserByName(Context.User.Identity.Name);
            var principal = new MyPrincipal(user);
            Thread.CurrentPrincipal = Context.User = principal;
            return;
        }
    }
}

After debugging I see that OnAuthentication has been called a lot of times for each tread.

If I use images on the page a request will be produced for each image and call OnAuthentication? Am I right?

I read some articles about authentication and I see that it is common solution to use OnAuthentication method like I have done.

I got an advise to use an HttpModule or a Filter to optimize authentication. How can it be done? Thank you!