ASP.NET身份验证问题

时间:2010-06-03 08:32:47

标签: c# asp.net

我的登录程序有问题。

有些客户抱怨他们无法登录。我可以在日志中看到他们的登录成功,并且他们被从登录页面重定向到成员区域。但是,某种程度上没有检测到登录,它们被退回到登录页面。

我已经要求客户检查是否支持cookie(http://www.html-kit.com/tools/cookietester/),但即使此测试返回true,问题仍然存在。

这就是我实现登录程序(简称)的方法:

protected void Login(string email, string password)
{

FormsAuthentication.SignOut();


Guid clientId = /* Validate login by checking email and password, if fails display error otherwise get client id */


FormsAuthentication.SetAuthCookie(clientId.ToString(), true);

HttpContext.Current.Response.Redirect("~/Members.aspx");


}

在成员页面上,我通过Page_Load函数检查身份验证:

public static void IsAuthenticated()
{
 if (!HttpContext.Current.User.Identity.IsAuthenticated)
 {
         HttpContext.Current.Response.Redirect("~/Login.aspx", true);
 }
}

也许我完全错误地使用FormsAuthentication?

我以前问过这个问题,但仍然无法解决这个问题,我会感激任何帮助。

来自我的网站.Config:

<system.web>
    <compilation debug="false">
      <assemblies>
       ...
      </assemblies>
    </compilation>
    <authentication mode="Forms"/>
    <sessionState mode="InProc" cookieless="false" timeout="180"/>
    <customErrors mode="On"/>
    <httpHandlers>
    ...
    </httpHandlers>
    <httpModules>
    ...
    </httpModules>   </system.web>

3 个答案:

答案 0 :(得分:2)

public static void IsAuthenticated() {  if(!HttpContext.Current.User.Identity.IsAuthenticated)  {          HttpContext.Current.Response.Redirect(“〜/ Login.aspx”,true);  } }

使用表单身份验证时不需要

在web.config中指定表单身份验证(在其中还指定了登录页面)

<authentication mode="Forms">
  <forms loginUrl="/Authorization/Login" timeout="60" />
</authentication>

并且您拒绝所有未经身份验证的用户访问

<authorization>
          <deny users="?" />
      </authorization>

您不必自己检查用户的身份验证,框架会处理这个问题。

我会将FormsAuthentication.SignOut();代码放在“退出”链接

之后

答案 1 :(得分:1)

以不同方法分隔SignOut()和SetAuthCookie()的调用。您可以在登录页面第一次加载时调用FormsAuthentication.SignOut(); - 只需在登录页面上调用SignOut()即可。并打电话 验证成功后FormsAuthentication.SetAuthCookie(clientId.ToString(), true);

答案 2 :(得分:1)

通常情况下,您会将FormsAuthentication.Authenticate与某些会员提供商一起使用,但这应该有效,而且实际上是在我的机器中。

您是否从已注册的HTTP模块中删除FormsAuthentication?通常,这是在机器范围的web.config中:

<configuration>
  <system.web>
    <httpModules>
      <add name="FormsAuthentication" type="System.Web.Security.FormsAuthenticationModule"  />
    </httpModules>
  </system.web>
</configuration>

如果你将<clear />放在你自己的web.config的同一部分中,你就可以有效地删除该模块。

我测试的Web.config很干净,只配置了<authentication mode="Forms"/>