Form验证之简单应用
1,创建四个文件夹 Admin AdminFolder,DataClass,UserFolder。
Admin:登录以及注册页面
AdminFolder:放入拥有Admin权限的页面
UserFolder:放入拥有User权限的页面
DataClass:Serialize.cs 序列化以及反序列化
LoginInfo.cs:存入登录信息
BasePage.cs 每个页面所要继承取值的类
2,创建一些Web.config
AdminFolder下的config:
<?xml version="1.0"?><configuration> <system.web> <authorization> <allow roles="Admin"/> <deny users="*"/> </authorization> </system.web></configuration>
roles为Admin,如果没有权限,拒绝一切用户。
UserFolder同理。
根目录下Config:
<authentication mode="Forms"> <forms loginUrl="Admin/Login.aspx" timeout="20" path="/" protection="All" /> </authentication> <location path="Admin/Register.aspx"> <system.web> <authorization> <allow users="*"/> </authorization> </system.web> </location>
location写为regist.aspx,代表不限制注册页用户。
loginUrl:默认登录页面。
3,一些代码:
BasePage.cs:
public class BasePage:Page {public LoginInfo LoginUser {get {//从票据中返回UserData,并反序列化为对象 string strUser = ((FormsIdentity)this.Context.User.Identity).Ticket.UserData;return Serialize.DnSerializeFun(strUser); } } }
LoginInfo.cs:
[Serializable]public class LoginInfo {public int id { get; set; }public string Name { get; set; }public DateTime LoginTime { get; set; }public string Roles { get; set; } }
Serializable代表可被序列化。
Serialize.cs:
//对象序列化为字符串 public string SerializeFun(LoginInfo Li) { BinaryFormatter bf = new BinaryFormatter(); MemoryStream ms = new MemoryStream(); bf.Serialize(ms, Li);byte[] objbyte = ms.ToArray();return Convert.ToBase64String(objbyte, 0, objbyte.Length); }//字符串序列化为对象 public static LoginInfo DnSerializeFun(string SerializeStr) {byte[] byt = Convert.FromBase64String(SerializeStr); BinaryFormatter bf = new BinaryFormatter(); MemoryStream ms = new MemoryStream(byt, 0, byt.Length);return bf.Deserialize(ms) as LoginInfo; }
Global.asax:
protected void Application_AuthenticateRequest(Object sender, EventArgs e) {if (this.Context.User != null) {if (this.Context.User.Identity.IsAuthenticated) {if (this.Context.User.Identity is FormsIdentity) {string strUser = ((FormsIdentity)this.Context.User.Identity).Ticket.UserData; string[] roles = DataClass.Serialize.DnSerializeFun(strUser).Roles.Split(','); this.Context.User = new GenericPrincipal(this.Context.User.Identity, roles); } } } }
GenericPrincipal:用户属于哪个权限
两种登录方法:
//自动设置Ticket private void AutoLogin() { FormsAuthentication.SetAuthCookie(TextBox1.Text, false); Response.Redirect("Main.aspx"); }//手动设置Ticket private void TicketLogin() { LoginInfo dl = new LoginInfo(); Serialize sr = new Serialize(); if (TextBox1.Text == "123" && TextBox2.Text == "123") {//LoginInfo赋值 dl.id = 1; dl.Name = TextBox1.Text; dl.LoginTime = DateTime.Now; //判断什么Roles,用,分开 dl.Roles = "User,Admin";//序列化LoginInfo string SeStr = sr.SerializeFun(dl); //定义ticket FormsAuthenticationTicket ft = new FormsAuthenticationTicket(1, "Admin", DateTime.Now, DateTime.Now.AddMinutes(20), false, SeStr); //加密ticket string strTicket = FormsAuthentication.Encrypt(ft); //使用userdata保存cookie HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, strTicket); cookie.Expires = ft.Expiration; Response.Cookies.Add(cookie); Response.Redirect("../AdminFolder/AdminPage.aspx"); }else { Response.Write("密码错误"); } }
页面取值:
public partial class UserPage : DataClass.BasePage {protected void Page_Load(object sender, EventArgs e) { Response.Write(LoginUser.Roles); } }
转载于:https://www.cnblogs.com/fanwenbin/archive/2011/12/10/2283260.html
Form验证之简单应用相关推荐
- python Django Session,CSRF,Model操作,Form验证,中间件,缓存,信号
Django Session,CSRF,Model操作,Form验证,中间件,缓存,信号 Session CSRF Model 操作 Form 验证 中间件 缓存 信号 1,Session 基于coo ...
- Project Web Access 2007自定义FORM验证登录实现 zt
背景: Project Server 2007安装后生成的WEB管理网站Project Web Access默认采用Windows验证,并自动将安装时登录系统的计算机管理员指定为Project Web ...
- Django之Form验证clean方法(验证篇六)
上一篇:Django之Form验证select的choice选项数据从数据库实时调用(验证篇五)点击跳转 目录篇:Django之Form及ModelForm目录篇 点击跳转 下一篇:django之mo ...
- [py][mx]django form验证-给db减压
django form认证-解压db压力 一般系统都需要前后端都验证 前端验证容器逃逸破解,如通过js console口去发 试想如果后端只有db验证,那么前端无论发什么后端都查询一次db,对db压力 ...
- python requests form data_Python爬虫:Request Payload和Form Data的简单区别说明
Request Payload 和 Form Data 请求头上的参数差别在于: Content-Type Form Data Post表单请求 代码示例 headers = { "Cont ...
- [Asp.Net] Form验证中 user.identity为false
这个方法可以是user.identity设置为true FormsAuthentication.SetAuthCookie(Username, true); 但是要开启form验证, 在配置文件中 & ...
- tornado web高级开发项目之抽屉官网的页面登陆验证、form验证、点赞、评论、文章分页处理、发送邮箱验证码、登陆验证码、注册、发布文章、上传图片...
本博文将一步步带领你实现抽屉官网的各种功能:包括登陆.注册.发送邮箱验证码.登陆验证码.页面登陆验证.发布文章.上传图片.form验证.点赞.评论.文章分页处理以及基于tornado的后端和ajax的 ...
- 登录功能中发送邮箱验证的简单使用
登录功能中发送邮箱验证的简单使用 java开发中常用的邮箱相关的就是给邮箱发送验证码,发送验证码在java中使用javamail,它提供了一套发送和接收功能的标准,支持协议:smtp,pop3,ima ...
- 西游之路——python全栈——通用模块(pager、check_code、form验证)
1.验证码 1 import random 2 from PIL import Image, ImageDraw, ImageFont, ImageFilter 3 4 _letter_cases = ...
最新文章
- 信贷风控知识问答库(持续更新)
- 详解在group by分组查询中where 和 having的用法和区别。
- 树莓派vnc用法 linux,怎样使用VNC在树莓派上运行远程桌面
- mumu模拟器点击无响应_常用安卓模拟器介绍?PC模拟器哪个好用
- Unity 修改asset store下载路径(win10)
- 微信脚本配置服务器,微信自动加人脚本教程
- 动态炫酷的404页面源码
- 喜马拉雅难登“喜马拉雅”
- 开发第一步之SMTP协议发送邮件,获取手机的详细信息
- 今天给大家分享用scratch的画笔绘制彩色花瓣!
- 《数据分析思维》:分析方法与业务知识
- 解决win2008 R2远程桌面授权过期的方法
- html怎么设置页脚注释,Word写论文时怎么插入页脚注释?
- 基于Python实现语法分析
- 使用CMake构建/开始使用CMake
- 《Scanner的hasNext、hasNextInt用法》
- NR DCI Format介绍
- Web Scalability for Startup Engineers TipTechniques for Scaling You Web Application --读书笔记
- Oracle rman备份还原
- KMIP4J数据处理流程