asp.net中的forms验证
1. <authentication mode="Forms" >
<forms defaultUrl="~/login.aspx" loginUrl="~/login.aspx" name=".dyj" protection="All" requireSSL="false" >
</forms>
</authentication>
如果forms验证,下面的配置必不可少
<authorization>
<deny users="?"/>
<!--如果使用forms验证没有这个,不会起作用的。禁止匿名访问-->
</authorization>
注意:deny,allow是按照从上而下的顺序来执行的。
2.通过设置与<system.web>同级的location可以设置用户访问某个文件夹的权限,详细见web.config的代码,例如:
<location path="Admin">
<!--Admin需要受限制的文件夹-->
<system.web>
<authorization>
<allow users="admin"/>
<!--admin就是登录框中要验证的用户名-->
<deny users="*"/>
</authorization>
</system.web>
</location>
3.下面就可以在web页面中编写代码了
/// <summary>
/// 从数据库中获取用户名和密码进行验证
/// </summary>
/// <param name="userName"></param>
/// <param name="passWord"></param>
/// <returns></returns>
private bool ValidateUser(string userName, string passWord)
{
using( SqlConnection connection=new SqlConnection(@"Data Source=(local);Initial Catalog=northwind;Integrated Security=True"))
{
connection.Open();
SqlCommand command=new SqlCommand("select count(*) from userinfo where userid=@user and password=@password",connection);
SqlParameter user = new SqlParameter("@user", userName);
command.Parameters.Add(user);
SqlParameter pwd = new SqlParameter("@password", passWord);
command.Parameters.Add(pwd);
int i = Convert.ToInt32(command.ExecuteScalar());//返回首行首列
if (i == 1)
{
return true;
}
else
{
Response.Write("<script>alert('用户名或者密码错误请核实')</script>");
return false;
}
}
}
/// <summary>
/// 登录
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Button1_Click(object sender, EventArgs e)
{
if (ValidateUser(TextBox1.Text.Trim(),TextBox2.Text.Trim()))
{
if (Request["ReturnUrl"] == null || Request["ReturnUrl"] == "")
{
//必须指定验证通过后跳转的页面
FormsAuthentication.RedirectFromLoginPage(TextBox1.Text, false);
if (TextBox1.Text.Trim() == "admin")
{
Response.Redirect("~/admin/default.aspx");
}
else
{
Response.Redirect("Index.aspx");
}
}
else
{
//返回到先前未登录的页面,加入票证
}
FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1,
TextBox1.Text,//用户名
DateTime.Now,//票证发出时本地的日期和时间
DateTime.Now.AddMinutes(30),//票证过期时本地的日期和时间
true,//票证是否储存在持久性cookie
"userData",//储存在票证中用户的特定数据
FormsAuthentication.FormsCookiePath//票证储存在cookie中的路径
);
// 加密票证
string encTicket = FormsAuthentication.Encrypt(ticket);
//创建cookie
Response.Cookies.Add(new HttpCookie(FormsAuthentication.FormsCookieName, encTicket));
FormsAuthentication.RedirectFromLoginPage(TextBox1.Text, true);
}
}
/// <summary>
/// 登出
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Button2_Click(object sender, EventArgs e)
{
FormsAuthentication.SignOut();
Response.Redirect("~/default.aspx");
}
代码全部调试成功了。环境:vs2005.上面就是我编写的所有的页面代码。下面把web.config完整的贴出来。
<?xml version="1.0"?>
<configuration>
<appSettings/>
<connectionStrings/>
<system.web>
<compilation debug="true"/>
<authentication mode="Forms">
<forms defaultUrl="~/Default.aspx" loginUrl="~/default.aspx" name=".dyj" protection="All">
<!--defaultUrl默认页面,loginUrl登录页面,name:cookie名称,protection:cookie加密-->
</forms>
</authentication>
<authorization>
<deny users="?"/>
<!--如果使用forms验证没有这个,不会起作用的。禁止匿名访问-->
</authorization>
</system.web>
<location path="Admin">
<!--Admin需要受限制的文件夹-->
<system.web>
<authorization>
<allow users="admin"/>
<!--admin就是登录框中要验证的用户名-->
<deny users="*"/>
</authorization>
</system.web>
</location>
<location path="public">
<system.web>
<authorization>
<allow users="*"/>
</authorization>
</system.web>
</location>
<location path="teng" allowOverride="false">
<!--teng文件夹下面的web.config不能覆盖authorization节的内容,其余节不受限制-->
<system.web>
<authorization>
<allow users="admin,teng"/>
<!--如果有多个用户名,在中间可以逗号隔开-->
<deny users="*"/>
</authorization>
</system.web>
</location>
<location path="user">
<!--只要登录用户即可访问-->
<system.web>
<authorization>
<deny users="?"/>
</authorization>
</system.web>
</location>
</configuration>
顺便介绍几个Microsoft提供的有用的方法:
1.获取验证模式:User.Identity.AuthenticationType;
2.登录的用户名:User.Identity.Name;