簡単なセッションログイン

1877 ワード

Loginフロントページ
    <form id="form1" action ="" method="post">

        <input type="text" name="txtN" />

        <input type="password" name="txtP" />

        <input type="submit" value="  " />

    </form>


 Loginバックグラウンドページ
 protected void Page_Load(object sender, EventArgs e)

        {

            if (Request.HttpMethod.ToLower() == "post")

            {

                string strN = Request.Form["txtN"];

                string strP = Request.Form["txtP"];

                //    

                if (strN == "admin" && strP == "123")

                {

                    //      Session 

                    //Context.Session

                    Session["cname"] = strN;

                    //          

                    Response.Redirect("SessionIndex.aspx");

                }

            }

        }


 indexバックグラウンドページ
  protected void Page_Load(object sender, EventArgs e)

        {

            if (Session["cname"] != null && !string.IsNullOrEmpty(Session["cname"].ToString()))

            {

                Response.Write("     ~~:" + Session["cname"].ToString());

            }

            else

            {

                Response.Write("<script>alert('      ~~!');window.location='SessionLogin.aspx';</script>");

                Response.End();

            }

        }


 ブラウザがCookieを無効にしたときに正常にアクセスできないようにするには、Web.configに次のコードを追加する必要があります.
//<system.web>      ,         

      <sessionState cookieless="AutoDetect"></sessionState>