2016-09-01 118 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部分,現在它的工作原理!太感謝了! –