Asp Net MVC 5 Идентификация пользователя IsAuthenticated возвращает false - PullRequest
0 голосов
/ 10 апреля 2019

Я использую аутентификацию по формам и создаю заявку

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Login([Bind(Include = "Username,Password,RememberMe")]LoginViewModel model)
{
   // other code here ....

    var ticket = new FormsAuthenticationTicket(
                        1,
                        user.Id.ToString(),
                        DateTime.Now,
                        DateTime.Now.AddDays(5),
                        model.RememberMe,
                        user.Roles.Select(c => c.Nome).FirstOrDefault(),
                        FormsAuthentication.FormsCookiePath
                        );

                    // Encrypt the ticket.
                    string encryptedTicket = FormsAuthentication.Encrypt(ticket);
                    // Create the cookie.
                    HttpCookie authenticationCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket); // Name of auth cookie (it's the name specified in web.config) // Hashed ticket
                    authenticationCookie.Expires = DateTime.Now.AddDays(7);
                    // Add the cookie to the list for outbound response
                    Response.Cookies.Add(authenticationCookie);

                    return RedirectToAction("Index");

    // more code here...
}

Это в контроллере входа для аутентификации пользователя.

Теперь после этого я пытаюсь вручную перенаправить на контроллер входа

 // GET: /Account/Login
        [AllowAnonymous]
        public ActionResult Login(string returnUrl)
        {
            if (User.Identity.IsAuthenticated)
            {
                return RedirectToAction("Index");
            }

            ViewBag.ReturnUrl = returnUrl ?? Url.Action("Index");

            return View();
        }

, а User.Identity.IsAuthenticated возвращает false ??Почему это?Я вижу файл cookie, созданный в браузере

    <authentication mode="Forms">
  <forms name=".ADAuthCookie" loginUrl="~/Account/Login" defaultUrl="~/" timeout="2880" slidingExpiration="true" protection="All" />
</authentication>

РЕДАКТИРОВАТЬ

Просто сказать, что если я использую вместо этого, он возвращает true.Я не понимаю, почему он не работает с Ticket

FormsAuthentication.SetAuthCookie(user.Id.ToString(), model.RememberMe);
...