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私... 玩家必备科普!wepoker私人局辅助,wpk真吗,一直真的有挂(有挂猫腻)玩家必备科普!wepok...
玩家必看教程!hhpoker是... 玩家必看教程!hhpoker是真的吗,wpk辅助软件,真是真的是有挂(确实有挂)1、下载好正确养号方...
揭秘攻略!德普之星透视辅助插件... 揭秘攻略!德普之星透视辅助插件,德州局脚本,原来真的是有挂(证实有挂)1、在插件功能辅助器技巧中,中...
教程攻略!pokerrrr2辅... 教程攻略!pokerrrr2辅助,wepoker辅助器官方,一贯是真的有挂(发现有挂)1、每一步都需...
我来分享!wepokerplu... 我来分享!wepokerplus辅助,wepoker黑侠破解,一直有挂(有挂方式)1、任何透视是真的...
9分钟了解!we-poker辅... 9分钟了解!we-poker辅助软件教程,购买的wpk辅助在哪里下载,真是是有挂(有挂细节)1、让任...
我来教教你!德普之星辅助工具如... 我来教教你!德普之星辅助工具如何打开,德州透视插件,真是是真的有挂(有挂功能)1、有没有辅助教程、有...
必备科技!we-poker辅助... 必备科技!we-poker辅助软件教程,购买的wpk辅助在哪里下载,真是存在有挂(今日头条)暗藏猫腻...
玩家亲测!hhpoker买挂,... 玩家亲测!hhpoker买挂,wepoker私人局辅助器怎么用,竟然是真的有挂(有挂细节)1)wep...
解密关于!德州局透视脚本免费版... 解密关于!德州局透视脚本免费版下载手机版,德州机器人代打脚本,总是是真的有挂(有挂秘籍)1、公共底牌...