ASPNET揭秘笔记之八

基于表单的验证

用WebConfig文件验证用户

可以在WC(Web.Config)文件中存储少量的用户名和密码
<Configuration>
    
<System.Web>
       
<authentication mode="forms">
        
<forms>
           
<credentials passwordFormat="Clear">
             
<user name="sam" password="sam"/>
             
<user name="bill" password="gates"/>
            
</credentials>
        
</forms>
       
</authentication>
      
<authorization>
         
<deny users="?" />
      
</authorization>
    
</System.Web>
</Configuration>

要检查一个用户名和相应的密码在WC文件中是否存在,可以使用FormsAuthentication类的Authenticate方法,如果存在,返回True,否则,返回False。

If FormsAuthentication.Authenticate(txtUserName.Text,txtPassword.Text) Then
    FormsAuthentication.RedirectFromLoginPage(txtUsername.Text,
False)
else
    
me.lblMessage.text="用户名或密码错误!"
End if


在WC文件中对密码加密
可以用SHA1和MD5哈希算法来加密密码
<credentials passwordFormat="SHA1">
   
<user name="sam" password="">
</credentials>
将passwordFormat的值设为SHA1并且输入密码的SHA1哈希值。
FormsAuthentication类包含HashPasswordForStoringInConfigFile方法,该方法接受两个参数,一个是要加密的纯文本密码和一个哈希算法的名称(如SHA1或MD5),返回值为经过哈希算法计算后的密码。

strPwd=FormsAuthentication.HashPasswordForstoringInConfigFile(me.txtPassWord.Text.Trim,"SHA1")
posted @ 2008-07-18 11:52  年华倒影  阅读(202)  评论(0)    收藏  举报