2017-10-16 125 views
0

我正在使用OpenIddict進行身份驗證/授權。
我需要手動檢查訪問令牌並獲取該令牌後面的用戶(ClaimsPrincipal)。怎麼樣?OpenIddict:如何手動檢查訪問令牌並獲取身份

使用案例:
我正在使用SignalR。在客戶端的每個方法調用中,我想檢查用戶是否通過身份驗證。我的計劃是從Angular發送訪問令牌並在Hub方法中檢查它。在控制器上使用[Authorize]屬性時,基本上也必須發生同樣的事情。

回答

1

假設這個問題與How to authorize SignalR Core Hub method with JWT有關,我不建議你自己解密由OpenIddict發出的不透明訪問令牌。

如果你真的想自己做,你可以手動實例化一個TicketDataFormat實例與OpenIddict使用ASP.NET核心數據保護「的宗旨字符串」:

// Resolve the data protection provider from the DI container. 
// Depending on where this snippet is used, you must be able 
// to directly use constructor injection (e.g in a controller). 
var provider = app.ApplicationServices.GetRequiredService<IDataProtectionProvider>(); 

var protector = provider.CreateProtector(
    nameof(OpenIdConnectServerHandler), 
    nameof(OpenIdConnectServerOptions.AccessTokenFormat), 
    OpenIdConnectServerDefaults.AuthenticationScheme); 

var format = new TicketDataFormat(protector); 

// If the access token is not malformed, a non-null value 
// is returned. Note that you'll have to manually validate 
// the expiration date and the audience of the ticket. 
var ticket = format.Unprotect("your access token"); 
相關問題