加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

SignalR(.NET Core)中的JWT身份验证,而不在查询字符串中传递令牌

发布时间:2020-12-15 22:46:45 所属栏目:asp.Net 来源:网络整理
导读:我在ASP.NET Core Web API应用程序中使用JWT身份验证令牌.令牌由API本身生成,而不是由第三方生成. 我将SignalR成功添加到堆栈中,但现在我需要验证正在尝试执行服务器(Hub)方法的用户. 有人建议在 JavaScript中的“qs”属性中传递令牌.这对我来说不行,因为我
我在ASP.NET Core Web API应用程序中使用JWT身份验证令牌.令牌由API本身生成,而不是由第三方生成.
我将SignalR成功添加到堆栈中,但现在我需要验证正在尝试执行服务器(Hub)方法的用户.
有人建议在 JavaScript中的“qs”属性中传递令牌.这对我来说不行,因为我们的令牌真的很大(它们包含很多索赔).
我尝试编写一个自定义中间件,从有效载荷读取令牌并自动验证用户.问题在于,当使用WebSockets时,中间件不被执行.
任何想法都会有所帮助.

解决方法

看看建议使用查询字符串 Authenticate against a ASP.NET Core 1.0 (vNext) SignalR application using JWT的文章.我知道你的令牌太长,但作者解释了如何使用中间件来验证请求.

以下是文章的总结:

  • SignalR does not have any special authentication mechanism built in,it is using the standard ASP.NET authentication.
  • JWT is typically sent in the Authorization header of a request
  • The SignalR JavaScript client library does not include the means to send headers in the requests,it does however allow you to pass a query string
  • If we pass the token in the query string we can write a middleware that adds a authorization header with the token as its value. This must be done before the Jwt middleware in the pipeline
  • Be aware of the “Bearer ” format

我已经突出了关键点,您的自定义中间件应该在Jwt中间件之前注册.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读