简单的Session登录
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>
作者:
mekor
出处:http://www.cnblogs.com/Mekor/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接。如有问题,可以发邮件
邮箱: hiccer@126.com
微博: mekor 联系我,非常感谢。
出处:http://www.cnblogs.com/Mekor/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接。如有问题,可以发邮件
邮箱: hiccer@126.com
微博: mekor 联系我,非常感谢。

浙公网安备 33010602011771号