2016-08-22 3 views
0

나는 signalR 서버를 가지고 있으며 클라이언트가 Azure AD로부터 얻을 OAuth 토큰의 유효성을 검사해야합니다. AuthorizeHubConnection 메서드에서이 작업을 수행하려고합니다. 나는 기본적으로이 작업을 수행이 http://geekswithblogs.net/shaunxu/archive/2014/05/27.aspx 시도 :이 항상 티켓에 null를 돌려줍니다SignalR에서 OAuth/AAD 토큰 검증

dataProtectionProvider = new DpapiDataProtectionProvider(); 
var secureDataFormat = new TicketDataFormat(dataProtectionProvider.Create()); 
// authenticate by using bearer token in query string 
var token = request.QueryString.Get(WebApiConfig.AuthenticationType); 
var ticket = secureDataFormat.Unprotect(token); 

var에 D. http://ronaldwildenberg.com/signalr-hub-authentication-with-adal-js-part-2/ 여기

그것이 무엇이다 : 검색을 조금 후

는이 기사를 통해 온

public class JwtTokenAuthorizeAttribute : AuthorizeAttribute 
{ 
    // Location of the federation metadata document for our tenant. 
    private const string SecurityTokenServiceAddressFormat = 
     "https://login.windows.net/{0}/federationmetadata/2007-06/federationmetadata.xml"; 

    private static readonly string Tenant = "yourtenant.onmicrosoft.com"; 
    private static readonly string ClientId = "12345678-ABCD-EFAB-1234-ABCDEF123456"; 

    private static readonly string MetadataEndpoint = string.Format(
     CultureInfo.InvariantCulture, SecurityTokenServiceAddressFormat, Tenant); 

    private static readonly IIssuerSecurityTokenProvider CachingSecurityTokenProvider = 
     new WsFedCachingSecurityTokenProvider(
      metadataEndpoint: MetadataEndpoint, 
      backchannelCertificateValidator: null, 
      backchannelTimeout: TimeSpan.FromMinutes(1), 
      backchannelHttpHandler: null); 

    public override bool AuthorizeHubConnection(
     HubDescriptor hubDescriptor, IRequest request) 
    { 
    // Extract JWT token from query string (which we already did). 
    ... 

    // Validate JWT token. 
    var tokenValidationParameters = 
     new TokenValidationParameters { ValidAudience = ClientId }; 
    var jwtFormat = 
     new JwtFormat(tokenValidationParameters, CachingSecurityTokenProvider); 
    var authenticationTicket = jwtFormat.Unprotect(userJwtToken); 

    ... 

이 가진 문제는 카타나 프로젝트에서 클래스를 복사하는 제안이다 : https://katanaproject.codeplex.com/SourceControl/latest#src/Microsoft.Owin.Security.ActiveDirectory/WsFedCachingSecurityTokenProvider.cs . 이것은보기 흉하게 보입니다. 또 다른 문제는 임차인 ID를 모르는 데다가 토큰을 가지고 어디에서나 찾을 수 없다는 것입니다. 그래서 이것이 작동하더라도, 나는 한 걸음 더 나아갈 것입니다.

마무리 방법 : SignalR을 사용하여 AzureAD 토큰의 유효성을 검사하는 방법을 찾고 싶습니다. 처음에는 단순한 것 같았습니다. 이것에 대한 simpe 방법이 있습니까?

답변

0

그것은 간단했다 :

JwtSecurityTokenHandler tokenHandler = new JwtSecurityTokenHandler(); 
tokenHandler.ValidateToken(token, authTokenValidationParameters, out validatedToken);