0

任何人都可以帮我找出以下问题的解决方案。

  1. 在 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() 事件中存储会话变量?

4

4 回答 4

2

您可以稍后在请求中尝试执行此操作,例如在PreRequestHandlerExecute事件中:

protected void Application_PreRequestHandlerExecute(object sender, EventArgs e)
{
    IPrincipal objIPrincipal = HttpContext.Current.User;
    if ((objIPrincipal.Identity.IsAuthenticated) && (objIPrincipal.Identity.AuthenticationType == "Forms"))
    {
        HttpSessionState session = HttpContext.Current.Session;
        CustomPrincipal objCustomPrincipal = new CustomPrincipal();
        if (session[objIPrincipal.Identity.Name] == null)
        {
            // get data from database or wherever
            objCustomPrincipal = objCustomPrincipal.GetCustomPrincipalObject(objIPrincipal.Identity.Name);
            CustomIdentity ci = (CustomIdentity)objCustomPrincipal.Identity;
            Object countryID = FatchMasterInfo.GetCountryID(ci.CultureId);
            Object weatherLocationID = FatchMasterInfo.GetWeatherLocationId(ci.UserId);
            // save in session (not cache as cache is application-wide, not per-user):
            session.Add(objIPrincipal.Identity.Name, objCustomPrincipal);
            session.Add(objIPrincipal.Identity.Name + "_CountryID", countryID);
            session.Add(objIPrincipal.Identity.Name + "_WeatherLocationID", weatherLocationID);
        }
        else
        {
            // already have custom principal object in session
            objCustomPrincipal = (CustomPrincipal)session[objIPrincipal.Identity.Name];
        }

        // set the custom principal object to context/thread
        HttpContext.Current.User = objCustomPrincipal;
        Thread.CurrentPrincipal = objCustomPrincipal;
    }
}
于 2009-08-01T07:59:48.030 回答
1

在向缓存对象添加值之前,请检查它是否已存在于缓存中。

于 2009-08-01T08:27:02.530 回答
1

您可能不想在每个请求中发生的任何情况下访问会话。有些请求甚至没有会话(例如,大量的 Web 服务调用,或者对加载静态资源的 WebResource.axd 的调用)。

于 2009-08-01T08:05:43.883 回答
0

您可能没有启用会话状态。它是否可以在其他任何地方使用(例如在 Web 表单的显示中)?

在 web.config 中的 system.web 元素下查找<sessionState>元素,确保它已打开(将其设置为 InProc,除非您有网络场)。

于 2009-08-01T08:19:01.407 回答