表单身份验证问题

时间:2013-10-10 17:05:25

标签: asp.net

尝试使用表单身份验证仅在通过登录页面登录后才允许访问页面。当我登录并尝试重定向时,它只是将我重定向回登录页面。

网络登录控制

protected void WebGenLogin_Authenticate(object sender, AuthenticateEventArgs e)
    {
        //Verify user against active directory
        if (new AD().validate(WebGenLogin.UserName, WebGenLogin.Password))
        {
            Session["UserAuthentication"] = WebGenLogin.UserName;
            Session.Timeout = 30;
            FormsAuthentication.RedirectFromLoginPage(WebGenLogin.UserName, WebGenLogin.RememberMeSet);
            Response.Redirect("~/WebGen/Gen/Create.aspx");
        }
        else
        {
            Session["UserAuthentication"] = "";
            Response.Redirect("http://thekickback.com/rickroll/rickroll.php");
        }
    }

Create.aspx Web.config

<authentication mode="Forms">
    <forms defaultUrl="~/WebGen/Gen/Create.aspx" loginUrl="../Login.aspx" slidingExpiration="true" timeout="30" />
  </authentication>

4 个答案:

答案 0 :(得分:0)

你可以试试这个:

if (new AD().validate(WebGenLogin.UserName, WebGenLogin.Password))
{
        Session["UserAuthentication"] = WebGenLogin.UserName;
        Session.Timeout = 30;
        FormsAuthentication.SetAuthCookie(WebGenLogin.UserName, false);
        FormsAuthentication.RedirectFromLoginPage(WebGenLogin.UserName, WebGenLogin.RememberMeSet);

        ***SNIP***

答案 1 :(得分:0)

我不知道AD()调用的对象类型,但您可能没有使用默认的ASP.NET成员资格功能。我记得,如果返回true,则成员类的ValidateUser方法会产生实际记录用户的副作用。

在对用户进行身份验证之后,您可能需要将HttpContext.User设置为表示用户的新IPrincipal,然后在重定向之前调用FormsAuthentication.SetAuthCookie()。

答案 2 :(得分:0)

好的我明白了。它与我的代码无关。但我删除了在会话中存储用户名。

我要做的是将IIS上的根站点更改为应用程序。 使用Login.aspx将身份验证模式行放在根目录中 Create.aspx在另一个文件夹中。我从它的Web.config中删除了身份验证模式,只是放入了deny部分,一切正常。

答案 3 :(得分:0)

代码实际上有效。发现它是IIS的一个问题。需要将整个文件夹结构转换为应用程序而不是其他部分。