ASP.NET 2.0:Httpcontext.current.session.add() 中的问题

发布于 2024-07-30 07:37:11 字数 1349 浏览 4 评论 0原文

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

  1. 在 ASP.NET 网站中:在 Application_OnPostAuthenticate() 事件中,我编写的任何代码都会针对每个请求执行。 因此,由于这个customidentity对象,每次请求都会调用countryid和weatherid(调用数据库获取值)。 它会影响页面的响应时间和不必要的代码执行。

    void Application_OnPostAuthenticateRequest(对象发送者,EventArgs e) {

    //获取当前User的引用 
    
      IPrincipal objIPrincipal = HttpContext.Current.User; 
    
      // 如果我们正在处理经过身份验证的表单身份验证请求 
    
      if ((objIPrincipal.Identity.IsAuthenticated) && (objIPrincipal.Identity.AuthenticationType == "表单")) 
      { 
          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("测试", FatchMasterInfo.GetWeatherLocationId(ci.UserId);); 在缓存的地方我发现了愚蠢的错误 “对象引用未设置为对象的实例”

我不知道我们是否可以将会话变量存储在Application_OnPostAuthenticate()事件中?

Can anybody help me to find out solution of following problem.

  1. In ASP.NET website: at Application_OnPostAuthenticate() event, whatever code i write is executed for every request. therefore due to this customidentity object, countryid and weatherid is called everytime for each request (call for database for value). It effect response time of page and unneccessary code execute.

    void Application_OnPostAuthenticateRequest(object sender, 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;
    }
    

    }

To solve this problem when i try tochange code as follows
HttpContext.Current.Session.Add("test", FatchMasterInfo.GetWeatherLocationId(ci.UserId);); in place of cache i found foolowing error
"Object refrence not set to the instance of object"

I don't know whether we can store session variable inside Application_OnPostAuthenticate() event or not?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

北座城市 2024-08-06 07:37:11

您可以稍后在请求中尝试执行此操作,例如在 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;
    }
}

You could try doing this a bit later in the request, such as in the PreRequestHandlerExecute event:

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;
    }
}
坦然微笑 2024-08-06 07:37:11

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

You probably don't want to access the session in any event that happens in every request. Some requests don't even have session (for instance, a lot of web service calls, or calls to WebResource.axd that load static resources).

喵星人汪星人 2024-08-06 07:37:11

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

Before adding value to cache object, check if it already exists in the cache.

梦里°也失望 2024-08-06 07:37:11

您可能没有启用会话状态。 它可以在其他地方工作吗(比如在网络表单的显示中)?

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

You might not have session state enabled. Does it work anywhere else (like in a web form's display)?

Look for a <sessionState> element under your system.web element in web.config make sure it's turned on (set it to InProc unless you have a web farm).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文