Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 登录c的基本计数器#_C#_.net - Fatal编程技术网

C# 登录c的基本计数器#

C# 登录c的基本计数器#,c#,.net,C#,.net,我一直用一个登录计数器来记录有人尝试登录的次数。(尝试失败) 我是来自使用C#和.NET的Cognex的VisionPro Designer。 当在从第一次失败尝试开始的时间范围内(比如5分钟)达到最大尝试次数时,密码框将被阻止 我是编程初学者,只是自学 目前的设置只是没有,程序中内置了一个用户数据库 用户名的文本框和密码的密码框(显然)。 我将其设置为,当passwordbox获得更改的值时,它会将凭据与用户数据库匹配,如果是“true”,则会进入“高级选项”页面。我不希望任何不受信任的机器操

我一直用一个登录计数器来记录有人尝试登录的次数。(尝试失败)

我是来自使用C#和.NET的Cognex的VisionPro Designer。 当在从第一次失败尝试开始的时间范围内(比如5分钟)达到最大尝试次数时,密码框将被阻止

我是编程初学者,只是自学

目前的设置只是没有,程序中内置了一个用户数据库

用户名的文本框和密码的密码框(显然)。 我将其设置为,当passwordbox获得更改的值时,它会将凭据与用户数据库匹配,如果是“true”,则会进入“高级选项”页面。我不希望任何不受信任的机器操作员进入高级设置页面

该程序使用“值标签”,您可以在程序中的任何位置获取值或更改值,为LoginAttests“tag_LoginCount”设置标签

这是密码箱代码:

if ($System.Users.Login($Tag_InputUsername, $Tag_InputPassword))  
{
    $Pages.MainPage.PasswordBox.Password = ""; //Resets the passwordbox password at login

    $HMI.ShowPage("LidSettings"); //Opens the Lidsettings page

    $Pages.LidSettings.Slider2.Value = 0; //Disables the advanced settings option
}
else
{  
    $Pages.MainPage.PasswordBox.Password = ""; //resets password
}

根据你的描述,很难说你需要什么。我将假设您提供的代码片段位于处理某些用户输入的事件处理程序中。我将把事件处理程序放在一个类中。为了能够在多次调用处理程序之间保留计数器的值,您需要将计数器的值存储在处理程序之外的某个位置。一种方法是使用类级变量

public class C
{
    public C()
    {
        _counter = 0; // initialize the counter when creating a class instance
    {

    private int _counter;

    private void Handler()
    {
        if (System.Users.Login(Tag_InputUsername, Tag_InputPassword))  
        {
            Pages.MainPage.PasswordBox.Password = ""; //Resets the passwordbox password at login

            HMI.ShowPage("LidSettings"); //Opens the Lidsettings page

            Pages.LidSettings.Slider2.Value = 0; //Disables the advanced settings option

            _counter++; // increment the counter field
        }
        else
        {  
            Pages.MainPage.PasswordBox.Password = ""; //resets password
        }
    }
}

我认为这并不是您想要的,因为有效和无效登录尝试之间没有区别,但您应该能够找出如何调整示例。

看起来您已经定义了一个名为“tag\u LoginCount”的标记。我想这是一个整数。您可以在“标记管理器”中检查现有标记并添加新标记

我想再加几个标签:

  • 失败登录(整数)
  • TimeOfFirstLogin(日期时间)
  • UserLockedOut(布尔值)
  • UserLockoutStart(日期时间)
要在脚本中使用标记,只需在标记名前面加上美元符号(例如,$FailedLogins),或将它们从工具箱拖放到脚本中

下面的脚本应该实现与您想要的接近的效果:

const int USER_LOCKOUT_PERIOD = 10;
const int MAX_LOGIN_ATTEMPTS = 8;
const int LOGIN_TIMEFRAME = 5;

// Check if user is locked out
int minutesUserLockedOut = (int)DateTime.Now.Subtract($UserLockoutStart).TotalMinutes;

if ($UserLockedOut && (minutesUserLockedOut > USER_LOCKOUT_PERIOD))
{
    $UserLockedOut = false;
}

if ($UserLockedOut)
{
    int userLockoutMinutesRemaining = (int)(USER_LOCKOUT_PERIOD - minutesUserLockedOut);
    $HMI.ShowMessage("Too many failed login attempts.  System locked for " + userLockoutMinutesRemaining + " minutes.");
    $Pages.MainPage.PasswordBox.Password = ""; //resets password
    return;
}

// Reset number of failed logins if first failed login more than LOGIN_TIMEFRAME minutes ago
TimeSpan minutesSinceFirstBadLogin = (int)DateTime.Now.Subtract($TimeOfFirstBadLogin).TotalMinutes;
if (minutesSinceFirstBadLogin > LOGIN_TIMEFRAME)
{
    $FailedLogins = 0;
}

if ($System.Users.Login($Tag_InputUsername, $Tag_InputPassword))
{
    // Successful login!  Reset number of bad logins
    $FailedLogins = 0;

    $HMI.ShowPage("LidSettings"); //Opens the Lidsettings page
    $Pages.LidSettings.Slider2.Value = 0; //Disables the advanced settings option
}
else
{
    // If this is the first failed login, record the time
    if ($FailedLogins == 0)
    {
        $TimeOfFirstBadLogin = DateTime.Now;
    }

    // Increment the number of failed logins
    $FailedLogins = $FailedLogins + 1;

    // Lock the user out if the number of failed logins exceeds maximum allowed
    if ($FailedLogins > MAX_LOGIN_ATTEMPTS)
    {
        $UserLockedOut = true;
        $UserLockoutStart = DateTime.Now;
    }
}

$Pages.MainPage.PasswordBox.Password = ""; //resets password

那些类似php的变量是什么?O。o@EmpereurAiman谢谢,我没注意到。不确定他们在原始帖子中做了什么。@emperueraiman我使用的程序是多种编程语言加上自己的“标记管理器”的组合。我正在构建一个程序,该程序可以直观地检查我们生产的零件的质量,每行3个摄像头。我已经完成了程序,所以我正在设置用户界面,操作员可以在机器的触摸屏上更改一些选定的设置。要做到这一点,我需要一些基本的编程。@Kapol我只需要一种方法来更改“Tag_LoginCount”的值。“标记”本身可以保存该值,直到您使用命令重置它为止。不需要组件、类等。您应该将失败的尝试存储在数据库中。每次凭据评估为false时,您都应该使用计数更新一列。