</th>
<td>
<asp:TextBox ID="txtPassWord" runat="server" TextMode="Password"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Button ID="submit" runat="server" Text="提交" OnClick="submit_Click" />
</td>
<td>
<asp:TextBox ID="Vcode" runat="server" Width="85px"></asp:TextBox>
<asp:Image ID="Image1" runat="server" ImageUrl="~/ashx/ValidCodeashx.ashx" />
</td>
</tr>
</table>
</div>
</form>
login.aspx.cs文件
[csharp] view plain copy
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace 检查登陆验证
{
public partial class Login : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void submit_Click(object sender, EventArgs e)
{
string vcode = this.Vcode.Text.Trim();
if (this.txtUserName.Text.Trim() == "zhangshan" && this.txtPassWord.Text.Trim() == "123456")
{
if (Session["ValidCode"] != null)
{ string ss=Session["ValidCode"].ToString();
if (vcode ==ss )
{
Session["UserName"] = "zhangshan";
Response.Redirect("Welcome.aspx");
}
}
}
}
}
}
赋送验证码ValidCodeashx.ashx
[csharp] view plain copy
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.SessionState;
namespace 检查登陆验证.ashx
{
///
/// ValidCodeashx 的摘要说明
///
public class ValidCodeashx : IHttpHandler, IRequiresSessionState
{
public void ProcessRequest(HttpContext context)
{
//1.随即生成4位数字(及1000到9999之间的数字)
Random rdm = new Random();
int number = rdm.Next(1000, 10000);
context.Session["ValidCode"] = number.ToString();
CreateValidateGraphic(number.ToString(), context);
}
/// <summary>
/// 创建验证码的图片
/// </summary>
/// <param name="containsPage">要输出到的page对象</param>
/// <param name="validateNum">验证码</param>
public void CreateValidateGraphic(string validateCode, HttpContext context)
{
Bitmap image = new Bitmap((int)Math.Ceiling(validateCode.Length * 12.0), 22);
Graphics g = Graphics.FromImage(image);
try
{
//生成随机生成器
Random random = new Random();
//清空图片背景色
g.Clear(Color.White);
//画图片的干扰线
for (int i = 0; i < 25; i++)
{
int x1 = random.Next(image.Width);
int x2 = random.Next(image.Width);
int y1 = random.Next(image.Height);
int y2 = random.Next(image.Height);
g.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2);
}
Font font = new Font("Arial", 12, (FontStyle.Bold | FontStyle.Italic));
System.Drawing.Drawing2D.LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height),
Color.Blue, Color.DarkRed, 1.2f, true);
g.DrawString(validateCode, font, brush, 3, 2);
//画图片的前景干扰点
for (int i = 0; i < 100; i++)
{
int x = random.Next(image.Width);
int y = random.Next(image.Height);
image.SetPixel(x, y, Color.FromArgb(random.Next()));
}
//画图片的边框线
g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);
//保存图片数据
MemoryStream stream = new MemoryStream();
image.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg);
//输出图片流
context.Response.Clear();
context.Response.ContentType = "image/jpeg";//设置报文头
context.Response.BinaryWrite(stream.ToArray());
}
finally
{
g.Dispose();
image.Dispose();
}
}
public bool IsReusable
{
get
{
return false;
}
}
}
}
2>使用自定义特性来实现,验证用户是否已经登录
思路:首先定义一个验证用户是否已经登录的特性。然后再有需要验证用户是否登录的页面类中打上这个特性标签
首先定义一个特性
[csharp] view plain copy
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.SessionState;
namespace 检查登陆验证.Attributes
{
[AttributeUsage(AttributeTargets.Class)] //这个特性只能用于类
public class CheckLoginAttribute : Attribute //定义一个名字叫CheckLoginAttribute 的特性,用户检查用户是否已经登录
{
//我们知道在一个类上打上[CheckLogin] 就等于 new CheckLoginAttribute() 所以我们在这里定义了这个CheckLoginAttribute特性的不带参数的构造函数,在构造函数中判断用户是否已经登录。如果用户未登录,那么就跳到Login.aspx页面
public CheckLoginAttribute()
{
if (HttpContext.Current.Session["UserName"] == null)
{
HttpContext.Current.Response.Redirect("Login.aspx");
}
}
}
}
现在就来使用一下这个[CheckLogin]特性
[csharp] view plain copy
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using 检查登陆验证.Attributes;
namespace 检查登陆验证.admin
{
[CheckLogin] //因这个Welcome页面只有用户登陆后才能访问,所以在这个页面类上打上验证用户是否登陆的[CheckLogin]特性标签,这样,用户每次访问这个页面的时候就会先执行CheckLogin类中的构造函数中的代码。构造函数中的代码就用户判断用户是否已经登陆,如果未登录就会跳到登录页面了
public partial class Welcome : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
}
}
3>使用HttpModule来实现
如果使用HttpModule来实现的话,最好将需要做登陆验证的页面文件,和不需要做登陆验证的页面文件放到不同的文件夹下。这样的话再HttpModule里面就可以做判断,如果访问这个需要做登陆验证的文件下的文件的时候,就做登陆验证。不需要做登陆验证的文件夹下的页面就可以跳过登陆验证了。
如果将不需要验证登陆的页面和需要验证登陆的页面放在同一个文件夹下的话,就不好做这样的判断。
因为如果所有的页面都做登陆验证的判断的话,如果没有登陆。Login.aspx的Session["UserName"]也是为空的,这样用户在没有登陆的情况下,就永远无法跳转到登陆页面。因为在跳到登陆页面的时候,页面初始化,执行HttpApplication里面的19个事件,就会又执行验证Session["UserName"]是否为空,如果为空,又重新定向,这样就永远重新定向下去。就无法跳转到页面了、
[csharp] view plain copy
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Web;
using 检查登陆验证.Attributes;
namespace 检查登陆验证
{
public class CheckLoginModule : IHttpModule
{
public void Dispose()
{
//throw new NotImplementedException();
}
public void Init(HttpApplication context)
{
context.AcquireRequestState += context_AcquireRequestState;
//context.AcquireRequestState += new EventHandler(context_AcquireRequestState); 也可以这样祖册事件
}
void context_AcquireRequestState(object sender, EventArgs e)
{
HttpApplication application = sender as HttpApplication;
//获取用户请求的url (AbsolutePath:获取Uri的绝对路径)
string url = application.Context.Request.Url.AbsolutePath;
//判断这个Url是不是以/admin开始的。 其实就是判断访问的页面是不是在admin这个文件夹下。 如果访问admin这个文件下的页面那我们就判断用户是否已经登陆。如果不是admin这个文件夹下的目录,那么我就不做登陆判断
if (url.ToLower().StartsWith("/admin"))
{
if (HttpContext.Current.Session["UserNmae"] == null)
{
application.Response.Redirect("Login.aspx");
}
}
}
}
}
在Web.config文件下的节点下添加以下内容
[html] view plain copy
<system.webServer>
</system.webServer>
posted on
2016-02-20 00:34
雪夜
阅读(...)
评论(...)
编辑
收藏
- 用过滤器Filter判断用户是否登陆
用过滤器Filter判断用户是否登陆 WEB.XML <!-- 用户session的 键 sessionKEY --> <context-param> <param- ...
- .Net Mvc判断用户是否登陆、未登陆跳回登陆页、三种完美解决方案
开篇先不讲解,如何判断用户是否登陆,我们先来看用户登录的部分代码,账户密码都正确后,先将当前登录的用户名记录下来. public ActionResult ProcessLogin() { try { ...
- Ecshop在模板中判断用户是否登陆,获取用户等级信息
ecshop模板中smarty怎样判断用户等级.用户id.用户昵称用户名,请看以下方法,使用全局变量 <!-- {if $smarty.session.user_rank gt 1}--> ...
- django 判断用户是否登陆
基于类的视图登陆
- Win 2008 r2 远程桌面多用户登陆,一用户多登陆配置
Windows 2008 R2远程桌面,设置最大连接数,一个登录后另一个就被踢掉等问题 Windows 2008 R2配置如图: 1.打开远程桌面回话主机配置 2.右键RDP-Tcp,属性,可设置最大 ...
- java web 程序---登陆验证session。提示登陆
loigin.jsp <%@ page language="java" import="java.util.*" pageEncoding="g ...
- asp.net 登陆验证 Form表单验证的3种方式 FormsAuthentication.SetAuthCookie;FormsAuthentication.RedirectFromLoginPage;FormsAuthenticationTicket
我们在登陆成功后,使用下面的3种方法,都是同一个目的:创建身份验证票并将其附加到 Cookie, 当我们用Forms认证方式的时候,可以使用HttpContext.Current.User.Ident ...
- 在写WebApi判断用户权限时返回数据和接受支付结果 定义返回数据类型
using ADT.Core.Encrypt; using System; using System.Collections.Generic; using System.Linq; using Sys ...
- django实现自定义登陆验证
django实现自定义登陆验证 自定义装饰器函数和类 from utils.http import HttpResponseUnauthorized from django.views import ...
随机推荐
- Java基础 - 函数与方法
java 是一门面向对象编程,其它语言中的函数也就是java中的方法 方法的基本使用方法 package com.demo7; /* * 函数/方法 * * 定义格式: * 修饰符 返回值类型 方法名 ...
- java基础:父类与子类之间变量和方法的调用
1)父类构造函数 java中当调用某个类的构造方法的时候,系统总会调用父类的非静态初始化块进行初始化,这个调用是隐式的,而且父类的静态初始化代码 块总是会被执行,接着调用父类的一个或者多个构造器执行初 ...
- 20170411 F-02创建财务凭证
SAPMF05A 100 X 0 BDC_OKCODE /00 0 BKPF-BLDAT 20170411 0 BKPF-BLART SA 0 BKPF-BUK ...
- (转)fiddler使用简介--其三
原文地址:http://www.cnblogs.com/miantest/p/7294620.html 我们知道Fiddler是位于客户端和服务器之间的代理,它能够记录客户端和服务器之间的所有 HTT ...
- Python学习笔记3_数据类型
Python数据类型:数字.字符串.列表.元祖.字典 一.数字类型:(整型.长整型.浮点型.复数型) 1.整型(int):表示范围-2,147,483,648到2,147,483,647 2.长整型( ...
- 根据URL请求 返回XML字符串
public static string GetHttpResponse(string url) { string content = ""; // Create a new Ht ...
- scrapy使用笔记
新建项目 在需要新建项目的目录下发指令 scrapy startproject MySpider 其中MySpider为工程的名字,会新建一个文件夹 进入工程目录 新建一个爬虫 scrapy gens ...
- FSR薄膜压力传感器使用教程
FSR薄膜压力传感器教程 本店常用的外形有2种: 圆形: 长条形: 如果用单片机控制建议买带转换的,可以直接接单片机AD口或者数字IO去读取数值: 电压输出的AO接口是模拟量输出,可以接单片机的模拟口 ...
- jquery 字符串转为json
使用ajax从服务器拿到的数据,jquery总是认为是字符串,无法直接使用,可以通过下面代码转换: $.get("服务器路径", function(data) { data = e ...
- 17南宁区域赛 J - Rearrangement 【规律】
题目链接 https://nanti.jisuanke.com/t/19976 题意 给出 一个n 然后 给出 2*n 个数 可以重新排列成两行 然后 相邻的两个数 加起来 不能被三整除 可以上下相邻 ...
|