1,创建四个文件夹 Admin AdminFolder,DataClass,UserFolder。

Admin:登录以及注册页面

AdminFolder:放入拥有Admin权限的页面

UserFolder:放入拥有User权限的页面

DataClass:Serialize.cs  序列化以及反序列化

LoginInfo.cs:存入登录信息

BasePage.cs  每个页面所要继承取值的类

2,创建一些Web.config

AdminFolder下的config:

View Code

<?xml version="1.0"?><configuration>    <system.web>        <authorization>            <allow roles="Admin"/>            <deny users="*"/>        </authorization>    </system.web></configuration>

roles为Admin,如果没有权限,拒绝一切用户。

UserFolder同理。

根目录下Config:

View Code

  <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:

View Code

   public class BasePage:Page    {public LoginInfo LoginUser        {get            {//从票据中返回UserData,并反序列化为对象                string strUser = ((FormsIdentity)this.Context.User.Identity).Ticket.UserData;return Serialize.DnSerializeFun(strUser);            }        }    }

LoginInfo.cs:

View Code

  [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:

View Code

      //对象序列化为字符串        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:

View Code

        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:用户属于哪个权限

两种登录方法:

View Code

   //自动设置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("密码错误");            }        }

页面取值:

View Code

   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验证之简单应用相关推荐

  1. python Django Session,CSRF,Model操作,Form验证,中间件,缓存,信号

    Django Session,CSRF,Model操作,Form验证,中间件,缓存,信号 Session CSRF Model 操作 Form 验证 中间件 缓存 信号 1,Session 基于coo ...

  2. Project Web Access 2007自定义FORM验证登录实现 zt

    背景: Project Server 2007安装后生成的WEB管理网站Project Web Access默认采用Windows验证,并自动将安装时登录系统的计算机管理员指定为Project Web ...

  3. Django之Form验证clean方法(验证篇六)

    上一篇:Django之Form验证select的choice选项数据从数据库实时调用(验证篇五)点击跳转 目录篇:Django之Form及ModelForm目录篇 点击跳转 下一篇:django之mo ...

  4. [py][mx]django form验证-给db减压

    django form认证-解压db压力 一般系统都需要前后端都验证 前端验证容器逃逸破解,如通过js console口去发 试想如果后端只有db验证,那么前端无论发什么后端都查询一次db,对db压力 ...

  5. python requests form data_Python爬虫:Request Payload和Form Data的简单区别说明

    Request Payload 和 Form Data 请求头上的参数差别在于: Content-Type Form Data Post表单请求 代码示例 headers = { "Cont ...

  6. [Asp.Net] Form验证中 user.identity为false

    这个方法可以是user.identity设置为true FormsAuthentication.SetAuthCookie(Username, true); 但是要开启form验证, 在配置文件中 & ...

  7. tornado web高级开发项目之抽屉官网的页面登陆验证、form验证、点赞、评论、文章分页处理、发送邮箱验证码、登陆验证码、注册、发布文章、上传图片...

    本博文将一步步带领你实现抽屉官网的各种功能:包括登陆.注册.发送邮箱验证码.登陆验证码.页面登陆验证.发布文章.上传图片.form验证.点赞.评论.文章分页处理以及基于tornado的后端和ajax的 ...

  8. 登录功能中发送邮箱验证的简单使用

    登录功能中发送邮箱验证的简单使用 java开发中常用的邮箱相关的就是给邮箱发送验证码,发送验证码在java中使用javamail,它提供了一套发送和接收功能的标准,支持协议:smtp,pop3,ima ...

  9. 西游之路——python全栈——通用模块(pager、check_code、form验证)

    1.验证码 1 import random 2 from PIL import Image, ImageDraw, ImageFont, ImageFilter 3 4 _letter_cases = ...

最新文章

  1. 信贷风控知识问答库(持续更新)
  2. 详解在group by分组查询中where 和 having的用法和区别。
  3. 树莓派vnc用法 linux,怎样使用VNC在树莓派上运行远程桌面
  4. mumu模拟器点击无响应_常用安卓模拟器介绍?PC模拟器哪个好用
  5. Unity 修改asset store下载路径(win10)
  6. 微信脚本配置服务器,微信自动加人脚本教程
  7. 动态炫酷的404页面源码
  8. 喜马拉雅难登“喜马拉雅”
  9. 开发第一步之SMTP协议发送邮件,获取手机的详细信息
  10. 今天给大家分享用scratch的画笔绘制彩色花瓣!
  11. 《数据分析思维》:分析方法与业务知识
  12. 解决win2008 R2远程桌面授权过期的方法
  13. html怎么设置页脚注释,Word写论文时怎么插入页脚注释?
  14. 基于Python实现语法分析
  15. 使用CMake构建/开始使用CMake
  16. 《Scanner的hasNext、hasNextInt用法》
  17. NR DCI Format介绍
  18. Web Scalability for Startup Engineers TipTechniques for Scaling You Web Application --读书笔记
  19. Oracle rman备份还原
  20. KMIP4J数据处理流程

热门文章

  1. gitbook使用及book.json详细配置
  2. typeof与instanceof的区别
  3. JavaScript学习(五十九)—原型、原型链、闭包以及闭包的不足
  4. 怎样快速学会python_python入门如何更快的学习
  5. 在更新iOS14.5後,App要追踪我們會先收到彈窗提示
  6. 喝什么汤对肝脏有好处?
  7. 哪些蔬菜基本不会使用农药?
  8. 为什么有的人有心事就容易失眠?
  9. 我们就来看看网络算命究竟有哪些套路
  10. getMap(Thread t)