PostAuthenticateRequest fires several times

泪湿孤枕 提交于 2019-12-21 12:45:13

问题


I have Forms authentication and I need a custom object to be stored in HttpContext.Current.User and in Thread.CurrentPrincipal.

To get this, I listen to PostAuthenticateRequest event, then read from the DB all the user data I need to be stored in HttpContext.Current.User and in Thread.CurrentPrincipal, then I instantiate custom IPrincipal and IIdentity objects and assign them to the above locations.

The problem is that for some reason PostAuthenticateRequest fires several times for a single request.. This causes unnecessary DB roundtrips that hurt performance..

How should I address this? (ASP.NET MVC 2)

Thanks.


回答1:


Are you sure that it is running several times for a single request? Remember, every resource such as images and style sheets referenced on your page will trigger this event as they are treated as seperate requests. You are best advised to briefly cache the custom objects and check for their existence in the cache and only going to the DB if not there.

You will need to implement some locking on the cache as these requests typically happen very close together.




回答2:


Just use the tag in your web.config to remove any authentication from your scripts, css and image directories. For example:

<system.web>
...
</system.web>

<location path="~/Scripts">
   <system.web>
      <authorization>
         <allow users="*"/>
      </authorization>
   </system.web>   
</location>


来源:https://stackoverflow.com/questions/3195972/postauthenticaterequest-fires-several-times

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!