为什么 SignalR Context.User.Identity.Name 是空的?

Why is SignalR Context.User.Identity.Name empty?

This question 帮我修复了一个空 Context.User 值,但在修复之后,Context.User.Identity.Name 是空的。

这是我在 Window 中看到的 Context.User.Identity:

{System.Security.Principal.WindowsIdentity}
    AccessToken: {Microsoft.Win32.SafeHandles.SafeAccessTokenHandle}
    Actor: null
    AuthenticationType: ""
    BootstrapContext: null
    Claims: {System.Security.Principal.WindowsIdentity.<get_Claims>d__95}
    CustomSerializationData: null
    DeviceClaims: Count = 0
    Groups: null
    ImpersonationLevel: Anonymous
    IsAnonymous: true
    IsAuthenticated: false
    IsGuest: false
    IsSystem: false
    Label: null
    Name: ""
    NameClaimType: "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name"
    Owner: null
    RoleClaimType: "http://schemas.microsoft.com/ws/2008/06/identity/claims/groupsid"
    Token: {0}
    User: null
    UserClaims: Count = 0

我怀疑 IsAuthenticated: false 与此有关,但我的身份验证是在 SalesForce 包装器中处理的,而不是通过典型的 FormsAuthentication.SetAuthCookie() 代码 - 我已经搜索了我的存储库,我们不要在任何地方使用 SetAuthCookie。根据我在本地主机网页中看到的内容,我已成功通过身份验证。

必须执行哪些操作才能填充/获取对身份的访问权限Name

我认为您对 IsAuthenticated 属性 的怀疑是正确的。如果您在没有任何类型的令牌 (OAuth/JWT)、cookie、windows 身份验证等的情况下调用您的集线器(或网络 API),则不会填充 Claims/Identity 信息. Here's SignalR 集线器概述 authentication/authorization。我在 Signalr 集线器上成功使用了授权过滤器(包含在 Microsoft.AspNet.SignalR 中的那个),以及在调用中传递的 Bearer 令牌:

 var connection = new HubConnection("http://localhost:50042/signalr");
 connection.Headers.Add("Authorization", "Bearer " + token);

授权过滤器很早就在 Web api 管道中被调用。令牌将被解密,线程 Principle/Identity 将被设置。但是,这个令牌是由我的网站api发布的,因此,它能够被解析。

我没有任何 Salesforce 经验,但听起来您的应用程序设置与我最近所做的有点不同。我假设您的应用程序正在针对 SalesForce API 进行身份验证。从 Salesforce 返回的令牌随后必须用于 authenticate/authorize 调用,以防止对 Salesforce API 的后续调用。但是,您不能 decrypt/parse 该令牌来提取声明(希望如此。否则那不好)。

您是否尝试过将 JWT 与 Salesforce 结合使用(首先请参阅 here)? Or with Web API, utilizing external authentication (example here)? You can probably pass the JWT back and forth between your client and the .NET Web Api that I assume is hosting the signalr hub, and use a custom authorization filter to parse the JWT token to authorize the call to the hub/set the Identity (see Whosebug post)。

希望这能让您有所收获。