0

私のasp.net Webアプリケーションでは、Cache.Soを使用して、別のマシンまたは同じPCで同じユーザー名から複数のユーザーがログインするのを防ぎたいのですが、Global.asax.asページで、

私はこの行を置きます...

protected void Application_PreRequestHandlerExecute(Object sender, EventArgs e)
   {
     if (Session["user"] != null) // e.g. this is after an initial logon
     {
        string sKey = (string)Session["user"];
        string sUser = (string)HttpContext.Current.Cache[sKey];
     }
   }

そして私のページのログイン送信ボタンをクリックして、

string userName=uName.Text;
string passWord=pwd.Text;

string sKey = uName.Text;
string sUser = Convert.ToString(Cache[sKey]);
if (sUser == null || sUser == String.Empty)
           {
               TimeSpan SessTimeOut = new TimeSpan(0, 0, HttpContext.Current.Session.Timeout, 0, 0);
               HttpContext.Current.Cache.Insert(sKey, sKey, null, DateTime.MaxValue, SessTimeOut, System.Web.Caching.CacheItemPriority.NotRemovable, null);
               Session["user"] = TextBox1.Text.Trim();

               if (userName.Equals(db_userName) && passWord.Equals(db_password))
               {
                   Response.Write("Welcome " + userName);
               }
               else
               {
                   Response.Write("Invalid Login");
               }
           }
           else
           {
               Response.Write("<Marquee><h1><font color=red>ILLEGAL LOGIN ATTEMPT!!!</font></h1></marquee>");
               return;
           }

しかし、アプリケーションを実行すると、Application_PreRequestHandlerExecute イベントで Session state is not available in this context エラー メッセージが発生しています...

ここで何が問題なのかわかりません...だから、この問題から抜け出すために私を導いてください...

または、これの代わりに別の良いアプローチを教えてください...

4

3 に答える 3

1

セッション状態は、最初のリクエストでは利用できず、後続のリクエストでは null になる場合があります。キー/値だけでなく、セッション状態オブジェクトの null をチェックします。

if(HttpContext.Current.Session!=null){
//Proceed with xyz
}

サイドノート、ここでの Response.Write の使用...無効ではありませんが、典型的ではありません。エリアへのリダイレクトをお勧めします。

于 2012-11-06T06:40:38.953 に答える
0

このイベント ハンドラを使用する

 void Application_AcquireRequestState(object sender, EventArgs e)
   {
       if(HttpContext.Current.Session!=null)
          {
              // Place your Code here 
          }
   }
于 2012-11-06T06:52:24.350 に答える
0

使用するHTTPContext.Current.Session

于 2012-11-06T06:43:54.910 に答える