signalr Hub中Context的User为Null

1.问题

signalr无法判断某次连接是哪个用户,在Context中查看User对象为null
signalr Hub中Context的User为Null_第1张图片

2.解决

在start_up中加入 ConfigureAuth(app); 跟app.MapSignalR()一起;
signalr Hub中Context的User为Null_第2张图片
其中我的ConfigureAuth方法如下(用的是Microsoft.AspNet.Identity中的登录、用户管理、用户权限等功能,如果不懂可以查找相关资料):

using BE.Common;
using BE.EF;
using BLL.GlobalMethod;
using Microsoft.AspNet.Identity;
using Microsoft.Owin;
using Microsoft.Owin.Security.Cookies;
using Microsoft.Owin.Security.DataHandler.Encoder;
using Microsoft.Owin.Security.DataHandler.Serializer;
using Microsoft.Owin.Security.DataProtection;
using Owin;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;

namespace JGHPCX.App_Start
{
    public partial class Startup
    {
        public static IDataProtector dataProtector = null;

        // For more information on configuring authentication, please visit https://go.microsoft.com/fwlink/?LinkId=301864
        public void ConfigureAuth(IAppBuilder app)
        {
            // 配置数据库上下文、用户管理器和登录管理器,以便为每个请求使用单个实例
            app.CreatePerOwinContext(DBModel.Create);
            app.CreatePerOwinContext<AppUserManager>(AppUserManager.Create);
            app.CreatePerOwinContext<AppSignInManager>(AppSignInManager.Create);
            app.CreatePerOwinContext<AppRoleManager>(AppRoleManager.Create);

             
            // 使应用程序可以使用 Cookie 来存储已登录用户的信息
            // 并使用 Cookie 来临时存储有关使用第三方登录提供程序登录的用户的信息
            // 配置登录 Cookie
            var options = new CookieAuthenticationOptions
            {
                AuthenticationType = CustomAuthenticationTypes.ApplicationCookie,
                LoginPath = new PathString("/Login/LoginPage"),
                Provider = new CookieAuthenticationProvider
                {
                    // 当用户登录时使应用程序可以验证安全戳。
                    // 这是一项安全功能,当你更改密码或者向帐户添加外部登录名时,将使用此功能。
                    //OnValidateIdentity = SecurityStampValidator.OnValidateIdentity(
                    //    validateInterval: TimeSpan.FromMinutes(30),
                    //    regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager))
                }
            };
            app.UseCookieAuthentication(options);
            app.UseExternalSignInCookie(CustomAuthenticationTypes.ExternalCookie);

            // 使应用程序可以在双重身份验证过程中验证第二因素时暂时存储用户信息。
            app.UseTwoFactorSignInCookie(CustomAuthenticationTypes.TwoFactorCookie, TimeSpan.FromMinutes(5));

            // 使应用程序可以记住第二登录验证因素,例如电话或电子邮件。
            // 选中此选项后,登录过程中执行的第二个验证步骤将保存到你登录时所在的设备上。
            // 此选项类似于在登录时提供的“记住我”选项。
            app.UseTwoFactorRememberBrowserCookie(CustomAuthenticationTypes.TwoFactorRememberBrowserCookie);

       
        }

       
    }
}

你可能感兴趣的:(C#,SignalR)