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

相关内容

热门资讯

7分钟辅助!wepoker透视... 7分钟辅助!wepoker透视有没有,wepoker轻量版透视,妙计教程(有挂功能)1、每一步都需要...
6分钟辅助!德普之星怎么开辅助... 6分钟辅助!德普之星怎么开辅助,wepoker脚本下载,项目教程(真实有挂)1、玩家可以在德普之星怎...
第五分钟辅助!aapoker怎... 第五分钟辅助!aapoker怎么设置提高好牌几率,如何下载wpk透视版,演示教程(有挂方针)1、aa...
第8分钟辅助!wpk安卓下载辅... 第8分钟辅助!wpk安卓下载辅助,哈糖大菠萝怎么挂,经验教程(有挂方法)哈糖大菠萝怎么挂破解侠是真的...
第三分钟辅助!hhpoker免... 第三分钟辅助!hhpoker免费辅助器,aapoker辅助软件合法吗,烘培教程(有挂神器)一、aap...
第二分钟辅助!wepoker插... 第二分钟辅助!wepoker插件功能辅助器,wepoker私人局透视插件,妙招教程(有挂细节)1、金...
六分钟辅助!wepoker透视... 六分钟辅助!wepoker透视是真的吗,wpk俱乐部辅助器,阶段教程(发现有挂)1、wepoker透...
第四分钟辅助!wepoker有... 第四分钟辅助!wepoker有没有透视方法,wepoker有机器人吗,妙计教程(有挂攻略)1、用户打...
第3分钟辅助!hh poker... 第3分钟辅助!hh poker透视器下载,德普之星透视,技法教程(有挂功能)在进入hh poker透...
第1分钟辅助!wepoker数... 第1分钟辅助!wepoker数据分析,we-poker有人玩吗,法门教程(有挂助手)1、we-pok...