从OWIN CookieAuthentication cookie填充SignalR HubCallerContext.User

时间:2015-03-21 06:18:57

标签: authentication asp.net-mvc-5 signalr asp.net-identity owin

我有一个使用OWIN CookieAuthentication的MVC5网络应用程序。当用户成功登录到Web应用程序时,将连接到在控制台应用程序中运行的SignalR集线器,该控制器应用程序也配置了OWIN CookieAuthentication。

在SignalR Startup中,我指定了GlobalHost.HubPipeline.RequireAuthentication(),因为我想通过Web应用程序发送的ClaimsPrincipal获取HubCallerContext.User属性。检查可用的cookie,我可以看到auth cookie已成功接收,但我无法弄清楚如何设置OWIN配置,以便从中设置只读主体。

我已经看过相关的帖子建议,例如下面的变体:

        var cookieAuthenticationOptions = new CookieAuthenticationOptions
        {
            AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
            AuthenticationMode = AuthenticationMode.Active,
            Provider = new CookieAuthenticationProvider
            {
                OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, ApplicationUser>(
                    TimeSpan.FromSeconds(30),
                    (manager, user) => manager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie))
            }
        };

        app.UseCookieAuthentication(cookieAuthenticationOptions);

但是我不能让这些产生任何影响,并且无论如何它似乎涉及很多外围的Identity框架相关类的设置(现在我已经从MVC5项目模板中移植了测试)。我只想用加密到故障单中的声明来保护User属性,然后在我的SignalR系统中查找已识别的用户,但我觉得合适。我该怎么做?

编辑:作为解决方法,我创建了一个单独的服务器cookie&#34;在我在网络应用程序中登录用户时。我二进制序列化ClaimsIdentity,用AES加密,base 64编码并将其放入cookie中。然后在我的Hub类中,我有一个按需的User属性,它读取该cookie并解压缩它。不是理想的解决方案!

0 个答案:

没有答案