登录控件不会将用户重定向到其他页面

时间:2012-06-22 13:00:46

标签: asp.net

我正在开发VS2010。这是我的问题:我有一个表单,用于验证用户,然后将它们重定向到另一个页面。重定向不起作用如果我写用户名和密码系统说什么都没有,两个文本框是空的:

这是我登录控件的一部分

<asp:Login ID="LoginUser" runat="server" EnableViewState="false" 
        RenderOuterTable="false" DestinationPageUrl="~/verwaltung.aspx" MembershipProvider="AspNetSqlMembershipProvider">

并且在我的web.config中我有:

<membership>
      <providers>
        <clear/>
        <add name="AspNetSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider" connectionStringName="ApplicationServices" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10" applicationName="/"/>
      </providers>
    </membership>

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

你说你的代码隐藏是空的,实际上当你使用登录控制时,你需要写这样的登录处理程序。

protected void Login1_Authenticate1(object sender, AuthenticateEventArgs e)
        {
            SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["Online"].ConnectionString);
            SqlCommand cmd = new SqlCommand("SELECT FirstName, LastName FROM Account WHERE Username=@Username AND Password=@Password", conn);
            SqlDataReader rdr = null;
            SqlParameter Username = new SqlParameter();
            Username.SqlDbType = SqlDbType.VarChar;
            Username.ParameterName = "Username";
            Username.Value = Login1.UserName.ToString().ToLower().Trim();

            SqlParameter Password = new SqlParameter();
            Password.SqlDbType = SqlDbType.VarChar;
            Password.ParameterName = "Password";
            Password.Value = Login1.Password;

            cmd.Parameters.Add(Username);
            cmd.Parameters.Add(Password);

            try
            {
                conn.Open();
                rdr = cmd.ExecuteReader();
                while (rdr.Read())
                {
                    e.Authenticated = true;//User was found in the database
                    Session["Username"] = Login1.UserName.ToString();
                    Session["Name"] = rdr[0].ToString() + " " + (rdr[1].ToString() == null ? "" : rdr[1]);
                }
            }
            finally
            {
            if (conn != null)
                conn.Close();
            if (rdr != null)
                rdr.Close();
            if (e.Authenticated)
                Response.Redirect("profile.aspx");//Do whatever needs to be done when user gets authenticated
            }
        }