任何人都可以帮我找出以下问题的解决方案。
在 ASP.NET 网站中:在 Application_OnPostAuthenticate() 事件中,我编写的任何代码都会针对每个请求执行。因此,由于这个 customidentity 对象,每次请求都会调用 countryid 和 weatherid(调用数据库以获取值)。它影响页面的响应时间和不必要的代码执行。
void Application_OnPostAuthenticateRequest(对象发送者,EventArgs e){
// Get a reference to the current User IPrincipal objIPrincipal = HttpContext.Current.User; // If we are dealing with an authenticated forms authentication request if ((objIPrincipal.Identity.IsAuthenticated) && (objIPrincipal.Identity.AuthenticationType == "Forms")) { CustomPrincipal objCustomPrincipal = new CustomPrincipal(); objCustomPrincipal = objCustomPrincipal.GetCustomPrincipalObject(objIPrincipal.Identity.Name); HttpContext.Current.User = objCustomPrincipal; CustomIdentity ci = (CustomIdentity)objCustomPrincipal.Identity; HttpContext.Current.Cache["CountryID"] = FatchMasterInfo.GetCountryID(ci.CultureId); HttpContext.Current.Cache["WeatherLocationID"] = FatchMasterInfo.GetWeatherLocationId(ci.UserId); Thread.CurrentPrincipal = objCustomPrincipal; }
}
为了解决这个问题,当我尝试更改代码如下 HttpContext.Current.Session.Add("test", FatchMasterInfo.GetWeatherLocationId(ci.UserId);); 代替缓存,我发现了愚蠢的错误“对象引用未设置为对象实例”
我不知道我们是否可以在 Application_OnPostAuthenticate() 事件中存储会话变量?