问题描述
我有一个网站,用户登录该网站的登录次数最多为5次。如果用户已超过特定次数的尝试,则用户将被锁定。现在解锁用户的默认时间是24小时。任务是我需要将解锁用户的时间减少到20分钟。任何人都可以帮我完成这项任务。以下是用户超过最大尝试次数时锁定用户的代码。
I have a website where a user has 5 maximum login attempts to login into the website. If the user has crossed the particular number of attempts then the user will be locked. Now the default time to unlock the user is 24 hours . The task is that I need reduce the time to unlock the user to 20 minutes. Can anyone help me to do this task. The below is the code to lock the user if the user the crossed the maximum number of attempts.
private int _maxLoginAttempts = 5;
protected void btnLogin_Click(object sender, EventArgs e)
{
bool loginOK = false;
bool approved = false;
bool maxAttempsReached = false;
UserDetailService usersService = new UserDetailService();
ContactService contactsService = new ContactService();
UserDetail currentUser = null;
//Get user by Email
Contact currentContact = contactsService.GetByEmailAddress(uxUserName.Text);
if (ApplicationCacheControl.UserLoginAttempts().ContainsKey(uxUserName.Text))
{
ApplicationCacheControl.UserLoginAttempts()[uxUserName.Text] += 1;
if (ApplicationCacheControl.UserLoginAttempts()[uxUserName.Text] >= _maxLoginAttempts)
{
maxAttempsReached = true;
try
{
//Audit lockout
//AuditLogManager.RecordUserAction(ActivityAuditLog.ApplicationActivityTypeEnum.PublicUserLockedOut, -1, null,
// "User", "Max. number of logins attempted for " + uxUserID.Text, true);
}
catch { }
}
}
else
{
ApplicationCacheControl.UserLoginAttempts().Add(uxUserName.Text, 1);
}
if (maxAttempsReached)
{
phMaxLogins.Visible = true;
phLoginControls.Visible = false;
if (OnLoginFailed != null)
{
EventArgs newArgs = new EventArgs();
OnLoginFailed(this, newArgs);
}
}
else if (currentContact != null)
{
if (currentContact.UserDetails != null)
{
currentUser = currentContact.UserDetails.FirstOrDefault();
if (currentUser != null)
{
CDS.Framework.Library cdsLibrary = new CDS.Framework.Library(Common.ApplicationName);
if (currentUser.Password == cdsLibrary.EncryptString(uxPassword.Text, true))
{
loginOK = true;
if (ApplicationCacheControl.UserLoginAttempts().ContainsKey(uxUserName.Text))
{
ApplicationCacheControl.UserLoginAttempts()[uxUserName.Text] = 0;
}
if (currentUser.UserStatusTypeID == 2)
{
approved = true;
currentUser.LastLoggedIn = DateTime.Now;
usersService.Save(currentUser);
}
}
}
}
}
推荐答案
if (OnLoginFailed != null)
{
EventArgs newArgs = new EventArgs();
OnLoginFailed(this, newArgs);
}
代码中的其他地方可能是事件的声明,它可能如下所示:
Somewhere else in your code might be the declaration of the event, it might look like this:
public event EventHandler OnLoginFailed;
在其他地方(可能在项目/解决方案的其他地方),可能会有一个方法来处理这个事件(订阅它),以及一个连接订阅方法到事件...也许这样的事情:
处理方法:
Somewhere else (maybe elsewhere in the project/solution), there will probably be a method to handle this event (subscribe to it), and a wire-up to subscribe the method to the event... Maybe something like this:
handling method:
private void LockUser(object sender, EventArgs e)
{
// This code runs to lock the user for a period of time...
}
subscription / wireup
subscription/wireup
someObject.OnLoginFailed += new EventHandler(LockUser);
您发布的代码不会锁定用户 - 它会引发一个事件来告诉别人锁定用户。您的任务是找到订阅方法(如果它在您的代码库中!)并在那里进行适当的修改。祝你好运!
另请查看:
这篇关于减少解锁用户的时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!