与现有的授权集成SignalR [英] Integrating SignalR with existing Authorization
问题描述
我一直在努力将SignalR授权的方式使用自定义的授权供应商(称为MVCAuthorization)属性我下楼的试图重新为中心专门授权提供了几个洞的兔子,但竟然是远太过复杂。所以我在想,我怎么能我现有的控制器和动作授权整合与我SignalR集线器和方法是什么?
I've been working on a way of integrating SignalR Authorization Attributes with a custom authorization provider (called MVCAuthorization) I went down a few rabbit holes of trying to recreate an Authorization provider for hubs specifically, but that turned out to be far too complicated. So I was wondering, how I can integrate my existing Controller and Action Authorization with my SignalR Hubs and methods?
推荐答案
我想通了,你可以检索IAuthorization提供商。
I figured out that you can retrieve an IAuthorization provider.
如果你把你的集线器控制器,你的方法为你的行动,你所要做的就是创建一个实现一个SignalR属性IAuthorizeHubConnection和IAuthorizeHubMethodInvocation
If you treat you hub as a controller, and your methods as your actions, all you have to do is create a SignalR Attribute that implements IAuthorizeHubConnection and IAuthorizeHubMethodInvocation
public class HubAuthorizeAttribute : Attribute, IAuthorizeHubConnection,IAuthorizeHubMethodInvocation
{
public virtual bool AuthorizeHubConnection(HubDescriptor hubDescriptor, Microsoft.AspNet.SignalR.IRequest request)
{
IAuthorizationProvider authorizationProvider = DependencyResolver.Current.GetService<IAuthorizationProvider>();
return authorizationProvider.IsAuthorizedController(hubDescriptor.Name);
}
public virtual bool AuthorizeHubMethodInvocation(IHubIncomingInvokerContext hubIncomingInvokerContext)
{
IAuthorizationProvider authorizationProvider = DependencyResolver.Current.GetService<IAuthorizationProvider>();
return authorizationProvider.IsAuthorizedAction(hubIncomingInvokerContext.MethodDescriptor.Hub.Name, hubIncomingInvokerContext.MethodDescriptor.Name);
}
}
然后,所有你所要做的就是把属性上的集线器或你想要授权的任何方法
Then all you have to do is put the attribute on your hub or any methods you want authorized
[HubAuthorize]
public class Message : Hub
{
public void Send(string message)
{
}
}
这篇关于与现有的授权集成SignalR的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!