我必须对站点主控实施asp.net 登录控制:
我所做的是:
我拿了3样东西
- 站长
- 主页.aspx
- webform1.aspx
在站点管理员中,我写了以下内容:
<form id="form1" runat="server">
<div>
<asp:Login ID="Login1" runat="server" OnAuthenticate="Login1_Authenticate" OnLoginError="Login1_LoginError">
</asp:Login>
</div>
<div>
<asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
</asp:ContentPlaceHolder>
</div>
</form>
在后面的代码中,我写了以下内容:
public partial class SiteMaster1 : System.Web.UI.MasterPage
{
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
ViewState["LoginErrors"] = 0;
}
#region Login Functionality
/// <summary>
/// will validation if the username and password while click on login button from asp.net login button
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Login1_Authenticate(object sender, AuthenticateEventArgs e)
{
if (YourValidationFunction(Login1.UserName, Login1.Password))
{
//e.Authenticated = true;
Login1.Visible = false;
//MessageLabel.Text = "Successfully Logged In";
}
else
{
e.Authenticated = false;
}
}
/// <summary>
/// Will show the error
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Login1_LoginError(object sender, EventArgs e)
{
if (ViewState["LoginErrors"] == null)
ViewState["LoginErrors"] = 0;
int ErrorCount = (int)ViewState["LoginErrors"] + 1;
ViewState["LoginErrors"] = ErrorCount;
if ((ErrorCount > 3) && (Login1.PasswordRecoveryUrl != string.Empty))
Response.Redirect(Login1.PasswordRecoveryUrl);
}
/// <summary>
/// function to check the username and password to server
/// </summary>
/// <param name="UserName"></param>
/// <param name="Password"></param>
/// <returns></returns>
private bool YourValidationFunction(string UserName, string Password)
{
bool boolReturnValue = false;
string strConnection = "i wrote correct string, cannot write here on stackoverflow";
SqlConnection sqlConnection = new SqlConnection(strConnection);
String SQLQuery = "SELECT UserName, Password FROM aspnet_Users";
SqlCommand command = new SqlCommand(SQLQuery, sqlConnection);
SqlDataReader Dr;
sqlConnection.Open();
Dr = command.ExecuteReader();
while (Dr.Read())
{
if ((UserName == Dr["UserName"].ToString()) & (Password == Dr["Password"].ToString()))
{
boolReturnValue = true;
}
Dr.Close();
return boolReturnValue;
}
return boolReturnValue;
}
#endregion
}
我的问题是:我想管理如何显示
Asp.Net 登录控制:
比方说,当我转到其他页面 webform1.aspx 时,我仍然能够看到 asp.net 登录控件(即使我隐藏了该控件)。而不是这个,我想显示welcome [Username]