ASP.NET中如何实现FORM认证登录
如何运用 Form 表单认证
ASP.NET 的安全认证,共有“Windows”“Form”“Passport”“None”四种验证模式。“Windows”与“None”没有起到保护的作用,不推荐使用;“Passport”我又没用过,唉……所以我只好讲讲“Form”认证了。我打算分三部分:
第一部分 —— 怎样实现from 认证;
第二部分 —— Form 认证的实战运用;
第三部分 —— 实现单点登录(Single Sign On)
第一部分 如何运用 Form 表单认证
一、 新建一个测试项目
为了更好说明,有必要新建一个测试项目(暂且为“FormTest”吧),包含三张页面足矣(Default.aspx、Login.aspx、UserInfo.aspx)。啥?有人不会新建项目,不会新增页面?你问我咋办?我看这么办好了:拖出去,打回原藉,从幼儿园学起……
二、 修改 Web.config
1、 双击项目中的Web.config(不会的、找不到的打 PP)
2、 找到下列文字 <authentication mode="Windows" /> 把它改成:
<authentication mode="Forms">
<forms loginUrl="Login.aspx" name=".ASPXAUTH"></forms>
</authentication>
3、 找到<authorization> <allow users="*" /></authorization>换成
<authorization><deny users="?"></deny></authorization>
这里没什么好说的,只要拷贝过去就行。虽说如此,但还是有人会弄错,如下:
<authentication mode="Forms">
<forms loginUrl="Login.aspx" name=".APSX"></forms>
<deny users="?"></deny>
</authentication>
若要问是谁把 <deny users="?"></deny> 放入 <authentication> 中的,我会很荣幸地告诉你,那是 N 年前的我:<authentication> 与 <authorization> 都是以 auth 字母开头又都是以 ation 结尾,何其相似;英文单词背不下来的我以为他们是一伙的……
三、 编写 .cs 代码——登录与退出
1、 登录代码:
a、 书本上介绍的
private void Btn_Login_Click(object sender, System.EventArgs e)
 
相关文档:
1. 注册js函数
String scriptString = "<script language=JavaScript>function doClick(){";
scriptString += "for(var index=0;index<myArray.length;index++)";
scriptString += "alert(myArray[index]);}<";
scriptString += "/" + "script>";
ClientScript.RegisterStartupScript(typeof(WebForm2) ......
1 获取错误信息并到指定页面不要使用Response.Redirect,而应该使用Server.Transfer
e.g
// in global.asax
protected void Application_Error(Object sender, EventArgs e) {
if (Server.GetLastError() is HttpUnhandledException)
Server.Transfer("MyErrorPage.aspx");
//其余的非HttpUnhandledExceptio ......
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<div>
<asp:updatepanel ID="UP" runat="server">
<ContentTemplate>
......
• 不要使用不必要的Session,和ASP中一样,在不必要的时候不要使用Session
• 不使用不必要的Server Control
• 不使用不必要的ViewState
• 不要用Excepti ......