C# 在信号器中进行用户身份验证后,IsAuthenticated属性仍然为false
我已在MVC web应用程序中登录用户,但我的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
Hub
类中的IsAuthenticated
属性仍然为false。以下是我的登录和重定向顺序:
登录
表单FormsAuthenticationTicket
并使用FormsAuthentication
类对票据进行加密FormsAuthentication.FormsCookieName
路径中的响应cookie中OnConnected
方法中检查用户是否经过身份验证时,我发现它仍然为false任何建议都会有帮助。提前感谢。如果您自己将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)