HttpContext.Current.User.Identity.IsAuthenticated = false 在 Nginx 环境中总是

HttpContext.Current.User.Identity.IsAuthenticated = false always in Nginx Enviroment

我遇到了一个问题,我使用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 在其中一个站点中是正确的。在另一个网站上它总是 returns false.

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

谢谢。

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

这样两个 IIS 实例都会知道用户。因此,您必须为您的网站使用 'out of process' 缓存实现。

这是一个walkthrough of one such solution using Memcached