简体   繁体   English

SignalR 获取服务器上的不记名令牌

[英]SignalR get bearer token on Server

For a project, I have a C# asp.net core server and an Angular web client application.对于一个项目,我有一个 C# asp.net 核心服务器和一个 Angular Z2567A5EC9705EB7AC2C98403 应用程序客户端。 I was following the SignalR documentation on bearer tokens.我正在关注关于不记名令牌的SignalR 文档 It states that in order to give the authentication token, you can pass it as follows (copy-paste of the documentation).它指出,为了提供身份验证令牌,您可以按如下方式传递它(文档的复制粘贴)。

let connection = new signalR.HubConnectionBuilder()
    .withUrl("/chathub", {
        accessTokenFactory: () => {
            // Get and return the access token.
            // This function can return a JavaScript Promise if asynchronous
            // logic is required to retrieve the access token.
        }
    })
    .build();

But now, I would like to extract this specific token on my C# server.但是现在,我想在我的 C# 服务器上提取这个特定的令牌。 The problem is, I can't find it anywhere.问题是,我在任何地方都找不到。 only the Context of type HubCallerContext is provided when a user connects in the method public override async Task OnConnectedAsync() or when a user invokes a method himself.当用户在public override async Task OnConnectedAsync()方法中连接或用户自己调用方法时,仅提供HubCallerContext类型的Context But this Context does not supply the given authentication token.但是此Context不提供给定的身份验证令牌。 How could I extract this?我怎么能提取这个?

As far as I know, if you want to get the token in the hub method, you could refer to below codes to get it.据我所知,如果你想在 hub 方法中获取令牌,你可以参考下面的代码来获取它。

 var accessToken = Context.GetHttpContext().Request.Query["access_token"];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM