SignalR中的AllowAnonymous属性 [英] AllowAnonymous attribute in SignalR

查看:143
本文介绍了SignalR中的AllowAnonymous属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

[Authorize] // Derived from Microsoft.AspNet.SignalR.Authorize
public class ChatHub : Hub
{
    [System.Web.Mvc.AllowAnonymous] // [AllowAnonymous] doesn't be supported here
    public async Task GetEmails()
    {
        using (var db = new MyDbContext())
        {
            var users = await db.Users.ToListAsync();
            var emails = new List<string>();
            users.ForEach(x => emails.Add(x.Email));
            Clients.All.getEmails(JsonConvert.SerializeObject(emails));
        }
    }
}

我的问题:我将断点设置为 GetEmails 方法,但从未实现.然后,我登录并再次尝试,它正在工作.

My problem: I'd set breakpoint to GetEmails method but it's never hit to. Then, I signed in and tried again, it's working.

这意味着: [System.Web.Mvc.AllowAnonymous] [Microsoft.AspNet.SignalR.Authorize] 中不起作用.

That means: [System.Web.Mvc.AllowAnonymous] doen't work within [Microsoft.AspNet.SignalR.Authorize].

所以,我的问题是:为什么命名空间 Microsoft.AspNet.SignalR 不支持 [AllowAnonymous] 属性?我是否需要为此声明 AllowAnonymous 属性?

So, my question is: Why doesn't namespace Microsoft.AspNet.SignalR support [AllowAnonymous] attribute? And do I need to declare AllowAnonymous attribute for that?

推荐答案

我已经解决了问题:使用 [System.Web.Mvc.Authorize] 代替 [Authorize] .像这样:

I've solved the problem: Using [System.Web.Mvc.Authorize] instead of [Authorize]. Like this:

[System.Web.Mvc.Authorize] 
public class ChatHub : Hub
{
    [System.Web.Mvc.AllowAnonymous] 
    public async Task GetEmails()
    {
        // ...
    }
}

我不明白为什么,但是它应该可以很好地工作:)

I don't understand why but it should work perfectly :)

这篇关于SignalR中的AllowAnonymous属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆