ASP.net的窗体身份验证
来源:互联网 发布:精通d3.js pdf 编辑:程序博客网 时间:2024/05/21 18:33
在我们实际的工作中,froms身份验证用的还是比较多的,我们接下来详细说一下:
做为web开发的程序员,我想登录窗体是接触的太多了。可是,我发现有的程序员在对身份验证的时候是把验证的用户名保存在一个session里的,然后进入系统的每个页面都去验证session是否为空,如果不为空那么就response.redirect("......aspx")。
我认为这种方法相对于asp.net提供的form身份验证来说是不足的,首先,就是增加代码量,因为我们在每个页面都要验证一下session是否存在;其次,session是存储在服务器内存中,我认为如果经常使用session势必会拖慢服务器的速度。而form身份验证则不同,它是把数据保存在cookie中的,所以,可以减轻服务器的压力。
举例一:
在项目中添加两个页面:login.aspx(用来做登录页面)和main.aspx(主界面)
如果我们添加了from身份验证的话,那么当然我们首先要先设置不允许匿名访问网站,接着我们把通过身份验证的用户添加到cookie中,web配置文件如下:
<?xml version="1.0" encoding="utf-8"?><!-- 有关如何配置 ASP.NET 应用程序的详细消息,请访问 http://go.microsoft.com/fwlink/?LinkId=169433 --><configuration> <system.web> <compilation debug="true" targetFramework="4.0" /> <authentication mode="Forms"> <forms name="save" loginUrl="login.aspx" protection="All"> </forms> </authentication> <authorization> <deny users="?"/> </authorization> </system.web></configuration>
说明:
进行设置后,如果我们直接访问main.aspx页面,那么会跳转到login.aspx。
我们在登录按钮下写上如下代码:
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;namespace LastTest{ public partial class login : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void Button1_Click(object sender, EventArgs e) { if (true) //可以通过查询数据库 验证用户是否合法 { //被注释的这两行语句相当于最下面的语句 就是保存用户后转回到原来的页面。 //System.Web.Security.FormsAuthentication.SetAuthCookie(TextBox1.Text, chkIsSavePwd.Checked); //Response.Redirect("main.aspx"); System.Web.Security.FormsAuthentication.RedirectFromLoginPage(TextBox1.Text, chkIsSavePwd.Checked); } else { } } }}
当然们也可以删除身份验证,退出登录,我们在主界面上加一个注销按钮:
注销下的代码:
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;namespace LastTest{ public partial class main : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void Button1_Click(object sender, EventArgs e) { //从浏览器删除from验证票证 System.Web.Security.FormsAuthentication.SignOut(); //重新回到登录页面 Response.Redirect("login.aspx"); } }}
当然,如果一个系统就有几个人用的话,那么我们也可以添加固定用户,然后对用户的密码可以进行加密:如果MD5加密或者SHA1,当然也可以使用clear(明文,不安全)。
- ASP.net的窗体身份验证
- ASP.net的窗体身份验证
- ASP.net基于窗体的身份验证
- ASP.Net:基于窗体的身份验证
- ASP.Net:基于窗体的身份验证
- ASP.Net:基于窗体的身份验证
- ASP.Net:基于窗体的身份验证
- ASP.Net:基于窗体的身份验证
- ASP.Net:基于窗体的身份验证
- ASP.Net:基于窗体的身份验证
- ASP.Net:基于窗体的身份验证
- ASP.Net:基于窗体的身份验证
- ASP.Net:基于窗体的身份验证
- asp.net中的窗体身份验证(完整篇之一:创建asp.net的窗体身份验证方式)
- asp.net中的窗体身份验证(完整篇之二:asp.net的窗体身份验证过程)
- ASP.NET中的窗体身份验证
- ASP.NET中的窗体身份验证
- asp.net窗体身份验证方案
- Windows FTP Error 425: Unable to build data connection
- mysql 常用命令
- 线程同步之AutoResetEvent的使用
- vdagent与server交互
- LINQ to SQL语句(9)之Top/Bottom和Paging和SqlMethods
- ASP.net的窗体身份验证
- Intrepid ASAP 2 Editor 及注册机
- linux下vim命令详解
- SecureCRT 命令行备注
- sql中对比两个字符串中缺少的字符
- 做有中国特色的程序员(转载)
- android 分辨率自适应
- 数据库的自动备份
- navigationController