本程序功能是使用Session将用户输入的用户名保存在Session中(登录成功情况下,登录失败不会有Session值),其它页面想访问时会先判断是否有之前存的Session值。

登录Login.htm页面:

<head>
<title></title>
<script type="text/javascript">
//刷新验证码
function refreshYZM() {
var imgYZM = document.getElementById("imgYZM");
imgYZM.src = "YanZhengMa.ashx?t="+new Date();
}
</script>
</head>
<body>
<form action="Login.ashx" method="post">
<table>
<tr><td>用户名</td><td><input type="text" name="username" /></td></tr>
<tr><td>密码</td><td><input type="password" name="password" /></td></tr>
<tr>
<td>
<img src="YanZhengMa.ashx" id="imgYZM" onclick="refreshYZM()" />
</td><td><input type="text" name="yzm" />
</td>
</tr>
<tr><td><input type="submit" name="btnLogin" value="登录" /></td><td>@msg</td></tr>
</table>
</form>
</body>
</html>

生成验证码YanZhengMa.ashx:

public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "image/jpeg";
Random rand = new Random();
int num = rand.Next(, );//生成4位随机数
string code = num.ToString();
context.Session[Login.YZM] = code;
using (Bitmap bmp = new Bitmap(, ))
{
using (Graphics g = Graphics.FromImage(bmp))
using (Font font = new Font(FontFamily.GenericSerif, ))
{
g.DrawString(code, font, Brushes.Red, new PointF(, ));
}
bmp.Save(context.Response.OutputStream, ImageFormat.Jpeg);
}
}

登录Login.ashx页在(必须实现IRequiresSessionState接口,在using System.Data.SqlClient;命名空间下):

public const string YZM = "YZM";//存储验证码
public const string LOGINUSERNAME = "LoginUserName";//将登录用户名存到Session,供多个页面访问
public const string LOGINURL = "LoginUrl";//尝试登录的时候的页面地址 public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/html";
string btnLogin = context.Request["btnLogin"];//获取点击按钮
string html = CommonHelper.ReadHtml("~/Login.htm");//获取Login.htm页面内容
if (string.IsNullOrEmpty(btnLogin))//判断按钮是否点击
{
html = html.Replace("@msg", "");
context.Response.Write(html);
}
else
{
string yzm = context.Request["yzm"];//获取用户输入的验证码
string yzmInServer = (string)context.Session[YZM];//获取服务器的验证码
if (yzm != yzmInServer)//判断用户输入与服务器的验证码
{
html = html.Replace("@msg","<font color=red>验证验错误</font>");
context.Response.Write(html);
return;
}
string username = context.Request["username"];
string password = context.Request["password"];
int r = (int)SQLHelper.ExecuteScalar("select count(*) from Users where name=@name and password=@password",
new SqlParameter { ParameterName = "@name", Value = username },
new SqlParameter { ParameterName = "@password", Value = password });
if (r <= )
{
html = html.Replace("@msg", "<font color=red>用户名或密码错误</font>");
context.Response.Write(html);
return;
}
else
{
//如果登录成功
context.Session[LOGINUSERNAME] = username;//把当前登录的用户名写到Session中
//获取上一次访问网址的Session值,在QueryYuE.ashx和ChangePassword.ashx中已作判断,
//当没登录时,就设置Session值"LOGINURL"为要访问的页面
string navUrl = (string)context.Session[LOGINURL];
if (navUrl != null)
{
context.Response.Redirect(navUrl);//重定向回到上一次访问的页面,动态获取
}
else
{
context.Response.Write("看,美丽的图片。。。。。");
}
} }
}

修改密码ChangePassword.ashx(须实现IRequiresSessionState):

public void ProcessRequest(HttpContext context)
{
string username = (string)context.Session[Login.LOGINUSERNAME];//获取登录时存入的Session值
if (username == null)//如果没登录强制性的访问此页面,则重定向到登陆页面
{
context.Session[Login.LOGINURL] = context.Request.Url.ToString();//把当前地址存到Session中
context.Response.Redirect("Login.ashx");
}
else
{
context.Response.Write("修改密码。。。,当前登录用户<a href='Loginout.ashx'>退出登录</a>" + username);
}
}

余额查询QueryYuE.ashx(须实现IRequiresSessionState):

public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/html";
string username = (string)context.Session[Login.LOGINUSERNAME];
if (username == null)//如果没登录强制性的访问此页面,则重定向到登陆页面
{
context.Session[Login.LOGINURL] = context.Request.Url.ToString();//把当前地址存到Session中
context.Response.Redirect("Login.ashx");
}
else
{
context.Response.Write("查询余额。。。,当前登录用户。<a href='Loginout.ashx'>退出登录</a>" + username);
}
}

退出登录Loginout.ashx(须实现IRequiresSessionState):

public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/html";
context.Session.Abandon();//销毁Session
context.Response.Redirect("Login.ashx");
}

帮助类CommonHelper.cs:

public class CommonHelper
{
public static string ReadHtml(string path)
{
string fillPath = HttpContext.Current.Server.MapPath(path);
string html = File.ReadAllText(fillPath);
return html;
}
}

Session的使用(登录例案+其它页面访问)的更多相关文章

  1. session过期返回登录页面跳出frame

    session 过期返回登录页面 方法1, HttpSession session = request.getSession(); String LOGIN_ID = (String) session ...

  2. struts2拦截器实现session超时返回登录页面(iframe下跳转到其父页面)

    需求:session超时时,返回登录页面,由于页面嵌套在iframe下,因此要跳转到登录页面的父页面,但是首页,登录页面等不需要进行跳转 实现: java文件:SessionIterceptor.ja ...

  3. session过期,登录页面嵌套问题解决

    项目主页是框架模式时,如果登录后长时间没有活动(操作),存储在session中的登录信息过期了,这时再去进行操作时,就会出现登录页面嵌套的问题,怎么解决呢? 这里介绍一种方法,只需要加上一段javas ...

  4. session失效后跳转到登陆页面

    一.编写Filter拦截器类 package com.pv.utils; import java.io.IOException; import java.io.PrintWriter; import ...

  5. 如何实现登录、URL和页面按钮的访问控制?

    用户权限管理一般是对用户页面.按钮的访问权限管理.Shiro框架是一个强大且易用的Java安全框架,执行身份验证.授权.密码和会话管理,对于Shiro的介绍这里就不多说.本篇博客主要是了解Shiro的 ...

  6. 权限管理系统之集成Shiro实现登录、url和页面按钮的访问控制

    用户权限管理一般是对用户页面.按钮的访问权限管理.Shiro框架是一个强大且易用的Java安全框架,执行身份验证.授权.密码和会话管理,对于Shiro的介绍这里就不多说.本篇博客主要是了解Shiro的 ...

  7. .net MVC使用Session验证用户登录(转载)

    .net MVC使用Session验证用户登录   用最简单的Session方式记录用户登录状态 1.添加DefaultController控制器,重写OnActionExecuting方法,每次访问 ...

  8. easyui datagrid 禁止选中行 EF的增删改查(转载) C# 获取用户IP地址(转载) MVC EF 执行SQL语句(转载) 在EF中执行SQL语句(转载) EF中使用SQL语句或存储过程 .net MVC使用Session验证用户登录 PowerDesigner 参照完整性约束(转载)

    easyui datagrid 禁止选中行   没有找到可以直接禁止的属性,但是找到两个间接禁止的方式. 方式一: //onClickRow: function (rowIndex, rowData) ...

  9. springMVC+request.session实现用户登录和访问权限控制

    用springmvc mybatis实现用户登录登出功能,使用session保持登录状态,并实现禁止未登录的用户访问.感谢谷歌资源,在这里做个学习记录加深自己的印象. 原文在我的https://my. ...

随机推荐

  1. autofac 初步学习

    //数据处理接口 public interface IDal<T> where T : class { void Insert (T model); void Update(T model ...

  2. The C Programming Language (second edition) 实践代码(置于此以作备份)

    1. #include <stdio.h> #include <stdlib.h> #include <math.h> #include<time.h> ...

  3. 一大早居然有骗子还是傻子,真是莫名其妙的,QQ1913522040,一看就是刚申请不久的

  4. zabbix: failed to accept an incoming connection

    错误描述 查日志发现: failed to accept an incoming connection: connection from "192.168.186.132" rej ...

  5. 宿主机为linux、windows分别实现VMware三种方式上网(转)

    一.VMware三种方式工作原理1 Host-only连接方式  让虚机具有与宿主机不同的各自独立IP地址,但与宿主机位于不同网段,同时为宿主主机新增一个IP地址,且保证该IP地址与各虚机IP地址位于 ...

  6. http https 区别

    HTTPS和HTTP的区别 一.https协议需要到ca申请证书,一般免费证书很少,需要交费.  二.http是超文本传输协议,信息是明文传输,https 则是具有安全性的ssl加密传输协议.  三. ...

  7. DetachedCriteria详细使用

    一.基本使用 1. 说明 Restrictions 是产生查询条件的工具类. 2. 定义 可以直接用class 创建 DetachedCriteria searDc = DetachedCriteri ...

  8. HDU4901 The Romantic Hero 计数DP

    2014多校4的1005 题目:http://acm.hdu.edu.cn/showproblem.php?pid=4901 The Romantic Hero Time Limit: 6000/30 ...

  9. C#操作XML类

    XML转换成HTML 1.//装载xsl XslCompiledTransform xslt = new XslCompiledTransform(); xslt.Load("output. ...

  10. POJ 2442 Sequence

    Pro. 1 给定k个有序表,取其中前n小的数字.组成一个新表,求该表? 算法: 由于  a1[1] < a1[2] < a1[3] ... <a1[n] a2[1] < a2 ...