【问题标题】:SignalR and OpenId ConnectSignalR 和 OpenId 连接
【发布时间】:2017-04-09 22:11:20
【问题描述】:

我有一个使用ASP.NET Core Web ApiOpenIddict 作为授权框架的服务器。现在我添加了一个SignalR 主机,并想为其添加授权。

different sources 我发现SignalR(JS 客户端)希望您在查询字符串中或通过 cookie 发送访问令牌,因为 websockets 不支持标头。

由于身份验证中间件不检查查询字符串或 cookie 容器中的授权条目,我需要实现这样一个提供者/检索器/解析器,它自己读取此值。

我找到了solution for IdentityServer,但没有找到OpenIddict

在哪里/如何使用OpenIddict 实现这样的令牌解析器?

【问题讨论】:

标签: asp.net-web-api asp.net-core oauth-2.0 openid openiddict


【解决方案1】:

如果你使用JwtBearerAuthentication,那么你可以使用OnMessageReceived来设置token:

Events = new JwtBearerEvents()
{
   OnMessageReceived = async (ctx) =>
   {
        ctx.Token = ctx.Request.Query["<qs-name>"];
   }
}

或者如果您使用IdentityServerAuthentication,那么您可以使用TokenRetriever(未测试但应该是这样的):

   TokenRetriever = (ctx) =>
   {
        return ctx.Request.Query["<qs-name>"];
   }

【讨论】:

    【解决方案2】:

    就像@adem-caglin 提到的那样,在 IdentityserverAuthentication 中,您使用 TokenRetriever 并且如果您追求的是标准不记名标头或查询字符串,则可以使用内置函数

    TokenRetriever = (request) => 
    {
        // by default calls TokenRetrieval.FromAuthorizationHeader()(request);
        // check if request is to signalr endpoint and only then apply FromQueryString
        return TokenRetrieval.FromQueryString()(request);
    }
    

    【讨论】:

      猜你喜欢
      • 2016-12-06
      • 2017-10-17
      • 2015-01-27
      • 2014-01-11
      • 1970-01-01
      • 2011-01-26
      • 2016-08-31
      • 2020-11-10
      相关资源
      最近更新 更多