在SignalR中进行用户身份验证后,IsAuthenticated属性仍为false

时间:2013-11-18 06:37:31

标签: c# asp.net-mvc authentication signalr asp.net-mvc-5

我在MVC Web应用程序中登录了用户,但IsAuthenticated类中的Hub属性仍为false。这是我的登录和重定向序列:

  1. 用户提交Login表单。
  2. 应用程序验证用户信息。
  3. 应用程序创建FormsAuthenticationTicket并使用FormsAuthentication类加密票证。
  4. 应用程序将加密票证添加到FormsAuthentication.FormsCookieName路径中的响应Cookie。
  5. 然后我将它重定向到一个启动SignalR连接的视图,当我检查用户是否通过覆盖OnConnected方法进行身份验证时,我发现它仍然是假的。
  6. 我应该如何处理这个问题,我对SignalR 1.1中的授权没有任何问题,我使用的是相同的代码,但这次使用的是SignalR 2.0。

    更新 实际上它在SignalR Hub类中仍然不是假的,它在整个应用程序域中都是错误的。

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

2 个答案:

答案 0 :(得分:2)

如果你自己添加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不适合,您需要一个客户主要类来存储用户信息。

答案 1 :(得分:0)

在Visual Studio 2013中,默认MVC模板使用ASP.Net Identity身份验证系统并将<authentication mode="None" />放入web.config中,因此这会阻止应用程序创建HttpCookie。我只需将其更改为<authentication mode="Forms" />,现在它正在运行。

相关问题