//基于forms先把IIS中该应用的匿名访问去掉
<forms>标记的属性
属性 选项 描述
name None 登录身份验证的Cookie名称
loginUrl None 登录页URL.如果没有身份验证Cookie,客户端将被重定向到此URL
protection ALL 应用程序同时使用数据验证和加密来保护Cookie
None 加密和验证都禁用
timeout 一段时间(按分钟计),这段时间之后身份验证Cookie将到期,默认值为30
path 由应用程序发布的Cookie的路径.默认值是反斜杠(/)
<authentication mode="Forms">
<forms name="YourCookieName" loginUrl="login.aspx" protection="ALL"></forms>
</authentication>
//授权
<authorization>
<allow users="?"/> //<allow users="*"/><!--允许所有用户 -->
<!--
<allow users="[逗号分隔的用户列表]"
roles="[逗号分隔的角色列表]"/>
<deny users="[逗号分隔的用户列表]"
roles="[逗号分隔的角色列表]"/>
-->
</authorization>
//login.aspx
登录代码
//连接数据库进行验证
if(true)//用户名是否合法
{
// System.Web.Security.FormsAuthentication.SetAuthCookie(this.TextBox1.Text,false);//指定的用户名写入到Cookie中(false临时Cookie,true永久Cookie)
// Response.Redirect("");
System.Web.Security.FormsAuthentication.RedirectFromLoginPage(this.TextBox1.Text,false);//转到用户原访问的页
//如果为true,可用System..Web.Security.FormsAuthentication.SignOut();删除,以后又要求登录
}
else
{
Response.Write("用户不合法");
}