我有以下代码:
Imports System.Web.Security
Public Class Form1
Dim symbols As Integer = 0
Private Sub cbSymbols_CheckedChanged(sender As Object, e As EventArgs) Handles cbSymbols.CheckedChanged
If cbSymbols.CheckState = 1 Then
symbols = 1
ElseIf cbSymbols.CheckState = 0 Then
symbols = 0
End If
End Sub
Private Sub btnGenerate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGenerate.Click
Dim password As String = Membership.GeneratePassword(ComboBox1.SelectedIndex + 6, symbols)
Label1.Text = password
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Label1.Font = New Font("Arial", 14)
If ComboBox1.Items.Count > 0 Then
ComboBox1.SelectedIndex = 0 ' The first item has index 0 '
End If
End Sub
End Class
但是,无论我的复选框(cbSymbols)是否被选中,我的密码在勾选时都有1个符号,未勾选2个符号。任何人都可以帮我诊断这个问题和/或改进我的代码吗?感谢。
答案 0 :(得分:1)
来自MSDN
Public Shared Function GeneratePassword ( _
length As Integer, _
numberOfNonAlphanumericCharacters As Integer _
) As String
numberOfNonAlphanumericCharactersType:生成的密码中的最小非字母数字字符数(例如@,#,!,%,&等)。
这解释了为什么你得到了这些结果。 我自己运行了几次测试,甚至只有符号的密码。
<强>更新强>
设置最多特殊字符不是开箱即用的。
您可以选择推出自己的选项:
从MemberShip.GeneratePassword
生成的密码开始,
在password-string中搜索特殊符号
(!char.IsLetterOrDigit(c)
)当你到达柜台时
用随机字母或数字替换它。
从头开始创建自己的密码生成器。