I'm using OWIN for authentication in ASP.NET MVC 5.
My project works perfectly in localhost with IIS Express. The problem is when I upload the project in a web server.
I log in and the application works fine for a moment. Then, it seems as if the session has expired. The HttpContext.User.Identity.Name is empty.
This is my action filter:
public override void OnActionExecuting(ActionExecutingContext context) { if (string.IsNullOrEmpty(context.HttpContext.User.Identity.Name)) { context.Result = new RedirectResult("authentication"); return; } } and this is my login
public JsonResult Login(LoginModel input) { if (ModelState.IsValid) { if(_AuthenticationLogica.ChecarUsuario(input.User, input.Pass)) { int idUser = _AuthenticationLogica.GetIdUser(input.User); var identity = new ClaimsIdentity(new[] { new Claim(ClaimTypes.Name, input.Usuario), new Claim(ClaimTypes.Sid, idUsuario+"") },DefaultAuthenticationTypes.ApplicationCookie,ClaimTypes.Name, ClaimTypes.Role); foreach (var item in _UsuariosLogica.GetPermissionUser(idUser)) { identity.AddClaim(new Claim(ClaimTypes.Role, item.IdDerecho + "")); } var claimsPrincipal = new ClaimsPrincipal(identity); // Set current principal Thread.CurrentPrincipal = claimsPrincipal; // if you want roles, just add as many as you want here (for loop maybe?) identity.AddClaim(new Claim(ClaimTypes.Role, "guest")); // tell OWIN the identity provider, optional // identity.AddClaim(new Claim(IdentityProvider, "Simplest Auth")); int id = _AuthenticationLogica.ObtenerIdUsuario("jcsoto"); Authentication.SignIn(new AuthenticationProperties { IsPersistent = true }, identity); FormsAuthentication.SetAuthCookie(input.Usuario, true); return Json(new { Resultado = 0, Mensaje = "Ready", IdUser = idUser }); } } return Json(new { Resultado = 1, Mensaje = "User or pass wrong" }); }