0

我必须对站点主控实施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]

4

1 回答 1

0

将 Login 控件的DestinationPageUrl属性设置为您想要的页面

作为旁注,您对用户进行身份验证的方法存在严重问题

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;
}

在你的函数的这个重写中,我使用了using 语句来确保在使用后关闭并销毁连接。我还引入了一个参数化查询来避免Sql 注入问题,并且我将查询命令更改为使用 ExecuteScalar 方法

连接是宝贵的资源,使用后应立即释放到操作系统。using 语句确保即使在出现异常的情况下,连接也会关闭并放置在右大括号处。参数化查询避免了将恶意字符串传递到数据库,并允许框架正确格式化数字值中的字符串、日期和小数。当您只需要从数据库返回一个值时,ExecuteScalar 非常有用,例如您只需要知道数据库中是否存在用户+密码对。

于 2013-09-08T11:55:43.947 回答