首页 新闻 论坛 群组 Blog 文档 下载 读书 Tag 网摘 搜索 .NET Java 游戏 视频 人才 外包 数据库 第二书店 程序员

doraemon1293/ 


共15个网摘 [ 1 ]   |  访问doraemon1293的个人空间

asp.net中的forms验证

doraemon1293收录,时间: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完整的贴出来。











































































































共15个网摘 [ 1 ] 

doraemon1293/相关标签



    网站简介广告服务网站地图帮助联系方式诚聘英才English 问题报告
    北京百联美达美数码科技有限公司 版权所有 京 ICP 证 020026 号
    Copyright © 2000-2006, CSDN.NET, All Rights Reserved