C# HttpContext.User.Identity.Name有时为空
我正在ASP.NETMVC5中使用OWIN进行身份验证 我的项目在带有IIS Express的localhost中工作得非常好。问题是当我将项目上传到web服务器时 我登录后,应用程序可以正常运行一会儿。然后,会话似乎已过期。HttpContext.User.Identity.Name为空 这是我的动作过滤器:C# HttpContext.User.Identity.Name有时为空,c#,asp.net-mvc,owin,C#,Asp.net Mvc,Owin,我正在ASP.NETMVC5中使用OWIN进行身份验证 我的项目在带有IIS Express的localhost中工作得非常好。问题是当我将项目上传到web服务器时 我登录后,应用程序可以正常运行一会儿。然后,会话似乎已过期。HttpContext.User.Identity.Name为空 这是我的动作过滤器: public override void OnActionExecuting(ActionExecutingContext context) { if (
public override void OnActionExecuting(ActionExecutingContext context)
{
if (string.IsNullOrEmpty(context.HttpContext.User.Identity.Name))
{
context.Result = new RedirectResult("authentication");
return;
}
}
这是我的登录名
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" });
}
如果会话过期,则不再对用户进行身份验证。这不是正常的行为吗?您的应用程序池是否因为某种原因而循环使用?默认会话超时为20分钟。如果您想要更长的时间,可以在web.config中进行配置。@muratgu我的超时时间是60分钟,但会话有时会在一分钟内过期。我不使用池回收,我不知道那是什么@GabrielGM