我在login.aspx中放了一个login控件,在login.aspx.cs中写了如下登录代码:    protected void Login1_Authenticate(object sender, AuthenticateEventArgs e)
    {
        bool Authenticated = false;
        Authenticated = SiteLevelCustomAuthenticationMethod(Login1.UserName, Login1.Password);
        e.Authenticated = Authenticated;
        if (Authenticated == true)
        {
            Response.Redirect("MainForm.aspx");
        }
    }
    private bool SiteLevelCustomAuthenticationMethod(string UserName, string Password)
    {
        bool boolReturnValue = false;
        ConnectionStringSettings cs = ConfigurationManager.ConnectionStrings["CS"];
        string strConnection = cs.ConnectionString;
        SqlConnection Connection = new SqlConnection(strConnection);
        String strSQL = "Select * From 用户";
        SqlCommand command = new SqlCommand(strSQL, Connection);
        SqlDataReader Dr;
        Connection.Open();
        Dr = command.ExecuteReader();
        while (Dr.Read())
        {
            if ((UserName == Dr["工号"].ToString()) & (Password == Dr["密码"].ToString()))
            {
                boolReturnValue = true;
                Session["username"] = Dr["姓名"].ToString();
                Session["role"] = Dr["角色"].ToString();
            }
        }
        Connection.Close();
        return boolReturnValue;
web.config中写了如下配置:<?xml version="1.0"?>
<configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">
<appSettings/>
<connectionStrings>
  <add name="CS" connectionString="Data Source=#######;Initial Catalog=DJS;Persist Security Info=True;User ID=sa;Password=******"
   providerName="System.Data.SqlClient" />
 </connectionStrings>
<system.web>
<compilation debug="true">
</compilation>
<authentication mode="Forms">
   <forms name=".FormsAuthCookie" timeout="30" />
  </authentication>
<authorization>
<deny users="?" />
</authorization>
</system.web>
</configuration>为什么登陆的时候,不提示登录失败,但始终停留在登陆页面
如果把“<authorization>
<deny users="?" />
</authorization>
”这段去掉,能够登录,但是其他页面匿名用户也能访问了应该怎么弄啊
??

解决方案 »

  1.   

    写漏了 loginUrl="Login.aspx"问题依旧
      

  2.   

    <authentication mode="Forms">
     <forms name=".FormsAuthCookie" timeout="30" />
    <allow users="?" />
    </authentication>
      

  3.   

    改成allow岂不是允许匿名访问了吗?我觉得问题不在这啊似乎是我登录代码中没有通过验证,还是默认用户为匿名用户,所以才出现登录不进去的情况,不知道我分析的对不对
      

  4.   

    <authentication mode="Forms">
     <forms name=".FormsAuthCookie" timeout="30" />
      <deny users="?" />
    </authentication>
    刚看错了,,
    authentication的顺序。
      

  5.   

    <forms>在<authentication >里面
      

  6.   

    <forms>本来就在<authentication >里面啊如果把<deny users="?" />写到<authentication >里面会报错的
      

  7.   


     if ((UserName == Dr["工号"].ToString()) & (Password == Dr["密码"].ToString()))
    //这应该是与的关系吧  你这是“异或”吧?
                {
                    boolReturnValue = true;
                    Session["username"] = Dr["姓名"].ToString();
                    Session["role"] = Dr["角色"].ToString();
                }
    &注意这个
      

  8.   

    & --------------------- 按位与 
    | --------------------- 按位或 
    ^ --------------------- 按位异或“&”不就是“与”吗?我还是觉得我的登录代码没有把用户变成非匿名用户,因为在Web.config中deny匿名用户就始终停留在Login页面,如果不deny匿名用户,就可以完成正常登录和使用那么"e.Authenticated = true;"这句代码到底能不能通过cookie的验证啊,将当前用户变成非匿名用户啊??
      

  9.   

    我知道了,在"e.Authenticated = true;"后面加一句"FormsAuthentication.RedirectFromLoginPage(Login1.UserName, false);"就OK了
    Web.config配置没有问题,关键还是如我上面分析的那样,"e.Authenticated = true;"并没有完成验证,所以当前用户还是匿名的,只有加上"FormsAuthentication.RedirectFromLoginPage(Login1.UserName, false);"当前用户才会验证通过,成为非匿名用户,今天高手都不在吗?