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?
isauthenticated is False when a user is already logged in. However, there is a very wide range of answers, from trivial Web.
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.
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" />
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With