剥离领先零

时间:2014-12-17 18:08:25

标签: vb.net visual-studio visual-studio-2012 integer

我正在使用以下代码在AD extensionAttribute3中设置用户PIN。这对1-9的数字很有效。但是,如果有人先输入零,则会将其删除。在第一个数字未被剥离后输入零,只是前导零。 我怎样才能阻止零剥离的发生?有人可以设置037475的密码,这将是一个有效的密码,但最终会被剥夺到37475。

  Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim PIN As Integer
        Dim ADName As String = GetLogonName()
        Dim OSlanguage As ADProperties = Language()
        If EnterPIN.Text.Length < 4 Then
            MsgBox(OSlanguage.PINError2, MsgBoxStyle.Exclamation, OSlanguage.PINError1)
            EnterPINLabel.ForeColor = Color.Red
        ElseIf (CheckConsecutiveChars(EnterPIN.Text, 4)) Then
            MsgBox(OSlanguage.PINError3, MsgBoxStyle.Exclamation, OSlanguage.PINError1)
            EnterPINLabel.ForeColor = Color.Red
        Else
            PIN = Integer.Parse(EnterPIN.Text)
            Dim dirEntry As DirectoryEntry = GetDirectoryEntry()
            Dim dirSearcher As DirectorySearcher = New DirectorySearcher(dirEntry)
            dirSearcher.Filter = "(&(objectClass=user)(objectCategory=person)(sAMAccountName=" & ADName & "))"
            dirSearcher.SearchScope = SearchScope.Subtree
            Dim searchResults As SearchResult = dirSearcher.FindOne()
            If Not searchResults Is Nothing Then
                Dim dirEntryResults As New DirectoryEntry(searchResults.Path)
                SetADProperty(dirEntryResults, "extensionAttribute3", PIN)
                dirEntryResults.CommitChanges()
                dirEntryResults.Close()
                MsgBox(OSlanguage.Success3, MsgBoxStyle.OkOnly, OSlanguage.Success1)
                Form1_Load(Me, New System.EventArgs)
            End If
            dirEntry.Close()
        End If
    End Sub

2 个答案:

答案 0 :(得分:1)

您需要将值读取为字符串而不是整数。

答案 1 :(得分:1)

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim PIN As String = ""
    Dim ADName As String = GetLogonName()
    Dim OSlanguage As ADProperties = Language()
    If EnterPIN.Text.Length < 4 Then
        MsgBox(OSlanguage.PINError2, MsgBoxStyle.Exclamation, OSlanguage.PINError1)
        EnterPINLabel.ForeColor = Color.Red
    ElseIf (CheckConsecutiveChars(EnterPIN.Text, 4)) Then
        MsgBox(OSlanguage.PINError3, MsgBoxStyle.Exclamation, OSlanguage.PINError1)
        EnterPINLabel.ForeColor = Color.Red
    Else
        PIN = EnterPIN.Text.Trim 'Will trim spaces only
        Dim dirEntry As DirectoryEntry = GetDirectoryEntry()
        Dim dirSearcher As DirectorySearcher = New DirectorySearcher(dirEntry)
        dirSearcher.Filter = "(&(objectClass=user)(objectCategory=person)(sAMAccountName=" & ADName & "))"
        dirSearcher.SearchScope = SearchScope.Subtree
        Dim searchResults As SearchResult = dirSearcher.FindOne()
        If Not searchResults Is Nothing Then
            Dim dirEntryResults As New DirectoryEntry(searchResults.Path)
            SetADProperty(dirEntryResults, "extensionAttribute3", PIN) '<-- Attention, if your class supports Integer, it needs to change to string now
            dirEntryResults.CommitChanges()
            dirEntryResults.Close()
            MsgBox(OSlanguage.Success3, MsgBoxStyle.OkOnly, OSlanguage.Success1)
            Form1_Load(Me, New System.EventArgs)
        End If
        dirEntry.Close()
    End If
End Sub

如果你想使用前导零,你只需要将值声明为String的类型而不是整数,也许你需要设置文本框只接受数字作为键。