始终在Nginx环境中的HttpContext.Current.User.Identity.IsAuthenticated = false

时间:2016-12-19 08:47:49

标签: c# asp.net-mvc nginx load-balancing

我发生了一个问题,我使用nginx来实现负载均衡。我使用HttpContext.Current.User.Identity.IsAuthenticated +分布式Redis缓存告诉服务器用户已登录。

public override void OnAuthorization(AuthorizationContext filterContext)
{
    if (filterContext.HttpContext.User.Identity.IsAuthenticated && RedisSession.IsExistKey("Username"))
       //HttpContext.Current.Session["Username"] == null
    {
       base.OnAuthorization(filterContext);
    }
}

我发现redis缓存没问题,但httpContext.Current.User.Identity.IsAuthenticated 在其中一个网站上都是如此。在另一个网站上,它总是返回false。

例如:我使用nginx使用不同的端口映射了两个IIS网站,当我刷新测试页面时,它会给我不同的答案。

感谢。

1 个答案:

答案 0 :(得分:1)

我认为在负载均衡的环境中,您应该在两个站点之间共享您的网站会话。

这样两个IIS实例都会知道用户。因此,您必须使用“过程中”'您网站的缓存实施。

这是使用walkthrough的一个此类解决方案的Memcached