如何通过以下条件登录页面? [英] how to do login page by following conditions?
本文介绍了如何通过以下条件登录页面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有人知道吗?
在登录页面中,我有2个字段1)loginemailid 2)密码....在此
当我在没有ID的情况下都输入这两个字段时,密码不在DB中,则应显示请注册以登录",而当我单独输入密码时,则应显示无效的用户名和密码"...... ........
Can anyone know?
in login page i have 2 fields 1)loginemailid 2)password....in this
when i enter both field by no id,password not in DB it should display "Please Register to login" and when i enter the password alone weong it should display" "Invalid username and password"..................
推荐答案
示例代码:
Sample code :
private void CheckPassword()
{
string emailAddress = this.txtLoginRmailId.Text;
string password = this.txtPassword.Text;
string errorMessage;
bool userExist = Bll.CheckIfUserExist(emailAddress);
if (userExist == false)
{
errorMessage = "Please register to login";
Response.Write(errorMessage);
}
else
{
bool userIsValid = Bll.ValidateUserCredential(emailAddress, password);
if (userIsValid == false)
{
errorMessage = "Invalid username and password";
}
Response.Write(errorMessage);
}
}
(如果您使用登录控件..
if you are using login control..
<asp:Label ID="lblMessage" runat="server" Text="Label"></asp:Label>
<asp:Login ID="ctlLogin" runat="server" OnAuthenticate="OnAuthenticate">
<LayoutTemplate>
<asp:TextBox ID="UserName" runat="server"></asp:TextBox>
<asp:TextBox ID="Password" runat="server" TextMode="Password" ></asp:TextBox>
<asp:Button ID="Button2" ValidationGroup="ctlLogin" CommandName="Login" runat="server" Text="Button" />
</LayoutTemplate>
</asp:Login>
背后的代码...
code behind...
bool Authenticated;
protected void OnAuthenticate(object sender, AuthenticateEventArgs e)
{
Authenticated = false;
Authenticated = UserAuthenticate(ctlLogin.UserName, ctlLogin.Password);
e.Authenticated = Authenticated;
if (Authenticated == true)
{
Response.Redirect("~/home.aspx");
}
}
private bool UserAuthenticate(string UserName, string Password)
{
bool passwordMatch = false;
SqlCommand cmd = new SqlCommand("Select email,Password FROM [User] where Email=@userName", con);
cmd.CommandType = CommandType.Text;
//Usage of Sql parameters also helps avoid SQL Injection attacks.
SqlParameter sqlParam = cmd.Parameters.Add("@userName", SqlDbType.VarChar, 100);
sqlParam.Value = UserName;
try
{
con.Open();
SqlDataReader reader = cmd.ExecuteReader();
reader.Read(); // Advance to the one and only row
// Return output parameters from returned data stream
if (reader.HasRows) // check that reader contain the record or not
{
if (reader != null)
{
string dbid = reader.GetString(0);
string dbpass = reader.GetString(1);
if (dbid == UserName && dbpass == Password)
{
passwordMatch = true;
}
}
}
else
{
lblMessage.Text = "You have not registered yet!";
}
}
catch (Exception ex)
{
throw new Exception("Execption verifying password. " + ex.Message);
//lblMessage.Text = "Login error: " + ex.Message;
}
finally
{
con.Close();
}
return passwordMatch;
}
希望你能有主意.祝你好运.
hope you will get idea. good luck.
这篇关于如何通过以下条件登录页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文