Fre*_*ood 5 .net c# asp.net-mvc asp.net-mvc-4 signalr
在SignalR中,我体验到Context.User突然变成空值,并且它有时完全为空但是这永远不会发生,因为只有授权用户才能访问集线器.
这些奇怪行为的原因是什么?我在Visual Studio 2013上使用SignalR 2.0和ASP.NET MVC 4.
[Authorize]
public class FeedHub : Hub
{
public override Task OnConnected()
{
var name = Context.User.Identity.Name;// here is User is not null
var user = GetUser();// but it is changing to null inside this private method
return base.OnConnected();
}
private User GetUser()
{
var name = Context.User.Identity.Name;// here is User property is null and throws exception
return null;//
}
public override Task OnDisconnected()
{
//In here Context.User property is sometimes null but in my opinion this should never be null
// because Hub is protected by Authorize attribute.
return base.OnDisconnected();
}
}
Run Code Online (Sandbox Code Playgroud)
use*_*591 -2
我用于Context.ConnectionId我的 SignalR 2.0 项目,我对它非常满意。
在本例中存储connid static string,或者astatic List来存储传入的连接id,因为http是无状态的,每次回发都会删除该信息。
public class FeedHub : Hub
{
static string username = "";
public override Task OnConnected() //event to fire whenever someone joins
{
var name = Context.ConnectionId;//capture the unique incoming connection id
var username = name;//write it into the static string
return base.OnConnected();
}
public override Task OnDisconnected() //event to fire whenever someone quits
{
//In here Context.User property is sometimes null because you only recognize authorized users.
return base.OnDisconnected();
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3414 次 |
| 最近记录: |