c# - SignalR 安全 : how is it working?

标签 c# asp.net signalr

我只想知道:

  • 存在于所有请求中的“连接 token ”在何处创建?通过谁 ?可以定制吗?
  • 如何仅在“connect”方法上应用 AuthorizeAttribute?

看,我只希望用户在第一次发送凭据,然后获得一个 token (定制的会很棒)并使用此 token 进行通信。

我准确地说我使用了一个简单的集线器,没有持久连接。

最佳答案

据我所知,连接 token 只是一个 ID 和用户名。 ID是随机生成的。在 SignalR 的早期版本中,您可以通过实现 IConnectionIdFactory 接口(interface)来自定义它,但是 that hasn't been possible since 2013 .

现在,为了回答“它是如何生成的”这个问题,让我们深入研究 SignalR 的源代码。我正在使用 ILSpy 来搜索源代码。它可以在线免费获得。你可以看到我的 ILSpy 窗口 here .

有趣的代码在 Microsoft.AspNet.SignalR.Infrastructure.ConnectionManager 中:

public IPersistentConnectionContext GetConnection(Type type)
{
    if (type == null)
    {
        throw new ArgumentNullException("type");
    }
    string fullName = type.FullName;
    string persistentConnectionName = PrefixHelper.GetPersistentConnectionName(fullName);
    IConnection connectionCore = this.GetConnectionCore(persistentConnectionName);
    return new PersistentConnectionContext(connectionCore, new GroupManager(connectionCore, PrefixHelper.GetPersistentConnectionGroupName(fullName)));
}

这导致我们:

internal Connection GetConnectionCore(string connectionName)
{
    IList<string> signals = (connectionName == null) ? ListHelper<string>.Empty : new string[]
    {
        connectionName
    };
    string connectionId = Guid.NewGuid().ToString();
    return new Connection(this._resolver.Resolve<IMessageBus>(), this._resolver.Resolve<IJsonSerializer>(), connectionName, connectionId, signals, ListHelper<string>.Empty, this._resolver.Resolve<ITraceManager>(), this._resolver.Resolve<IAckHandler>(), this._resolver.Resolve<IPerformanceCounterManager>(), this._resolver.Resolve<IProtectedData>());
}

原来如此。连接 ID 只是一个随机的 Guid, token 是 ID 加上用户名。

关于c# - SignalR 安全 : how is it working?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45092935/

相关文章:

javascript - 使用 jquery 或 javascript 上传 1 GB 的文件

azure - Azure Web App 重新启动后 SignalR 重新连接

.net - Angular - 如何检测后端变化

c# - 在没有类型拆箱的情况下调用具有特定参数的方法

c# - Windows 服务计划每天在 6 :00 AM 运行一次

c# - 如何将 SQL Server 2012/14 地理类型列设置为 null?

runat ="server"后 Asp.net div 仍然无法访问

asp.net - 获取 ASP.NET MVC 站点在文件系统中的路径

c# - UISearchBar SearchButtonClicked 从未调用过?

c# - 解释为什么 "using"在服务中不起作用?