1 Star 1 Fork 0

Arvid / docs

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
signalr.md 1.23 KB
一键复制 编辑 原始数据 按行查看 历史
Arvid 提交于 2022-08-26 15:16 . update

SignalR

身份验证和授权

坑:

在做signalr身份验证是传递token发现传递不过来.net 客户端 与 前端 wss 传值方式不一致

.net 客户端通过请求标头 Headers 传递

connection = new HubConnectionBuilder()
	// 1.可以选择通过QueryString方式传递,身份验证key要与前端一致。
   .WithUrl("https://localhost:7293/instantMessage?authorization=123", options =>
   {
       // 2.通过添加自定义请求标头方式传递
       options.Headers.Add("authorization", "123");
       // 3.通过添加初始化身份验证提供程序来进行传递,这里标头key为:authorization,可能存在请求标头冲突被重置掉。
       //options.AccessTokenProvider = () => Task.FromResult("ABCD");
   })
  .WithAutomaticReconnect()//自动重新连接                         
  .Build();

前端通过QueryString 传递

connection = new signalR.HubConnectionBuilder()
	// 通过accessTokenFactory 工厂方法来经行处理。这里的QueryString的key是:access_token
    .withUrl("https://localhost:7293/instantMessage",{ accessTokenFactory: () => 'abcdefgh' })
    .configureLogging(signalR.LogLevel.Trace)
    .withAutomaticReconnect()
    .build();
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/wuxinheng/docs.git
git@gitee.com:wuxinheng/docs.git
wuxinheng
docs
docs
master

搜索帮助

344bd9b3 5694891 D2dac590 5694891