2010-06-03 202 views
3

我在登录过程中遇到问题。ASP.NET身份验证问题

有些客户抱怨说他们无法登录。我可以在我们的日志中看到他们的登录成功,并且他们从登录页面重定向到成员区域。但是,不知何故登录未被检测到,并且他们被弹回到登录页面。

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

这是怎么了,我实现了登录过程(simplyfied):通过在Page_Load中功能

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"); 


} 

在成员页面我检查认证:

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

也许我使用FormsAuthentication完全错误?

我已经问过这个,但还没有能够弄清楚这一点,我会很感激任何帮助。

从我的web.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> 
+0

你想念登录页面并拒绝所有用户(请参阅我的回答)。 – Michel 2010-06-03 09:03:01

+0

请注意,当前登录过程适用于大多数客户(99%)。 – 2010-06-03 09:19:17

回答

1

单独SignOut的()调用和SetAuthCookie()在不同的方法。当登录页面首次加载时,您可以拨打FormsAuthentication.SignOut(); - 只需在登录页面上远离调用SignOut()即可。并在认证成功后致电 FormsAuthentication.SetAuthCookie(clientId.ToString(), true);

+0

试过但它没有任何影响:( – 2010-06-03 09:31:57

2

公共静态无效IsAuthenticated(){ 如果 (HttpContext.Current.User.Identity.IsAuthenticated!) { HttpContext.Current.Response.Redirect( “〜/的Login.aspx”,真正的); } }

当您使用表单身份验证时不是必需的。

当您指定在web.config中的窗体身份验证(其中还指定登录页面)

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

,你拒绝所有非athenticated用户访问

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

你不您不必亲自检查用户的身份验证,框架就会照顾到这一点。

我会放在FormsAuthentication.SignOut();代码后面的“注销”链接

+0

由于我有点奇怪的要求,我不能使用传统的ASP.NET身份验证...我坚持使用SetCookieAuth和Identity.IsAuthenticated。 – 2010-06-03 10:05:22

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"/>

+0

没有我的天堂请注意,问题并未影响到所有人 – 2010-06-03 10:01:55

+0

您如何执行登录? – 2010-06-03 10:03:38