新增加signalR连接jwt认证

This commit is contained in:
不做码农 2022-02-28 18:37:23 +08:00
parent c067951d50
commit 6c2b4bc56f
2 changed files with 12 additions and 10 deletions

View File

@ -11,7 +11,6 @@ using ZR.Model;
namespace ZR.Admin.WebApi.Hubs namespace ZR.Admin.WebApi.Hubs
{ {
[Verify]
public class MessageHub : Hub public class MessageHub : Hub
{ {
//创建用户集合,用于存储所有链接的用户数据 //创建用户集合,用于存储所有链接的用户数据
@ -25,14 +24,14 @@ namespace ZR.Admin.WebApi.Hubs
/// <returns></returns> /// <returns></returns>
public override Task OnConnectedAsync() public override Task OnConnectedAsync()
{ {
//name 获取不到有待研究
var name = Context.User.Identity.Name; var name = Context.User.Identity.Name;
var user = clientUsers.Any(u => u.ConnnectionId == Context.ConnectionId); var user = clientUsers.Any(u => u.ConnnectionId == Context.ConnectionId);
//判断用户是否存在,否则添加集合 //判断用户是否存在,否则添加集合
if (!user) if (!user && Context.User.Identity.IsAuthenticated)
{ {
clientUsers.Add(new OnlineUsers(Context.ConnectionId, Context.User.Identity.Name)); clientUsers.Add(new OnlineUsers(Context.ConnectionId, name));
Console.WriteLine($"{DateTime.Now}{Context.User.Identity.Name},{Context.ConnectionId}连接服务端success当前已连接{clientUsers.Count}个"); Console.WriteLine($"{DateTime.Now}{name},{Context.ConnectionId}连接服务端success当前已连接{clientUsers.Count}个");
} }
Clients.All.SendAsync("onlineNum", clientUsers.Count); Clients.All.SendAsync("onlineNum", clientUsers.Count);
@ -51,7 +50,6 @@ namespace ZR.Admin.WebApi.Hubs
{ {
Console.WriteLine($"用户{user?.Name}离开了,当前已连接{clientUsers.Count}个"); Console.WriteLine($"用户{user?.Name}离开了,当前已连接{clientUsers.Count}个");
clientUsers.Remove(user); clientUsers.Remove(user);
Clients.All.SendAsync("onlineNum", clientUsers.Count); Clients.All.SendAsync("onlineNum", clientUsers.Count);
} }
return base.OnDisconnectedAsync(exception); return base.OnDisconnectedAsync(exception);

View File

@ -62,7 +62,7 @@ namespace ZR.Admin.WebApi
//绑定整个对象到Model上 //绑定整个对象到Model上
services.Configure<OptionsSetting>(Configuration); services.Configure<OptionsSetting>(Configuration);
//Cookie ÈÏÖ¤ //jwt ÈÏÖ¤
services.AddAuthentication(options => services.AddAuthentication(options =>
{ {
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme; options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
@ -100,6 +100,10 @@ namespace ZR.Admin.WebApi
app.Use((context, next) => app.Use((context, next) =>
{ {
context.Request.EnableBuffering(); context.Request.EnableBuffering();
if (context.Request.Query.TryGetValue("access_token", out var token))
{
context.Request.Headers.Add("Authorization", $"Bearer {token}");
}
return next(); return next();
}); });
//开启访问静态文件/wwwroot目录文件要放在UseRouting前面 //开启访问静态文件/wwwroot目录文件要放在UseRouting前面
@ -126,12 +130,12 @@ namespace ZR.Admin.WebApi
app.UseEndpoints(endpoints => app.UseEndpoints(endpoints =>
{ {
//ÉèÖÃsocketÁ¬½Ó
endpoints.MapHub<MessageHub>("/msgHub");
endpoints.MapControllerRoute( endpoints.MapControllerRoute(
name: "default", name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}"); pattern: "{controller=Home}/{action=Index}/{id?}");
//ÉèÖÃsocketÁ¬½Ó
endpoints.MapHub<MessageHub>("/msgHub");
}); });
} }