4

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" }); } 
10
  • if session expires, then the user is no longer authenticated. is that not normal behaviour? Commented Oct 25, 2016 at 1:56
  • is your Application pool recycling for some reason? Commented Oct 25, 2016 at 2:43
  • Default Session Timeout is 20 minutes. If you want longer than that, you can configure in web.config. Commented Oct 25, 2016 at 3:31
  • @muratgu my timeout is 60 minutes but the session expires sometimes in one minute more less. Commented Oct 25, 2016 at 15:21
  • im not using pool recycling, i dont know what is that @GabrielGM Commented Oct 25, 2016 at 15:22

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.