2011-08-17 9 views
0

私は、セッションSession.Add(「USEREMAIL」、txtUserNameを作成していますログインを作成し、このコードクッキーとセッション

if (ValidateUser(txtUserName.Value,txtUserPass.Value)) 
{ 
    //string useremail = Convert.ToString(txtUserName.Value); 
    Session.Add("useremail", txtUserName.Value); 
    FormsAuthenticationTicket tkt; 
    string cookiestr; 
    HttpCookie ck; 
    tkt = new FormsAuthenticationTicket(1, txtUserName.Value, DateTime.Now, 
    DateTime.Now.AddMinutes(30), chkPersistCookie.Checked, "your custom data"); 
    cookiestr = FormsAuthentication.Encrypt(tkt); 
    ck = new HttpCookie(FormsAuthentication.FormsCookieName, cookiestr); 
    if (chkPersistCookie.Checked) 
    ck.Expires=tkt.Expiration; 
    ck.Path = FormsAuthentication.FormsCookiePath; 
    Response.Cookies.Add(ck); 
} 

を使用してクッキーにユーザーの詳細を格納しています。値); 正常に認証された後、user.aspxにリダイレクトされます user.aspxページのuseremailの値を読みたいのですが、ユーザーページの値にアクセスしようとしたときにuseremailフィールドが表示されません。

protected void Page_Load(object sender, EventArgs e) 
    { 
     if 
      (Session["useremail"] == null) Response.Redirect("Home.aspx"); 
     else 

      BindGridView(useremail); 
    } 

そして、これは私のWebConfigです:私は間違ったをやっている場合

<authentication mode="Forms"><forms name=".YAFNET_Authentication" loginUrl="Home.aspx" protection="All" timeout="43200" cookieless="UseCookies"/></authentication> 

は私を修正します。

Session["useremail"] = "[email protected]"; 

:そしてまた

+0

あなたは 'BindGridView(Session [" useremail "]。ToString());感謝に(!Request.Cookiesは[FormsAuthentication.FormsCookieName] = NULL){ HttpCookie myCookie =新しいHttpCookie(FormsAuthentication.FormsCookieName)場合、私はクッキーを削除するには、このコードを使用していますそのため –

+0

いや。 myCookie.Expires = DateTime.Now.AddDays(-1d); Response.Cookies.Add(myCookie); } //FormsAuthentication.SignOut(); Response.Redirect( "Home.aspx");しかし、クッキーはそこにあり、私はuser.aspxページのログアウト方法を見ることができます – rookie

答えて

1

ちょうどあなたが認証、セッション状態とクッキーとの関係で混乱している

protected void Page_Load(object sender, EventArgs e) 
    { 
     if (Session["useremail"] == null) 
      Response.Redirect("Home.aspx"); 
     else 
      BindGridView((string)Session["useremail"]); 
    } 
+0

感謝を働いた – rookie

1

あなたはこのようにセッション状態にオブジェクトを追加することができ、私はGridViewの関数にその値を渡すことができるようにuser.aspxページにUSEREMAIL値を渡す方法を教えてくださいその後、次のようにそれを取得することができます。

var useremail = Session["useremail"] ?? null; 
if (useremail == null) 
{ 
    //... 
} 
else 
{ 
    BindGridView(useremail); 
} 

項目「USEREMAILは」それは電子メールアドレスが含まれていますotherwhise USEREMAIL変数がnullに設定されたセッションの状態で存在しない場合。

0

に変更します。 ASP.NETでは、セッション状態とフォーム認証がリンクされておらず、つまりスコープが異なります。認証されていないユーザーには、いくつかのセッション状態を持たせることができます。セッション認証とフォーム認証では、追跡の目的で異なるCookieが使用され、Cookie管理は多かれ少なかれ自動化されており、実行したときに管理するためのコードを書く必要はありません。また、あなたがクッキーに保存するものは、セッション状態になるものには関係しません。また、セッション認証とフォーム認証の両方を使用して、クッキーなしで作業を行うことも可能です。したがって、以下のようなコードはセッション状態で動作するはずです

Session["key"] = "put your data here"; 

// retrieve the data elsewhere 
var data = Session["key"]; 
+0

私はこのコードを使用して、クッキーを削除するためにありがとうございますif(Request.Cookies [FormsAuthentication.FormsCookieName]!= null) { HttpCookie myCookie =新しいHttpCookie(FormsAuthentication.FormsCookieName); myCookie.Expires = DateTime.Now.AddDays(-1d); Response.Cookies.Add(myCookie); } //FormsAuthentication.SignOut(); Response.Redirect( "Home.aspx");しかしクッキーはそこにあり、私はuser.aspxのページからログアウトする方法を見ることができます – rookie

関連する問題