2011-12-17 40 views
1

这里是我的配置部分:混淆有关窗体身份验证在ASP.NET MVC

<authentication mode="Forms"> 
    <forms loginUrl="~/Account/Login" timeout="43200" cookieless="UseCookies" slidingExpiration="true" /> 
</authentication> 

现在在控制器中,我有:

FormsService.SignIn(userName, true); 
var temp = User.Identity.IsAuthenticated; 

的温度值有时会被设置为false,有时真正。当用户是我自己时(在系统中有效)。无论我的aspx页面中有这个值,我都有:

<% if (Context.User.Identity.IsAuthenticated) 
      { %> 

这总是解决为真。那么我在我的控制器中做错了什么?我如何检查用户是否在控制器中进行了身份验证?

非常感谢!

回答

1

验证并设置身份验证Cookie后,您必须重定向。在随后的请求中,用户将被真正认证。

public ActionResult LogOn() 
{ 
    FormsService.SignIn(userName, true); 
    return RedirectToAction("authenticated"); 
} 
+0

谢谢!它做到了。我从来没有猜到。 – Barka

+0

@ user277498,原因如下:当您成功验证用户身份时,将身份验证Cookie添加到“Response”对象。在随后的请求中,这个cookie是由客户端发送的,因此它在'Request'对象中。表单身份验证模块在Request对象中查找cookie,以了解用户是否已通过身份验证。 –