如何在C#.net中验证有效密码 [英] how to authenticate Valid password in C#.net

查看:51
本文介绍了如何在C#.net中验证有效密码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在C#中验证valide密码.net





接受最小长度为8个字符,它包含大小写的组合字符,



数字并且必须至少有一个特殊字符

how to autthenticate valide password in C#.net


that accepts minimum length is 8 char and it contains combination of upper&lower case characters,

numerals and must have at least one special character

推荐答案

许多人尝试使用正则表达式,但这不是评估密码的最佳方式。有些事情使用正则表达式令人惊讶地难以或无法匹配。同时,您的标准看起来非常简单。



您真正需要的算法也非常简单。首先,然后立即知道长度: string.Length 。现在,这是你需要做的。为每个字符类创建一些局部整数变量。比如说,一个用于大写字母,另一个用于小写字母,另一个用于,例如,标点符号,数字等。将它们全部初始化为零。然后遍历所选密码字符串的所有字符。对于每个字符,检查它是否属于一个字符类并增加相应的变量。



在循环中,使用 System.Char 对当前字符进行分类的方法:

http://msdn.microsoft.com/en-us/library/7f0ddtxh(v = vs.110).aspx [ ^ ],

http://msdn.microsoft.com/en-us/library/yyxz6h5w(v=vs。 110).aspx [ ^ ],

http://msdn.microsoft.com/en-us/library/6w3ahtyy(v = vs.110).aspx [ ^ ],

http://msdn.microsoft.com/en-us/library/d1x97616(v = vs.110).aspx [ ^ ],

http://msdn.microsoft.com/en-us/library/9s91f3by(v=vs .110).aspx [ ^ ]等等......



在循环退出时,每个类中都有完整的字符数。



这就是全部。很简单,不是吗?



-SA
Many try to use Regular Expressions, but this is not the best way to evaluate password. Some things are surprisingly difficult or impossible to match using the Regex. At the same time, your criteria look very simple.

The algorithm you really need would be very simple, too. First, then length is known immediately: string.Length. Now, here is what you need to do. Create some local integer variables per the class of character. Say, one for upper-case letter, another for lower-case latter, one more for, say, punctuations, digits, etc. Initialize all of them to zero. Then loop through all the characters of the selected password string. For each character, check up if it belongs to one of the character class and increment corresponding variable.

In the loop, use System.Char methods to classify the current character:
http://msdn.microsoft.com/en-us/library/7f0ddtxh(v=vs.110).aspx[^],
http://msdn.microsoft.com/en-us/library/yyxz6h5w(v=vs.110).aspx[^],
http://msdn.microsoft.com/en-us/library/6w3ahtyy(v=vs.110).aspx[^],
http://msdn.microsoft.com/en-us/library/d1x97616(v=vs.110).aspx[^],
http://msdn.microsoft.com/en-us/library/9s91f3by(v=vs.110).aspx[^], and so on…

On the exit from the loop, you will have complete number of characters in each class.

That's all. Simple, isn't it?

—SA


这篇关于如何在C#.net中验证有效密码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆