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

相关内容

热门资讯

科普分享!拱趴大菠萝十三水作弊... 科普分享!拱趴大菠萝十三水作弊,wepoker是不是有人用挂,其实真的是有挂(证实有挂)1、让任何用...
分享实测!wpk刷入池率脚本,... 分享实测!wpk刷入池率脚本,wepoker破解器,本来真的有挂(有挂教学)1、操作简单,无需手机版...
我来教教大家!德州辅助工具到底... 我来教教大家!德州辅助工具到底怎么样,智星德州辅助译码插件靠谱吗,总是是真的有挂(有挂分析)1、这是...
热点推荐!wpk官网下载链接,... 热点推荐!wpk官网下载链接,aa poker辅助包,切实是有挂(有挂技术)1、脚本辅助下载、免费透...
揭秘!wepoker正确养号方... 揭秘!wepoker正确养号方法,hhpoker透视脚本下载,一贯真的有挂(有挂教程)1、进入游戏-...
一分钟快速了解!wepoker... 一分钟快速了解!wepoker透视挂底牌,wepoker有辅助吗,其实真的是有挂(有挂助手)1、玩家...
重大通报!wepoker有什么... 您好,这款游戏可以开挂的,确实是有挂的,需要了解加去威信【136704302】很多玩家在这款游戏中打...
重磅来袭!wpk显示有作弊,约... 重磅来袭!wpk显示有作弊,约局吧是否有挂,一直是有挂(有挂头条)小薇(辅助器软件下载)致您一封信;...
六分钟了解!hhpoker透视... 六分钟了解!hhpoker透视工具,佛手大菠萝辅助,确实真的有挂(有挂神器)1、用户打开应用后不用登...
来一盘!wepoker轻量版透... 来一盘!wepoker轻量版透视,wepokerplus作弊,一直真的是有挂(有挂教学)1、完成辅助...