Request.IsAuthenticated总是错误的

时间:2013-10-23 08:44:33

标签: c# asp.net-mvc

你能告诉我为什么FormsAuthentication.SetAuthCookie(user.Name, false);没有导致Request.IsAuthenticated成为真的吗?

这是我的代码:

[HttpPost]
    public ActionResult LogIn(karcioszki.Models.UserLoginModel  user)
    {
        if (ModelState.IsValid)
        {
            if (IsValid(user.Name, user.Password))
            {
                FormsAuthentication.SetAuthCookie(user.Name, false);
                return RedirectToAction("Index", "Home");
            }
            else
            {
                ModelState.AddModelError("", "Login or password is incorrect");
            }
        }

        return View(user);
    }

和if语句:

@if (Request.IsAuthenticated)
{
    <a href="@Href("~/")" class="active">Home</a>
    <a href="@Href("~/Cards")">Cards</a>
    @Html.ActionLink("Log out", "Logout", "User")
    @Html.Encode(User.Identity.Name)
}

另外,请告诉我,如何让它发挥作用?

编辑: 我在web.config(两者)中添加了身份验证,但它仍然无效。

<system.web>
<httpRuntime targetFramework="4.5" />
<compilation debug="true" targetFramework="4.5" />
<authentication mode="Windows"/>
<pages>
  <namespaces>
    <add namespace="System.Web.Helpers" />
    <add namespace="System.Web.Mvc" />
    <add namespace="System.Web.Mvc.Ajax" />
    <add namespace="System.Web.Mvc.Html" />
    <add namespace="System.Web.Routing" />
    <add namespace="System.Web.WebPages" />
    <add namespace="System.Web.Optimization" /> 
  </namespaces>
</pages>

我应该使用Windows还是其他模式?

10 个答案:

答案 0 :(得分:47)

我在MVC5项目中遇到了同样的问题。 解决方案是将以下行添加到system.webServer

中的modules部分
<remove name="FormsAuthentication" />
<add name="FormsAuthentication" type="System.Web.Security.FormsAuthenticationModule" />

答案 1 :(得分:16)

你必须设置FormsAuthentication.SetAuthCookie(acct.UserName, true); 验证用户后,请检查您是否必须在web.config中设置authentication mode="Forms"

答案 2 :(得分:8)

在Web.config中添加以下代码

<authentication mode="Forms">
  <forms loginUrl="~/_Login/Login" timeout="30" />
</authentication>

  <modules>
  <remove name="FormsAuthentication" />
  <add name="FormsAuthentication" type="System.Web.Security.FormsAuthenticationModule" />
</modules>

答案 3 :(得分:2)

您正在检查用户是否已通过身份验证。所以使用:

if (User.Identity.IsAuthenticated) 

这将解决问题,并且如上所述将web.config中的身份验证元素设置为Forms而不是Windows。

答案 4 :(得分:1)

我在MVC4 Web Application中遇到了完全相同的问题。此问题的另一个可能原因是IdentityModels.cs文件

中的方法
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
    {
                            // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
                            var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
                            // Add custom user claims here
                   return userIdentity;
    }

确保DefaultAuthenticationTypesApplicationCookie

干杯,

答案 5 :(得分:1)

我现在正在寻找约2个小时来解决问题。 如果你已经有一些设置就像你被告知在几个指南(如MSDN等),你仍然遇到问题,唯一能解决它的是添加:

<system.webServer>
  <modules>
     <remove name="FormsAuthentication" />
     <add name="FormsAuthentication" type="System.Web.Security.FormsAuthenticationModule" />
  </modules>
<system.webServer>

在webconfig中。

答案 6 :(得分:1)

就我而言,这是Chrome浏览器中的问题。我刚刚删除了所有已存储的cookie并可以正常工作。

答案 7 :(得分:0)

我通过删除浏览器中的缓存和cookie来解决它。

答案 8 :(得分:0)

在我的情况下,我遇到了这个问题,该问题最终出现在重定向循环中,在该循环中,用户将被重定向到登录名,成功登录后又回到我们的站点,但是由于Request.IsAuthenticated为false,因此用户被重定向到了登录agan。

在我们的案例中,这是通过使用另一个CookieManager

解决的
var cookieOptions = new CookieAuthenticationOptions();
cookieOptions.CookieManager = new SystemWebCookieManager(); 
app.UseCookieAuthentication(cookieOptions);

事实证明,在Katana中存在其他Bug管理器可以解决的某种错误,以防万一,这可以解决问题。

https://github.com/aspnet/AspNetKatana/wiki/System.Web-response-cookie-integration-issues

答案 9 :(得分:0)

只需这样做

add_subdirectory(${CMAKE_SOURCE_DIR}/../DirectXT ${CMAKE_BINARY_DIR}/bin/CMake/DirectXTK)

target_link_libraries(${PROJECT_NAME} DirectXTK)
target_include_directories(${PROJECT_NAME} PRIVATE ../DirectXTK/Inc)