从ASP.NET WebAPI 2中的标头和/或查询字符串中检索承载标记

Sob*_*han 4 signalr asp.net-web-api signalr-hub asp.net-identity

背景:

我有一个ASP.NET WebAPI项目.我正在使用Bearer Tokens来验证我的用户.我的一些控制器操作标有[Authorized]过滤器.在客户端,客户端通过调用http://foo.bar/Token然后将该令牌添加Authorization为其请求的标头来获取其令牌.

到目前为止没有问题,并且我的Startup.Auth.cs课程中的所有设置都应该正常工作:

OAuthOptions = new OAuthAuthorizationServerOptions
{
    TokenEndpointPath = new PathString("/Token"),
    Provider = new ApplicationOAuthProvider(PublicClientId),
    AuthorizeEndpointPath = new PathString("/api/Account/ExternalLogin"),
    AccessTokenExpireTimeSpan = TimeSpan.FromDays(14),
    // In production mode set AllowInsecureHttp = false
    AllowInsecureHttp = true                
};

// Enable the application to use bearer tokens to authenticate users
app.UseOAuthBearerTokens(OAuthOptions);
Run Code Online (Sandbox Code Playgroud)

现在我已经为我的项目添加了一些SignalR Hubs,我也想在集线器中验证用户身份.还有一些其他问题涉及客户端如何向SignalR连接添加承载令牌.摘要:

我试过的:

所以我创建了这个类来从查询字符串中检索我的访问令牌.

public class QueryStringOAuthBearerProvider : OAuthBearerAuthenticationProvider
{
    public override Task RequestToken(OAuthRequestTokenContext context)
    {
        var value = context.Request.Query.Get("access_token");

        if (!string.IsNullOrEmpty(value))
        {
            context.Token = value;
        }

        return Task.FromResult<object>(null);
    }
}
Run Code Online (Sandbox Code Playgroud)

然后在客户端,我这样做是为了传递我的访问令牌:

var connection = $.hubConnection();
var hub = connection.createHubProxy('fooHub');
connection.qs = { 'access_token': myAccessToken};
// Init connection
Run Code Online (Sandbox Code Playgroud)

这是一个想法,但是当我想QueryStringOAuthBearerProviderStartup.Auth.cs课堂上注册时,问题就出现了.

以下是我更改Startup.Auth.cs类的方法:

OAuthOptions = new OAuthAuthorizationServerOptions
{
    TokenEndpointPath = new PathString("/Token"),
    Provider = new ApplicationOAuthProvider(PublicClientId),
    AuthorizeEndpointPath = new PathString("/api/Account/ExternalLogin"),
    AccessTokenExpireTimeSpan = TimeSpan.FromDays(14),
    // In production mode set AllowInsecureHttp = false
    AllowInsecureHttp = true                
};

// Enable the application to use bearer tokens to authenticate users
app.UseOAuthBearerTokens(OAuthOptions);

// Enable the application to retrieve tokens from query string to authenticate users
app.UseOAuthBearerAuthentication(new OAuthBearerAuthenticationOptions()
{
    Provider = new QueryStringOAuthBearerProvider()
});
Run Code Online (Sandbox Code Playgroud)

所以我想我现在可以使用:

  1. 默认承载令牌,位于我的WebAPI调用请求的标头中.
  2. 自定义访问令牌,位于SignalR Hub的查询字符串中.

结果:

当我连接到我的集线器时,我可以从查询字符串中检索访问令牌,并对用户进行身份验证.但是,当我尝试调用WebAPI控制器操作时,我会System.InvalidOperationExceptionSequence contains more than one element.我认为这是因为我正在注册两种方式通过承载令牌验证我的用户,OWIN不喜欢这样.

题:

我怎样才能有两种方法来获得持票人令牌?

  • WebApi调用的默认行为(授权标头)
  • QueryStringOAuthBearerProvider SignalR Hubs(查询字符串).

请注意,我不想将访问令牌作为查询字符串传递给我的WebApi操作,而只是传递给SignalR Hub.

Sob*_*han 8

在盲目地寻找答案的同时,我遇到了一个与我的问题有点类似的帖子,并且在评论名称为mahmoud的评论中说道:

我发现了这个问题.而不是使用app.UseOAuthBearerTokens(OAuthOptions)申请app.UseOAuthAuthorizationServer(OAuthOptions).

它适用于我的情况,现在我可以使用这两种方法获得持票人令牌.这是我修改过的Startup.Auth.cs课程:

OAuthOptions = new OAuthAuthorizationServerOptions
{
    TokenEndpointPath = new PathString("/Token"),
    Provider = new ApplicationOAuthProvider(PublicClientId),
    AuthorizeEndpointPath = new PathString("/api/Account/ExternalLogin"),
    AccessTokenExpireTimeSpan = TimeSpan.FromDays(14),
    // In production mode set AllowInsecureHttp = false
    AllowInsecureHttp = true                
};

// Enable the application to use bearer tokens to authenticate users

//app.UseOAuthBearerTokens(OAuthOptions);   // Commented this line.

app.UseOAuthAuthorizationServer(OAuthOptions); // Added this line

// Enable the application to retrieve tokens from query string to authenticate users
app.UseOAuthBearerAuthentication(new OAuthBearerAuthenticationOptions()
{
    Provider = new QueryStringOAuthBearerProvider()
});
Run Code Online (Sandbox Code Playgroud)