我只想知道:
- 存在于所有请求中的“连接 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/