【问题标题】:SignalR Irequest User is nullSignalR 请求用户为空
【发布时间】:2020-04-19 14:27:39
【问题描述】:

我遵循了这个答案,但我在 Irequest request.User 中得到了 Null

SignalR - Sending a message to a specific user using (IUserIdProvider) *NEW 2.0.0*

我像这样在 app_start 文件夹中创建了一个类

public class CustomUserIdProvider : IUserIdProvider
    {
        public string GetUserId(IRequest request)
        {
            // your logic to fetch a user identifier goes here.

            // for example:

            var userId = request.User.Identity.Name;
            return userId.ToString();
        }
    }

在此之后,我在 Startup.cs 中初始化新的 cotom 提供程序,如下所示:

 public void Configuration(IAppBuilder app)
        {
            var idProvider = new CustomUserIdProvider();

            GlobalHost.DependencyResolver.Register(typeof(IUserIdProvider), () => idProvider);

            // Any connection or hub wire up and configuration should go here
            app.MapSignalR();
         }

但我在网上遇到异常

var userId = request.User.Identity.Name;
System.NullReferenceException: 'Object reference not set to an instance of an object.'

【问题讨论】:

  • 确保您的用户已通过身份验证,否则 User 属性将始终为空。
  • 不,登录工作正常.. 我在许多其他位置使用 User.Identity.Name.. 但似乎这不是同一类型

标签: c# asp.net model-view-controller signalr


【解决方案1】:

发现问题, 我必须在 startup.cs 结束时进行初始化

app.MapSignalR();

【讨论】:

    猜你喜欢
    • 2023-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-13
    • 2015-01-01
    • 2022-09-30
    • 2020-07-16
    • 1970-01-01
    相关资源
    最近更新 更多