asp.net中的forms验证
如果forms验证,下面的配置必不可少
注意:deny,allow是按照从上而下的顺序来执行的。
2.通过设置与同级的location可以设置用户访问某个文件夹的权限,详细见web.config的代码,例如:
3.下面就可以在web页面中编写代码了
///
/// 从数据库中获取用户名和密码进行验证
///
///
///
///
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("alert('用户名或者密码错误请核实')");
return false;
}
}
}
///
/// 登录
///
///
///
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);
}
}
///
/// 登出
///
///
///
protected void Button2_Click(object sender, EventArgs e)
{
FormsAuthentication.SignOut();
Response.Redirect("~/default.aspx");
}
代码全部调试成功了。环境:vs2005.上面就是我编写的所有的页面代码。下面把web.config完整的贴出来。
http://blog.csdn.net/teng_s2000/archive/2007/04/14/1564672.aspx
他们设置了哪些标签:
谁收藏了这个网址:
时间:2008-3-14 0:41:57 | 相关网摘
如果forms验证,下面的配置必不可少
注意:deny,allow是按照从上而下的顺序来执行的。
2.通过设置与同级的location可以设置用户访问某个文件夹的权限,详细见web.config的代码,例如:
3.下面就可以在web页面中编写代码了
///
/// 从数据库中获取用户名和密码进行验证
///
///
///
///
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("alert('用户名或者密码错误请核实')");
return false;
}
}
}
///
/// 登录
///
///
///
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);
}
}
///
/// 登出
///
///
///
protected void Button2_Click(object sender, EventArgs e)
{
FormsAuthentication.SignOut();
Response.Redirect("~/default.aspx");
}
代码全部调试成功了。环境:vs2005.上面就是我编写的所有的页面代码。下面把web.config完整的贴出来。