ASP登录控件停止onlogingin方法的执行



作为一个新手,针对以下场景寻求帮助。

我使用ASP LoginView控件,并有相应的事件处理程序。对于每次错误的登录尝试,我将存储尝试计数和用户详细信息,如果达到最大计数,我将禁用该用户。当用户下次尝试登录时,我检查用户是否启用/禁用,如果禁用则需要停止执行并阻止登录动作。

我试图使用

返回;

在onlogingin方法中,但这不会停止执行,仍然继续登录操作。

ASPX Code:
<asp:loginView id="loginView" runat="server">
<AnonymousTemplate>
<asp:login id="login1" runat="server"
OnLoggedIn="Login1_onLoggedIn"
OnLoggingIn="Login1_onLoggingIn"
OnLoginError="Login1_onLoginError"
<LayoutTemplate>
<asp:Textbox ID="username" runat="server" />
<asp:Textbox ID="password" runat="server" />
<asp:Button ID = "LoginButton" runat="server" Command="Login" />
</LayoutTemplate>
</asp:login>
</AnonymousTemplate>
</asp:loginView>

c代码:

protected void Page_load(object sender,EventArgs e)
{
}
protected void Login1_onLoggingIn(object sender,EventArgs e)
{
// This method is called when user clicked on Login Button.
// Checking if the user is enabled or disabled. 
// If user disabled - show error message and need to stop the execution here and not go further
// Tried return; - but execution still continue.
}
protected void Login1_onLoggedIn(object sender,EventArgs e)
{
// This method is called when user is logged.
}
protected void Login1_onLoginError(object sender,EventArgs e)
{
// This method is called on incorrect login attempt and store the count and user detail in DB
// If max incorrect login attempts reached, user marked as disabled for specific time limit.
}

如果有人可以指导我如何检查用户启用/禁用,如果用户禁用,我如何停止方法执行并阻止流程继续。

Thanks in advance

要取消OnLoggingIn方法的执行,您可以使用CancelEventArgs.Cancel属性:

protected void Login1_onLoggingIn(object sender,System.Web.UI.WebControls.LoginCancelEventArgs e)
{
var userDisabled= CheckIfUserIsDisabled(); //Implement this
if (isUserDisabled)
{
// Show an error message.
login1.FailureText = "Your account is disabled";
e.Cancel = true;// Here is the point
}
}

最新更新