Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Request.IsAuthenticated is always false

Tags:

c#

asp.net-mvc

Can you tell me why FormsAuthentication.SetAuthCookie(user.Name, false); is not causing Request.IsAuthenticated to be true?

Here is my code:

[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);     } 

and if statement:

@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) } 

Also, please tell me, how to make it work?

EDIT: I added authentication in web.config(both) but it still isn't working.

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

Should I use Windows or other mode?

like image 258
Kmaczek Avatar asked Oct 23 '13 08:10

Kmaczek


People also ask

Why is user identity IsAuthenticated false?

isauthenticated is False when a user is already logged in. However, there is a very wide range of answers, from trivial Web.

How does request IsAuthenticated work?

Identity with a new IIdentity object that will return true from its IsAuthenticated property. Request. IsAuthenticated will then return true . In the case of Forms authentication, the forms authentication module uses the encrypted authentication ticket contained in the authentication cookie to authenticate the user.


1 Answers

I had the same problem in an MVC5 project. The solution was to add the following lines to the modules section in the system.webServer

<remove name="FormsAuthentication" /> <add name="FormsAuthentication" type="System.Web.Security.FormsAuthenticationModule" /> 
like image 142
Ger Groot Avatar answered Sep 21 '22 11:09

Ger Groot