我有生成验证码的代码。我需要检查是否在24小时内生成了该代码。如果有,我需要它重新发送相同的代码。如果超过24小时,我需要它发送另一个代码。所有这些都有效,除了检查代码是在24小时内生成还是在之后生成。如果需要,我可以解释一下代码。
'This gets the current date
Dim CurrentDate As Date
CurrentDate = Today
'This gets the randomly generated verification code
For i As Integer = 1 To cnt
Dim idx As Integer = r.Next(0, s.Length)
sb.Append(s.Substring(idx, 1))
'This generates another if the Resend Verification code button is clicked
Next
oWebUserGE.SetValue("VerificationCode__pbi", sb.ToString())
oWebUserGE.Save()
oResultCode.Value = "SUCCESS"
Catch ex As System.Exception
oResultCode.Value = "FAILED"
End Try
答案 0 :(得分:0)
如果我没有数据库,我会如何做到这一点。请注意,没有验证检查 - 我留给您。
Imports System.Text
Public Class Form1
Public Sub New()
' This call is required by the designer.
InitializeComponent()
DoWork()
' Add any initialization after the InitializeComponent() call.
End Sub
Private Sub DoWork()
'Test Case. Receiving a code > 24 hours old.
Console.WriteLine("Test 1 Start - Old Code")
Dim sOldVerificationCode As String = GenerateVerificationCode(False)
Dim bUpdate As Boolean = CheckCode(sOldVerificationCode)
If bUpdate Then
Dim sNew As String = GenerateVerificationCode(True)
Console.WriteLine("Generating New Code. Old Code Expired: " + sNew + Environment.NewLine + "Test Passed")
Else
Console.WriteLine("Test Failed.")
End If
Console.WriteLine("Test 1 Finished - Old Code" + Environment.NewLine)
Console.WriteLine("Test 2 Start - New Code")
'Test Case. Receiving a code < 24 hours old.
Dim sNewVerificationCode As String = GenerateVerificationCode(True)
bUpdate = CheckCode(sNewVerificationCode)
If bUpdate Then
Console.WriteLine("Test Failed.")
Else
Console.WriteLine("Code Is Good. No New Code Generated." + Environment.NewLine + "Test Passed")
End If
Console.WriteLine("Test 2 Finished - New Code")
End Sub
Private Function CheckCode(ByVal sOldVerificationCode As String) As Boolean
Dim sVerCodeRaw As String = System.Net.WebUtility.UrlDecode(sOldVerificationCode) 'Url Decode, back to Base64 string.
Dim stringBytes As Byte() = Convert.FromBase64String(sVerCodeRaw) 'Get Base64 bytes.
Dim sVerCode As String = Encoding.UTF8.GetString(stringBytes) 'Get Raw String
Dim sCode As String = sVerCode.Split("|")(0) 'Code part
Dim sDate As String = sVerCode.Split("|")(1) 'Date part
Dim dtOldCode As DateTime = DateTime.Parse(sDate).ToLocalTime()
Return DateDiff(DateInterval.Day, dtOldCode, DateTime.Now) > 0
End Function
Private Function GenerateVerificationCode(ByVal bNew As Boolean) As String 'Dual purpose so we don't need 2 functions for nearly identical tasks.
Dim CurrentDate As DateTime = IIf(bNew, DateTime.Now, DateAdd(DateInterval.Day, -2, DateTime.Now))
Dim sb As New StringBuilder()
sb.Append(Guid.NewGuid.ToString().Replace("-", "")) 'Unique instead of random. Random is not unique.
sb.Append("|" + CurrentDate.ToUniversalTime().ToString()) 'Using UTC in case multiple servers in different timezones.
Dim stringBytes As Byte() = Encoding.UTF8.GetBytes(sb.ToString()) 'Byte array for use in converting to Base64.
Dim verCodeRaw As String = Convert.ToBase64String(stringBytes) 'now a base64 encoded string. This will make it look like gibberish in the
Dim urlVerification As String = System.Net.WebUtility.UrlEncode(verCodeRaw) 'Now URL Safe. Assuming it'll be a Get parameter.
Return urlVerification
End Function
End Class
输出:
Test 1 Start - Old Code
Old Code Expired. Generating New Code: Y2Q1Nzg0M2VjNTVlNDU3Njk1MDU4OTBiMmI1NTEzN2F8Mi8yMy8yMDE4IDc6NTc6NDYgUE0%3D
Test Passed
Test 1 Finished - Old Code
Test 2 Start - New Code
Code Is Good. No New Code Generated.
Test Passed
Test 2 Finished - New Code
您可以插入断点来检查中间值。
我已将其转换为Base64字符串,然后对其进行URL编码。 这不安全但如果作为GET附加到链接上它看起来会更好。
我还使用了GUID而不是随机数生成器。这保证了唯一值 - 您的随机数生成不保证是唯一的。