【问题标题】:Action Filter Attribute does not work correctly with Hub class动作过滤器属性不能与 Hub 类一起正常工作
【发布时间】:2014-08-19 08:47:57
【问题描述】:

在 asp.net Web API 中,我正在使用操作过滤器属性,我覆盖 OnActionExecuting 和 OnActionExecuted 以验证从客户端在标头中发送的令牌,当在任何控制器上添加该属性时,我可以在传递请求之前拦截请求到现在为止,控制器中的功能一切正常,但如果我在 Hub 类上添加与调试时相同的属性,则情况并非如此,我可以看到集线器上的方法首先被执行,然后 OnActionExecuting 和 OnActionExecuted .如何解决这个问题。

 [OurAuthorization]
    public class NotificationHub : Hub 
    {
        private AccPlusEntities1 db = new AccPlusEntities1();

        public void TrnAddedNotfication(string GroupName)
        {
            Clients.OthersInGroup(GroupName).AddedNotfication();
        }

        public void TrnDeletedNotfication(string GroupName)
        {
            Clients.OthersInGroup(GroupName).DeletedNotfication();
        }

        public void JoinGroup(string AccName)
        {
            var x = BaseController.CurrentUser.AccId;
            Groups.Add(Context.ConnectionId, AccName);
        }

        public void LeaveGroup(string AccName)
        {
            Groups.Remove(Context.ConnectionId, AccName);
        }

        public void Lock(int TrnId)
        {

        }
    }  

【问题讨论】:

    标签: c# asp.net asp.net-web-api signalr


    【解决方案1】:

    如您所见,操作筛选器属性不适用于 SignalR 集线器。幸运的是,SignalR 确实提供了它自己的AuthorizeAttribute,它可以实现相同的目的。

    与ActionFilterAttributes 一样,您可以将SignalR 的AuthorizeAttribute 子类化并覆盖UserAuthorized method。

    【讨论】:

    • 谢谢。您还可以覆盖 AuthorizeHubConnection 方法并获取有关请求的所有信息(用于日志事件)。
    猜你喜欢
    • 2022-12-11
    • 1970-01-01
    • 2013-03-28
    • 2023-03-23
    • 1970-01-01
    • 2015-05-06
    • 2017-06-17
    • 1970-01-01
    • 2018-11-10
    相关资源
    最近更新 更多