2009-08-01 15 views
0

次の問題の解決方法を見つけ出すことができます。ASP.NET 2.0:Httpcontext.current.session.add()の問題

  1. ASP.NET Webサイト:Application_OnPostAuthenticate()イベントでは、すべての要求に対してすべてのコードが実行されます。したがって、このカスタム識別オブジェクトのために、countryidとweatheridは毎回リクエストごとに呼び出されます(値のデータベースの呼び出し)。ページのレスポンスタイムと不要なコードが実行されます。

    空隙Application_OnPostAuthenticateRequest(オブジェクト送信者、のEventArgs電子) {

    // 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を(次のようにiはコードtochangeしようとすると、この問題を解決するために"test"、FatchMasterInfo.GetWeatherLocationId(ci.UserId););キャッシュの代わりにエラーが見つかりました "オブジェクトのインスタンスがオブジェクトのインスタンスに設定されていません"

私は、Application_OnPostAuthenticate()イベント内にセッション変数を保存できるかどうかわかりませんか?

答えて

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

@ironsam、ありがとうございました。私は同じ についてより多くのクエリを持っています。GetCustomPrincipalObject()を使用することによって、ログインしたユーザのロールと権限を取得しています。ここで役割と権限を見つけるのは良い習慣か、コードを変更して他の場所に配置する必要があります。 2. sessionstartイベントがsession.add()の正しい場所でない理由を教えてください。 私が提供したソリューションを実装しています。私はそれが問題を解決することを望む。 –

+0

編集:@ironsam、あなたが提供するソリューションを試しました。 次のコードを使用してaspxページでユーザーを取得するとき CustomIdentity ci = CustomIdentityとしてHttpContext.Current.User.Identity; ciの値はnullになります。 私は何をしたのですか? –

+0

aspxページで必要な場合は、セッションオブジェクトからCustomIdentityを取得できるはずです。 CustomPrincipal pi =(CustomPrincipal)session [objIPrincipal.Identity.Name]; CustomIdentity ci =(CustomIdentity)pi.Identity; ベストプラクティスについては、リクエストで特定のイベントが発生した場合は、を参照してください。 – ironsam

1

あなたはおそらく、すべてのリクエストで起こるいずれにしてもセッションにアクセスする必要はありません。要求の中にはセッション(例えば、多くのWebサービスコールや静的リソースをロードするWebResource.axdへの呼び出しなど)がないものもあります。

0

セッション状態が有効になっていない可能性があります。 Webフォームの表示のようにどこでも動作しますか?

web.configのsystem.web要素の下にある<sessionState>要素を探します(Webファームがない場合は、InProcに設定します)。

1

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