根据当前日期时间和LastLockoutDate显示用户解锁的剩余时间

时间:2014-11-29 18:09:44

标签: asp.net vb.net

我正在尝试显示用户帐户何时自动解锁的计数器。

我希望存储在unlockCounter中的计数器只显示剩余的分钟数。

unlockDate是以如下格式存储的DateTime:11/29/2014 1:06:05 PM。

编辑:这最终是我提出的完美工作。更新以防其他人感兴趣。

If (CurrentUser IsNot Nothing) Then

    If (CurrentUser.IsLockedOut) Then

        Dim lastLockout As DateTime = CurrentUser.LastLockoutDate
        Dim unlockDate As DateTime = lastLockout.AddMinutes(Membership.PasswordAttemptWindow).AddSeconds(-1)
        Dim unlockCounterMinutes As String = unlockDate.Subtract(DateTime.Now).Minutes + 1
        Dim unlockCounterSeconds As String = unlockDate.Subtract(DateTime.Now).Seconds

        If (unlockCounterMinutes > 1) Then
            LoginUser.FailureText = "Your account has been locked - Please try again in " & unlockCounterMinutes & " Minutes"
        ElseIf (unlockCounterSeconds > 1) Then
            LoginUser.FailureText = "Your account has been locked - Please try again in " & unlockCounterSeconds & " Seconds"
        Else
            LoginUser.FailureText = "Your account is now being unlocked - Please login again"
        End If

    End If

End If

1 个答案:

答案 0 :(得分:0)

使用unlockDate.Subtract(DateTime.Now)获取剩余时间窗口的Timespan个对象。

根据您想要的粒度,TotalMinutesTotalSeconds为正时,请勿让用户进入。