2017-10-16 5 views
0

인증/승인에 OpenIddict을 사용하고 있습니다.
수동으로 액세스 토큰을 확인하고 그 토큰 뒤에 사용자 (ClaimsPrincipal)를 가져와야합니다. 방법?OpenIddict : 수동으로 액세스 토큰을 확인하고 ID를 얻는 방법

사용 사례 :
SignalR을 사용하고 있습니다. 클라이언트에서 모든 메서드 호출에 나는 사용자가 인증되었는지 확인하고 싶습니다. 내 계획은 Angular에서 액세스 토큰을 보내 Hub 방법으로 확인하는 것입니다. 컨트롤러에서 [Authorize] 속성을 사용하면 기본적으로 같은 일이 발생해야합니다.

답변

1

이 질문이 How to authorize SignalR Core Hub method with JWT과 관련되어 있다고 가정 할 때 OpenIddict에서 발행 한 불투명 액세스 토큰을 해독하지 않는 것이 좋습니다.

// 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"); 
: 당신이 정말로 그것을 직접 수행 할 경우

, 당신은 수동으로 OpenIddict에서 사용하는 ASP.NET 코어 데이터 보호 "목적 문자열"와 TicketDataFormat 인스턴스를 인스턴스화 할 수 있습니다