对于管理匿名用户SignalR连接 [英] Managing SignalR connections for Anonymous user

查看:142
本文介绍了对于管理匿名用户SignalR连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用SignalR版本2.1.2与ASP.Net MVC 5安培; NServiceBus并具有下列要求

I am using SignalR version 2.1.2 with ASP.Net MVC 5 & NServiceBus and have following requirement

有是在SignalR用于发送通知的注册页面(匿名认证)。每个表单提交将产生一个新的连接ID它需要保持一个集合中,这样我可以发送响应到客户端。 Context.User.Identity.Name是空的,因此_connections.Add(姓名,Context.ConnectionId);不能在OnConnected()枢纽事件在此的帖子

There is a signup page (anonymous authentication) in which SignalR is used to send notifications. Every form submit will generate a new connection id which needs to be kept in a collection so that I can send response to the client. Context.User.Identity.Name is empty hence _connections.Add(name, Context.ConnectionId); cannot be used in OnConnected() hub event as given in this post

类似的问题。

如果有一个可能控制的ConnectionId然后我可以克服这种情况,但它看起来像SignalR新版本已经摆脱了连接工厂。

If there is a possibility to control the ConnectionId then I could overcome this situation but it looks like new version of SignalR has got rid of connection factory.

我使用Redis的缓存,这样一个选择是写我自己的连接管理代码保持这些连接ID在里面。

I am using Redis cache so one option is to write my own connection management code to keep these connection ids in it.

第二个选择是使用窗体身份验证以这样的方式,一个匿名角色分配给这些用户制约了使用匿名查看/控制器,反而给人一种名称的用户,这样Context.User.Identity.Name不为空。有了这个,我可以使用内置的SignalR机制来管理连接ID我。

Second option is to use Forms Authentication in such a way that a 'Anonymous Role' is assigned to these users which restricts the usage to anonymous views/controllers but gives a 'Name' to the user so that Context.User.Identity.Name is not empty. With this I can use built in SignalR mechanism to manage connection ids for me.

推荐答案

使用表单验证,存储联合Cookie和存储在cookie中的枢纽地区以及..
在SignalR jQuery代码,使用jQuery插件读取的HTTP cookie,并得到该地区的名称和订阅通知。

Use Forms Authentication, store a Federated Cookie and store the hub region in the cookie as well.. In SignalR jQuery code, use a jQuery plugin to read HTTP cookie and get the region name and subscribe to notifications.

另外,在你的.cshtml,jQuery的渲染与您的视图模型填充区域

Alternatively, in your .cshtml, render jQuery with region populated from your View Model.

注意:使用 FormsAuthentication.SetAuthCookie ,因为这将创建HTTP cookie的唯一并会在Ajax和非Ajax调用发送。

Note: Use FormsAuthentication.SetAuthCookie as this will create HTTP Only cookie and will be sent in Ajax and non-Ajax calls.

这篇关于对于管理匿名用户SignalR连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆