مقابله با پسوردهایی که ساده حدس زده میشوند
نویسنده: وحید نصیری
تاریخ: ۱۳۹۱/۰۸/۱۰ ۲۱:۵
آدرس: www.dntips.ir
| مطالب | ۳۶۹۴ |
| نویسندگان | ۲۷۶ |
| گروههای مطالب | ۱۰۲۴ |
| نقشههای راه | ۱۱۹ |
| دورهها | ۱۴ |
| اشتراکها | ۱۷۹۱۴ |
using System.Linq;
namespace SecurityModule
{
public static class SafePassword
{
public static ISet<string> BadPasswords = new HashSet<string>
{
"password",
"password1",
"123456",
"12345678",
"1234",
"qwerty",
"12345",
"dragon",
"******",
"baseball",
"football",
"letmein",
"monkey",
"696969",
"abc123",
"mustang",
"michael",
"shadow",
"master",
"jennifer",
"111111",
"2000",
"jordan",
"superman",
"harley",
"1234567",
"iloveyou",
"trustno1",
"sunshine",
"123123",
"welcome"
};
public static bool IsSafePasword(this string data)
{
if (string.IsNullOrWhiteSpace(data)) return false;
if (data.Length < 5) return false;
if (BadPasswords.Contains(data.ToLowerInvariant())) return false;
if (data.AreAllCharsEuqal()) return false;
return true;
}
public static bool AreAllCharsEuqal(this string data)
{
if (string.IsNullOrWhiteSpace(data)) return false;
data = data.ToLowerInvariant();
var firstElement = data.ElementAt(0);
var euqalCharsLen = data.ToCharArray().Count(x => x == firstElement);
if (euqalCharsLen == data.Length) return true;
return false;
}
}
}
public partial class RegisterController : Controller
{
//...
[HttpPost]
[OutputCache(Location = OutputCacheLocation.None, NoStore = true)]
public virtual ActionResult CheckPassword(string password1)
{
return Json(password1.IsSafePasword());
}
}
using System.ComponentModel.DataAnnotations;
using System.Web.Mvc;
namespace MyBlog.Models
{
public class RegisterViewModel
{
//...
[Display(Name = "کلمه عبور")]
[Required(ErrorMessage = "لطفا کلمه عبور خود را وارد نمائید")]
[DataType(DataType.Password)]
[StringLength(50, MinimumLength = 5, ErrorMessage = "حداقل طول کلمه عبور 5 حرف است")]
[Remote(action: "CheckPassword", controller: "Register", HttpMethod = "POST",
ErrorMessage = "کلمه عبور وارد شده را راحت میتوان حدس زد!")]
public string Password1 { get; set; }
}
}