盾怪网教程:是一个免费提供流行杀毒软件教程、在线学习分享的学习平台!

在ASP.NET中怎么用C#.NET完成基于表单的验证

时间:2024/11/7作者:未知来源:盾怪网教程人气:

[摘要]( passWord.Length > 25 ) ) System.Diagnostics.Trace.WriteLine( "[ValidateUser] Input vali...
( passWord.Length > 25 ) )
 {
  System.Diagnostics.Trace.WriteLine( "[ValidateUser] Input validation of passWord failed." );
  return false;
 }

 try
 {
  // Consult with your SQL Server administrator for an appropriate connection
  // string to use to connect to your local SQL Server.
  conn = new SqlConnection( "server=localhost;Integrated Security=SSPI;database=pubs" );
  conn.Open();

  // Create SqlCommand to select pwd field from users table given supplied userName.
  cmd = new SqlCommand( "Select pwd from users where uname=@userName", conn );
  cmd.Parameters.Add( "@userName", SqlDbType.VarChar, 25 );
  cmd.Parameters["@userName"].Value = userName;

  // Execute command and fetch pwd field into lookupPassword string.
  lookupPassword = (string) cmd.ExecuteScalar();

  // Cleanup command and connection objects.
  cmd.Dispose();
  conn.Dispose();
 }
 catch ( Exception ex )
 {
  // Add error handling here for debugging.
  // This error message should not be sent back to the caller.
  System.Diagnostics.Trace.WriteLine( "[ValidateUser] Exception " + ex.Message );
 }

 // If no password found, return false.
 if ( null == lookupPassword )
 {
  // You could write failed login attempts here to event log for additional security.
  return false;
 }

 // Compare lookupPassword and input passWord, using a case-sensitive comparison.
 return ( 0 == string.Compare( lookupPassword, passWord, false ) );

}
(注:这段代码的意思是先判断输入的用户名和密码是否符合一定的条件,如上,如果符合则连接到数据库,并且根据用户名来取出密码并返回密码,最后再判断取出的密码是否为空,如果不为空则再判断取出的密码和输入的密码是否相同,最后的false参数为不区分大小写)

 4.在cmdLogin_ServerLick事件里使用下面两种方法中的一种来产生表单验证的cookie并将页面转到指定的页面。
下面提供了两种方法的示例代码,根据你的需要来选择。
 a)在cmdLogin_ServerClick事件里调用RedirectFromLoginPage方法来自动产生表单验证cookie且将页面定向到一个指定的页面。
 private void cmdLogin_ServerClick(object sender,System.EventArgs e)
 {
  if(ValidateUser(txtUserName.value,txtUserPass.Value))

   FormsAuthentication.RedirectFromLoginPage(txtUserName.Value,chkPresistCookie.Checked);
   else
    Response.Redirect("logon.aspx",true);  

 }

 b)产生加密验证票据,创建回应的cookie,并且重定向用户。这种方式给了更多的控制权去让你如何去创建cookie,你也可以连同FormsAuthenticationTicket一起包含一些自定义的数据。
 private void cmdLogin_ServerClick(object sender,System.EventArgs e)
 {
  if(ValidateUser(txtUserName.value,txtUserPass.Value))
  {
   FormsAuthenticationTicket tkt;
   string cookiestr;
   HttpCookie ck;
   tkt=new FormsAuthenticationTicket(1,txtUserName.value,DateTime.Now,DateTime.Now.AddMinutes(30),chkPersistCookie.Checked,"your custom data"); //创建一个验证票据
   cookiestr=FormsAuthentication.Encrypt(tkt);//并且加密票据
   ck=new HttpCookie(FormsAuthentication.FormsCookieName,cookiestr);// 创建cookie
   if(chkpersistCookie.Checked) //如果用户选择了保存密码
    ck.Expires=tkt.Expiratioin;//设置cookie有效期
    ck.Path=FormsAuthentication.FormsCookiePath;//cookie存放路径
   Response.Cookies.Add(ck);
   string strRedirect;
   strRedirect=Request["ReturnUrl"];
   if(strRedirect==null)
    strRedirect="default.aspx";
   Response.Redirect(strRedirect,true);
  }
  else
   Reponse.Redirect("logon.aspx",true);
 }
  5.请确保在InititalizeComponent方法里有如下代码:
   this.cmdLogin.ServerClick += new System.EventHandler(this.cmdLogin_ServerClick);
 
(七)创建一个Default.aspx页面
 这一节创建一个测试页面用来作为当用户验证完之后重定向到的页面。如果用户第一次没有被记录下来就浏览到这个页,这时用户将被重定向到登录页面。
  1.把现有的WebForm1.aspx重命名为Default.aspx,然后在编辑器里打开。

  2.切换到HTML视图,复制以下代码到<form>标签之间:
 <input type="submit" Value="SignOut" runat="server" id="cmdSignOut">
这个按钮用来注销表单验证会话。
  3.切换到设计视图,保存页面。
  4.在后置代码里导入必要的名空间:
 using System.Web.Security;
  5.双击SingOut按钮打开后置代码(Default.aspx.cs),然后把下面代码复制到cmdSingOut_ServerClick事件处理中:
  private void cmdSignOut_ServerClick(object sender,System.EventArgs e)
  {
   FormsAuthentication.SignOut();//注销
   Response.Redirect("logon.aspx",true);
  }
  6.请确认在InititalizeComponent方法中有以下代码:
  this.cmdSignOut.ServerClick += new System.EventHandler(this.cmdSignOut_ServerClick);
  7.保存编译项目,现在可以运行这个应用程序了。
(八)附加提示
  1.如果想要在数据库里安全地存放密码,可以在存放到数据到之前先用FormsAuthentication类里的HashPasswordForStoringInConfigFile函数来加密。(注:将会产生一个哈希密码)
  2.可以在配置文件(Web.config)里存放SQL连接信息,以便当需要时方便修改。
  3.可以增加一些代码来防止黑客使用穷举法来进行登录。例如,增加一些逻辑使用户只能有两三次的登录机会。如果用户在指定的登录次数里无法登录的话,可以在数据库里设置一个标志符来防止用户登录直到此用户访问另一个页面或者请示你的帮助。另外,也可以在需要时增加一些适当的错误处理。
  4.因为用户是基于验证cookie来识别的,所以可以在应用程序里使用安全套接层(SSL)来保护验证cookie和其它有用的信息。
  5.基于表单的验证方式要求客户端的游览器接受或者启用cookies.
  6.在<authentication>配置节里的timeout参数用来控制验证cookies重新产生的间隔时间。可以给它赋一个适当的值来提供更好的性能和安全性。
  7.在Internet上的一些代理服务器或者缓冲可能会缓存一些将会重新返回给另外一个用户的包含Set-Cookie头的Web服务器响应。因为基于表单的验证是使用cookie来验证用户的,所以通过中间代理服务器或者缓冲的话可能会引起用户会被意外地搞错为原本不是要发送给他的用户。
  
  
 参考文章:
  如果想要知道如何通过配置<credentials>节点存放用户名和密码来实现基于表单的验证的话,请参考以下GotDotNet ASP.NET QuickStart示例:
  基于表单的验证:http://www.gotdotnet.com/QuickStart/aspplus/default.aspx?url=/quickstart/aspplus/doc/formsauth.aspx
  如果想要知道如何使用XML文件来存放用户名和密码来实现基于表单的验证的话,请参考SDK文档的以下示例:
 http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconcookieauthenticationusinganxmlusersfile.asp
   如果想要知道更多的关于ASP.NET安全的话,请参考Microsoft .NET Framework Developer's Guide文档:
ASP.NET 安全:  http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconaspnetwebapplicationsecurity.asp
   如果想知道更多关于System.Web.Security名空间的话,请参考:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemWebSecurity.asp
   如果想知道更多的关于ASP.NET配置的话,请参考Microsoft .NET Framework Developer's Guide文档:
ASP.NET配置:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconaspnetconfiguration.asp
ASP.NET配置节点:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpgrfaspnetconfigurationsections.asp
  如果想知道更多关于ASP.NET安全指导的话,请参考MSDN:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnbda/html/authaspdotnet.asp
  如果想知道更多关于ASP.NET的,请参考MSDN新闻组:
http://go.microsoft.com/fwlink/?linkid=5811&clcid=0x409

 这篇文章适用于:
Microsoft ASP.NET (included with the .NET Framework 1.1)
Microsoft Visual C# .NET (2003)
Microsoft ASP.NET (included with the .NET Framework) 1.0
Microsoft Visual C# .NET (2002)
Microsoft SQL Server 2000 (all editions)
Microsoft SQL Server 7.0
Microsoft SQL Server 2000 64 bit (all editions)
 
原文链接:http://support.microsoft.com/default.aspx?scid=kb;en-us;301240



关键词:在ASP.NET中如何用C#.NET完成基于表单的验证




Copyright © 2012-2018 盾怪网教程(http://www.dunguai.com) .All Rights Reserved 网站地图 友情链接

免责声明:本站资源均来自互联网收集 如有侵犯到您利益的地方请及时联系管理删除,敬请见谅!

QQ:1006262270   邮箱:kfyvi376850063@126.com   手机版