0

誰かが私が次の問題の解決策を見つけるのを手伝ってくれますか?

  1. ASP.NET Webサイトの場合:Application_OnPostAuthenticate()イベントで、私が記述したコードはすべて、要求ごとに実行されます。したがって、このcustomidentityオブジェクトにより、countryidとweatheridは、リクエストごとに毎回呼び出されます(値についてはデータベースを呼び出します)。ページの応答時間や不要なコード実行に影響します。

    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;
    }
    

    }

次のようにコードを変更しようとしたときにこの問題を解決するには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

おそらく、すべてのリクエストで発生するイベントにアクセスしたくないでしょう。一部のリクエストにはセッションさえありません(たとえば、多くのWebサービス呼び出し、または静的リソースをロードするWebResource.axdへの呼び出し)。

于 2009-08-01T08:05:43.883 に答える
1

キャッシュオブジェクトに値を追加する前に、それがすでにキャッシュに存在するかどうかを確認してください。

于 2009-08-01T08:27:02.530 に答える
0

セッション状態が有効になっていない可能性があります。他の場所(Webフォームの表示など)で機能しますか?

web.configのsystem.web要素の下にある要素を探し、<sessionState>それがオンになっていることを確認します(Webファームがない場合はInProcに設定します)。

于 2009-08-01T08:19:01.407 に答える