Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/266.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Request.IsAuthenticated始终为false_C#_Asp.net Mvc - Fatal编程技术网

C# Request.IsAuthenticated始终为false

C# Request.IsAuthenticated始终为false,c#,asp.net-mvc,C#,Asp.net Mvc,你能告诉我为什么FormsAuthentication.SetAuthCookie(user.Name,false)未导致请求。IsAuthenticated为真 这是我的密码: [HttpPost] public ActionResult LogIn(karcioszki.Models.UserLoginModel user) { if (ModelState.IsValid) { if (IsValid(user.Na

你能告诉我为什么
FormsAuthentication.SetAuthCookie(user.Name,false)未导致
请求。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 (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)
}
@if(Request.IsAuthenticated)
{
@ActionLink(“注销”、“注销”、“用户”)
@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还是其他模式?

您正在检查用户是否经过身份验证。因此,请使用:

if (User.Identity.IsAuthenticated) 

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

必须设置
FormsAuthentication.SetAuthCookie(acct.UserName,true)

验证用户并请检查后,您必须在web.config中设置
authentication mode=“Forms”

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

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

我在我的
MVC4 Web应用程序中遇到了完全相同的问题。此问题的另一个可能原因是
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;
    }
公共异步任务GenerateUserIdentityAsync(UserManager)
{
//注意authenticationType必须与CookieAuthenticationOptions.authenticationType中定义的类型匹配
var userIdentity=wait manager.CreateIdentityAsync(这是DefaultAuthenticationTypes.ApplicationOkie);
//在此处添加自定义用户声明
返回用户身份;
}
确保
DefaultAuthenticationTypes
applicationcokie


干杯,

我正在寻找大约2个小时来解决这个问题。 如果您已经得到了一些指南(如MSDN等)中告诉您的设置,但您仍然存在问题,那么唯一可以解决问题的方法就是添加:

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


在webconfig中。

在Web.config中添加以下代码

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



就我而言,这是Chrome浏览器的一个问题。我刚刚删除了所有存储的Cookie并开始工作。

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

在我的例子中,我遇到了这个问题,它最终导致了一个重定向循环,用户将被重定向到登录,成功登录,然后返回到我们的站点,但由于Request.IsAuthenticated为false,用户被重新定向到登录

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

var cookieOptions = new CookieAuthenticationOptions();
cookieOptions.CookieManager = new SystemWebCookieManager(); 
app.UseCookieAuthentication(cookieOptions);
事实证明,卡塔纳中存在某种缺陷,而另一位CookieManager解决了这个问题

就这么做吧

     <system.web>
    <authentication mode="Forms">
      <forms cookieless="UseCookies" loginUrl="~/User/Login" slidingExpiration="true"> 
   </forms>
    </authentication>
..........
.......
  </system.web>

..........
.......

如果
语句位于该
语句的位置(在什么视图中)?您是否检查了ModelSate.IsValid是否为true?您发送到操作的模型可能存在问题。是否已检查web.config的
身份验证
元素是否一切正常?你能把它贴出来吗?如果它位于布局文件中。模型是有效的,我调试了这个,它通过了,还有用户名是正确的。隐马尔可夫模型。。。我在web.config中没有身份验证元素(两者都有)。缺少表单身份验证部分,即它同时适用于
User.Identity.IsAuthenticated
Request.IsAuthenticated
,这两者之间有什么区别吗?它们的工作原理似乎相同。将身份验证更改为表单是关键。@Kmaczek两者之间没有实际区别。实际上,
Request.IsAuthenticated
的实现除了检查
User
User.Identity
是否为空之外,还检查
User.Identity.IsAuthenticated
。因此,您可以继续使用
请求。我已验证了
。这正是我将项目转移到MVC 5后发生的事情。非常感谢您发布Ger。我在MVC5上也遇到了这个问题。你也可以评论“删除”标签,就是这样。我也有同样的问题,加上上面两行,解决了这个问题。谢谢分享。我完全错过了这个。我删除了,我的问题立即得到了解决。system.WebServer在哪里?欢迎来到,你的问题是什么?这个解决方案对我很有效,我尝试了所有其他的可能性,最后它成功了。谢谢。我的自定义登录例程中完全没有这段代码,所以不要忘记这一点。这“解决”了问题,但现在HttpContext.Current.User.Identity.GetUserId()返回null…指向文档的链接,该文档解释了身份验证必须在哪个部分(system.web)中进行
     <system.web>
    <authentication mode="Forms">
      <forms cookieless="UseCookies" loginUrl="~/User/Login" slidingExpiration="true"> 
   </forms>
    </authentication>
..........
.......
  </system.web>