AngularSignalR认证问题
创始人
2024-10-29 15:31:40
0
  1. 在服务器端实现SignalR的Token认证逻辑。

  2. 通过使用SignalR的JwtBearer认证提供程序,配置服务器端SignalR发送授权令牌。

  3. 在客户端上使用JwtInterceptor拦截器,将Jwt令牌添加到每个SignalR请求的标头中。 示例: [Authorize] public class ChatHub : Hub { // 必须与前端代码一致 private const string HUB_PATH = "/mychat";

    public async Task SendMessage(string message) { await Clients.All.SendAsync("ReceiveMessage", message); } public override async Task OnConnectedAsync() { // 通过获取JWT令牌,验证并处理请求 var httpContext = Context.GetHttpContext(); var header = httpContext.Request.Headers["Authorization"].ToString();

     // 获取JWT令牌
     var token = header.Split(" ")[1];
    
     // 验证JWT令牌
     var validatedToken = new JwtSecurityToken(token);
    
     // 处理请求,没有问题才能继续
     if (validatedToken.ValidTo > DateTime.Now)
     {
        await base.OnConnectedAsync();
     }
     else
     {
        // 鉴定没过就断开连接
        Context.Abort();
     }
    

    } }

  4. 在Angular app.module.ts文件中,添加JwtInterceptor拦截器: import { HTTP_INTERCEPTORS } from '@angular/common/http'; import { JwtInterceptor } from './jwt.interceptor'; @NgModule({ ... providers: [ ... { provide: HTTP_INTERCEPTORS, useClass: JwtInterceptor, multi: true } ] }) export class AppModule { } 示例: import { Injectable } from '@angular/core'; import { HttpRequest, HttpHandler, HttpEvent, HttpInterceptor } from '@angular/common/http'; import { AuthService } from './auth.service'; import { Observable } from 'rxjs'; @Injectable() export class JwtInterceptor implements HttpInterceptor { constructor(private authService: AuthService) { } intercept(request: HttpRequest, next: Http

相关内容

热门资讯

透视脚本!hhpoker万能辅... 透视脚本!hhpoker万能辅助器,wepoker好友房开挂,规律教程(确实真的是有挂)1、许多玩家...
透视了解!红龙poker脚本,... 自定义aapoker透视插件系统规律,只需要输入自己想要的开挂功能,一键便可以生成出微扑克专用辅助器...
透视存在!wpk透视辅助方法,... 透视存在!wpk透视辅助方法,佛手大菠萝有挂吗,2025新版教程(素来有挂);亲,其实确实真的有挂(...
透视讲解!aapkoer德州辅... 透视讲解!aapkoer德州辅助挂下载,aapoker辅助工具存在吗,避坑细节(有挂教学)是一款可以...
透视黑科技!wepoker底牌... 透视黑科技!wepoker底牌透视脚本怎样安装,约局吧如何查看是否有挂,教你攻略(往昔存在有挂);是...
辅助透视!aapoker辅助是... 辅助透视!aapoker辅助是真的吗,wpk辅助器,实测分享(有挂技巧);辅助透视!aapoker辅...
透视游戏!wepoker辅助透... 透视游戏!wepoker辅助透视软件,wepoker黑侠辅助器,软件教程(先前真的有挂);亲们利用一...
透视实锤!智星德州菠萝透视插件... 自定义智星德州菠萝透视插件工具系统规律,只需要输入自己想要的开挂功能,一键便可以生成出微扑克专用辅助...
透视ai!hhpoker透视方... 自定义aapoker免费透视脚本系统规律,只需要输入自己想要的开挂功能,一键便可以生成出微扑克专用辅...
透视科技!hhpoker万能辅... 透视科技!hhpoker万能辅助器,hhpoker免费透视脚本,玩家攻略推荐(有挂教学)是一款可以让...