Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在信号器中进行用户身份验证后,IsAuthenticated属性仍然为false_C#_Asp.net Mvc_Authentication_Signalr_Asp.net Mvc 5 - Fatal编程技术网

C# 在信号器中进行用户身份验证后,IsAuthenticated属性仍然为false

C# 在信号器中进行用户身份验证后,IsAuthenticated属性仍然为false,c#,asp.net-mvc,authentication,signalr,asp.net-mvc-5,C#,Asp.net Mvc,Authentication,Signalr,Asp.net Mvc 5,我已在MVC web应用程序中登录用户,但我的Hub类中的IsAuthenticated属性仍然为false。以下是我的登录和重定向顺序: 用户提交登录表单 应用程序验证用户信息 应用程序创建FormsAuthenticationTicket并使用FormsAuthentication类对票据进行加密 应用程序将加密票据添加到FormsAuthentication.FormsCookieName路径中的响应cookie中 然后我将其重定向到启动信号器连接的视图,当我在overrideOnConn

我已在MVC web应用程序中登录用户,但我的
Hub
类中的
IsAuthenticated
属性仍然为false。以下是我的登录和重定向顺序:

  • 用户提交
    登录
    表单
  • 应用程序验证用户信息
  • 应用程序创建
    FormsAuthenticationTicket
    并使用
    FormsAuthentication
    类对票据进行加密
  • 应用程序将加密票据添加到
    FormsAuthentication.FormsCookieName
    路径中的响应cookie中
  • 然后我将其重定向到启动信号器连接的视图,当我在override
    OnConnected
    方法中检查用户是否经过身份验证时,我发现它仍然为false
  • 我应该如何处理这个问题,我在Signalr1.1中对授权没有任何问题,我使用的代码与Signalr2.0相同,但这次使用的是Signalr2.0

    更新:实际上,它在SignalR Hub类中仍然是错误的,但在所有应用程序域中都是错误的


    任何建议都会有帮助。提前感谢。

    如果您自己将cookie添加到响应中,您也需要自己解决它

    if(Login(model.UserName, model.Password)){
        // ecrypt user data and add to cookie
        // your code
    }
    
    // Global file code
    protected void Application_PostAuthenticateRequest(Object sender, EventArgs e)
    {
        // decrypt cookie and set current principal
        // your code
    
        Thread.CurrentPrincipal = HttpContext.Current.User = principal;
    }
    

    另外,如果传统的GenericPrincipal不适合,您需要一个customer principal类来存储用户信息。

    在Visual Studio 2013中,默认MVC模板使用ASP.Net身份验证系统,并将
    放在web.config中,因此这会阻止应用程序创建
    HttpCookie
    。我只是将其更改为
    ,现在它开始工作。

    在设置上述模式后,我仍然得到isAuthenticated false。@ahmadalibaloch从cookie中获取值后,如果在编写cookie时对字符串值进行加密,则需要对其进行解密,并且您最好了解有关IPrincipal接口的更多信息。太好了!它可以工作(SignarR+现有cookie)