ASPNET安全性高级编程 第五章 实现密码策略

来源:互联网 发布:vb编写can通讯的例程 编辑:程序博客网 时间:2024/06/06 13:23

1. 选取密码的最好方法是把A-Z a-z 0-9 之间的字符和几个特殊字符进行随机组合
2 密码的最小长度6-7位(服务器端自定义验证程序)
实例代码
private void CustomValidator1_ServerValidate(object source, System.Web.UI.WebControls.ServerValidateEventArgs args)
  {
  string strPwd=args.Value;
   if(validateLength(strPwd)==false)
   {
    args.IsValid=false;
    err.Text="The length of the password should be at least 7 characters";
   }
   else{
   args.IsValid=true;
    err.Text="";
   }
  }
  bool validateLength(string sPWD){
  //Check the length of the password
   if(sPWD.Length<6)
   {
    return false;
   }
   else{
   return true;
   }
  }
3 混合大小写的密码
4 对数字和特殊字符的支持
不要支持那些能用于脚本攻击的字符
提示:利用正则表达式是限定密码最好的办法
演示代码
bool validateMixedCase2(String sPWD){
      bool foundLower,foundUpper,foundNumeric,foundSymbol;
   foundLower=false;
   foundUpper=false;
   foundNumeric=false;
   foundSymbol=false;
   for(int i=0;i<sPWD.Length;i++){
    if(foundLower==false){
    foundLower=isLower(sPWD[i]);
    }
    if(foundUpper==false){
    foundUpper=isUpper(sPWD[i]);
    }
    if(foundNumeric==false)
    {
     foundNumeric=isNumeric(sPWD[i]);
    }
    if(foundSymbol==false)
    {
     foundSymbol=isSpecialCharacter(sPWD[i]);
    }
   }
   
   if(foundLower==true&&foundUpper==true&&foundNumeric==true&&foundSymbol==true)
   {
    return true;
   }
   else{
   return false;
   }   
  }
  bool isLower(char ch){
  //构造isLower
   if(ch>='a'&&ch<='z')
   {
    return true;
   }
   else{
   return false;
   }
  }
  bool isUpper(char ch){
   if(ch>='A'&&ch<='Z')
   {
    return true;
   }
   else{
   return false;
   }
  }
  bool isNumeric(char ch){
   //检测数字构造函数
   if(ch>='0'&&ch<='9')
   {
    return true;
   }
   else{
   return false;
   }
  }
  bool isSpecialCharacter(char ch){
  //构造检测特殊字符构造函数
   if(ch=='!')
    return true;
   else if(ch=='@')
    return true;
   else if(ch=='#')
    return true;
   else if(ch=='$')
    return true;
   else if(ch=='^')
    return true;
   else if(ch=='*')
    return true;
   else if(ch=='?')
    return true;
   else if(ch=='/')
    return true;
   else if(ch=='//')//Watch for the special character
    return true;
   else
    return false;
  }

  private void Button1_Click(object sender, System.EventArgs e)
  {
   if(validateMixedCase2(name.Text.Trim())==true)
   {
    err.Text="";
   }
   else{
   err.Text="请使用大小写字母数字特殊字符组合";
   }
  }

5 对新密码进行字典检查
字典破解就是所谓的蛮力破解他还可以假冒HTTP POST方法来发送数据
6 密码更新
我们建议用户应该经常更新自己的密码比如每三个月更新一次,我们可以把用户ID 用户名 密码 最近一次被更新的密码和被缩定的时间戳存储在一个简单的表中 当我们对用户进行身份验证时,可以检查时间戳.如果时间戳和当前时间之间的间隔大于一个指定的时间周期,我们就可以强制用户更改他们的密码.
在更新新密码时和最近使用的3个密码进行比较
一定保证您所存储的密码是散列或者加密密码
7 为用户生成随机密码
在创建用户或忘记密码时
演示代码:
private void Button1_Click(object sender, System.EventArgs e)
  {
   Random Rd=new Random();
   pass.Text=Convert.ToString(Rd.Next());
  }
8帮助忘记密码的用户
 (1)以电子邮件的方式向用户发送密码
为一个帐户分配一个新的随机密码 并把密码以电子邮件的方式发送
也可以在用户表中创建一个用为NeedToChangePassword的新列 把他的实质为 True在用户第一次登录时必须要更改这个密码
(2)以电子邮件的方式发送"更改密码的"链接
生成一个新的随机号或GUID(全局唯一标示符)并作为URL的一部分传递给用户
9 防止蛮力攻击
程序只接受3次错误密码,发现这种情况后,我们可以把Locked列设置为true并设置LockedTimeStamp对用户进行锁定然后我们可以显示一条消息告知用户5分钟内他们不能尝试再次登录
当下次用户登录时检查他的时间差,如果时间差超过5分钟则处理这个用户的身份验证请求,如果下于5分钟,则向这个用户发送一个电子邮件,告诉他们有人试图闯入系统,而他们的帐户将被临时锁定.

 
原创粉丝点击