Methods 集线器上的[Authorize]属性是否适用于OnConnection()方法?

Methods 集线器上的[Authorize]属性是否适用于OnConnection()方法?,methods,authorization,signalr,signalr-hub,Methods,Authorization,Signalr,Signalr Hub,集线器上的authorize属性是否应用于OnConnection方法 我问,因为如果我将[Authorize]属性应用于我的中心,非授权用户仍然可以调用此方法 我应该对此方法执行手动授权吗?Signal提供Authorize属性来指定哪些用户或角色可以访问集线器或方法。此属性位于Microsoft.AspNet.signal命名空间中。您可以将Authorize属性应用于中心或中心中的特定方法。将Authorize属性应用于中心类时,指定的授权要求将应用于中心中的所有方法。如果没有Author

集线器上的authorize属性是否应用于OnConnection方法

我问,因为如果我将[Authorize]属性应用于我的中心,非授权用户仍然可以调用此方法


我应该对此方法执行手动授权吗?

Signal提供Authorize属性来指定哪些用户或角色可以访问集线器或方法。此属性位于Microsoft.AspNet.signal命名空间中。您可以将Authorize属性应用于中心或中心中的特定方法。将Authorize属性应用于中心类时,指定的授权要求将应用于中心中的所有方法。如果没有Authorize属性,连接的客户端可以访问集线器上的任何公共方法

如果您在web应用程序中定义了一个名为Admin的角色,则可以使用以下代码指定只有该角色中的用户才能访问中心

[Authorize(Roles = "Admin")] 
public class AdminAuthHub : Hub 
{ 
} 
或者,您可以指定集线器包含一个对所有用户可用的方法,以及一个仅对经过身份验证的用户可用的第二个方法,如下所示

public class SampleHub : Hub 
{ 
    public void UnrestrictedSend(string message){ . . . } 

    [Authorize] 
    public void AuthenticatedSend(string message){ . . . } 
} 
以下示例针对不同的授权场景:

 [Authorize] – only authenticated users
  
 [Authorize(Roles = "Admin,Manager")] – only authenticated users in the specified roles
    
 [Authorize(Users = "user1,user2")] – only authenticated users with the specified user names
    
 [Authorize(RequireOutgoing=false)] – only authenticated users can invoke the hub,
 but calls from the server back to clients are not limited by authorization, such as, when only 
 certain users can send a message but all others can receive the message. 
 The RequireOutgoing property can only be applied to the entire hub, not on individuals methods within the hub. When RequireOutgoing is not set to false, only users that meet the authorization requirement are called from the server.
你可以在这里得到更多的细节


希望这能有所帮助。

我刚刚意识到为什么要为未经授权的用户调用该方法

这是因为我的中心上有以下内容:

[Authorize(RequireOutgoing=false)]
不仅仅是:

[Authorize]

RequireOutGong命名参数会导致为未经授权的用户调用OnConnection方法。

谢谢您的回答,但是使用Microsoft.AspNet.signal[Authorize]属性,仍然会为未经授权的用户调用OnConnection方法。谢谢如果您注意到我在底部的回答,我已经提到了RequireOutGoing属性,您需要将其设置为false,以便只有授权用户才能访问您的中心。