2016-09-01 69 views
1

有没有办法在web api项目中读取/解密不记名令牌?在Web API中解密承载令牌

我的web api还托管着通过websocket从浏览器调用的SignalR集线器。 与我的正常api调用不同,我无法在此添加授权标头。虽然我可以在查询字符串中发送令牌并在SignalR中心读取它。

默认情况下,令牌由owin解析为声明标识。我需要的是手动执行此操作。我会怎么做?

OAuthAuthorizationServerOptions serverOptions = new OAuthAuthorizationServerOptions() 
    { 
     AllowInsecureHttp = true, 
     TokenEndpointPath = new PathString("/token"), 
     AccessTokenExpireTimeSpan = TimeSpan.FromDays(Config.TokenLifetime), 
     Provider = new AuthProvider() 
    }; 

    // Token Generation 
    app.UseStageMarker(PipelineStage.Authenticate); // wait for authenticate stage, so we get the windows principle for use with ntlm authentication 
    app.UseOAuthBearerAuthentication(new OAuthBearerAuthenticationOptions()); 
    app.UseOAuthAuthorizationServer(serverOptions); 
+0

您使用OAuthBearerAuthenticationOptions生成令牌的连接? –

+1

一种方法是将身份验证提供程序存储在owin启动类中的静态变量中,并在您需要从令牌获取声明时调用它。 –

+0

@ Nikola.Lukovic:是的 –

回答

2

我认为在Startup.cs你有一个类似的代码:

var oAuthOpt = new OAuthBearerAuthenticationOptions 
{ 
    Provider = new OAuthTokenProvider(
     req => req.Query.Get("bearer_token"), 
     req => req.Query.Get("access_token"), 
     req => req.Query.Get("refresh_token"), 
     req => req.Query.Get("token"), 
     req => req.Headers.Get("X-Token")) 
}; 

app.UseOAuthBearerAuthentication(OAuthOpt); 

app.UseOAuthAuthorizationServer(new OAuthAuthorizationServerOptions 
{ 
    AllowInsecureHttp = true, 
    TokenEndpointPath = new PathString(settings.TokenEndpointBasePath), 
    AccessTokenExpireTimeSpan = Util.AccessTokenExpireTimeSpan, 
    Provider = new AuthorizationServerProvider(new AuthenticationService()), 
}); 

,你所要做的就是在Startup.cs与公共静态字段来代替oAuthOpt比使用它时,你需要解除您的持票人代币保护。

对于SignalR我创建了一个授权属性,我在那里采取oAuthOpt并使用它解码令牌。

这是我要做的事:

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, Inherited = false, AllowMultiple = false)] 
public sealed class AuthorizeHubAttribute : AuthorizeAttribute 
{ 
    public override bool AuthorizeHubConnection (HubDescriptor hubDescriptor, IRequest request) 
    { 
     var token = request.QueryString["Authorization"]; 
     var ticket = Startup.OAuthOpt.AccessTokenFormat.Unprotect(token); 
     if (ticket != null && ticket.Identity != null && ticket.Identity.IsAuthenticated) 
     { 
      request.Environment["server.User"] = new ClaimsPrincipal(ticket.Identity); 
      return true; 
     } 
     else 
      return false; 
    } 

    public override bool AuthorizeHubMethodInvocation (IHubIncomingInvokerContext hubIncomingInvokerContext, bool appliesToMethod) 
    { 
     var connectionId = hubIncomingInvokerContext.Hub.Context.ConnectionId; 
     var environment = hubIncomingInvokerContext.Hub.Context.Request.Environment; 
     var principal = environment["server.User"] as ClaimsPrincipal; 
     if (principal != null && principal.Identity != null && principal.Identity.IsAuthenticated) 
     { 
      hubIncomingInvokerContext.Hub.Context = new HubCallerContext(new Microsoft.AspNet.SignalR.Owin.ServerRequest(environment), connectionId); 
      return true; 
     } 
     else 
      return false; 
    } 
} 

VAR票= Startup.OAuthOpt.AccessTokenFormat.Unprotect(令牌);

这条线是Startup.cs

+0

感谢您的解决方案!不幸的是,它并不完全适用于我:在AuthorizeHubConnection中,由于某种原因,票证为空。但是,令牌可以正确地从请求中提取出来。您的令牌字符串如何在上面的行中看起来像?我的格式是'Bearer ' –

+1

你可以尝试从字符串中移除“Bearer”并将它传递给Unprotect方法 –

+0

好吧,我只是截断了Bearer部分,现在它的工作原理!太感谢了! –