C# asp.net登录功能
我必须向现场主管实施: 我所做的是: 我拿了三件东西 网站管理员 Home.aspx webform1.aspx 在site master中,我写了以下内容: 在代码隐藏中,我写了以下内容:C# asp.net登录功能,c#,asp.net,login,C#,Asp.net,Login,我必须向现场主管实施: 我所做的是: 我拿了三件东西 网站管理员 Home.aspx webform1.aspx 在site master中,我写了以下内容: 在代码隐藏中,我写了以下内容: public partial class SiteMaster1 : System.Web.UI.MasterPage { protected void Page_Load(object sender, EventArgs e) { if (
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]将登录控件的属性设置为您的指定页面
作为补充说明,您对用户进行身份验证的方法存在严重问题
private bool YourValidationFunction(string UserName, string Password)
{
bool boolReturnValue = false;
string strConnection = "i wrote correct string, cannot write here on stackoverflow";
String SQLQuery = "SELECT count(*) FROM aspnet_Users where Username=@uname AND Password = @pwd";
using(SqlConnection sqlConnection = new SqlConnection(strConnection))
using(SqlCommand command = new SqlCommand(SQLQuery, sqlConnection))
{
sqlConnection.Open();
command.Parameters.AddWithValue("@uname", Username);
command.Parameters.AddWithValue("@pwd", Password);
int result = Convert.ToInt32(command.ExecuteScalar());
boolReturnValue = (result > 0);
}
return boolReturnValue;
}
在重写函数的过程中,我使用了,以确保在使用连接后关闭并破坏连接。此外,为了避免问题,我还引入了一种新的查询方法,并将query命令更改为使用ExecuteScalar方法
连接是一种宝贵的资源,应该在使用后立即释放到操作系统。using语句确保即使在出现异常的情况下,连接也会关闭并在关闭大括号处释放。参数化查询可避免恶意字符串被传递到数据库,并允许框架正确格式化数值中的字符串、日期和小数。当您只需要从数据库返回一个值时,ExecuteScalar非常有用,就像您只需要知道数据库中是否存在配对用户+密码一样。您没有提到严重的问题!非常感谢您的帮助,但是我想在转到其他页面时隐藏asp.net登录控件,并检查用户是否登录,如果您重定向一些密码,加密和解密方法,这将对我有帮助。对不起,我不是asp.net方面的专家。但是,您可以使用此处提供的有关登录控件和ASP.NET成员资格提供程序的一些资料来确保站点安全