C# 验证信令器中的OAuth/AAD令牌

C# 验证信令器中的OAuth/AAD令牌,c#,oauth,signalr,jwt,adal,C#,Oauth,Signalr,Jwt,Adal,我有一个信号服务器,我需要验证客户端将从Azure AD获得的OAuth令牌。我想在AuthorizeHubConnection方法中进行验证。 我试过这个,基本上是这样的: 变量d 这将在票据中始终返回null 经过一番搜索,我发现了这篇文章: 它的作用如下: public class JwtTokenAuthorizeAttribute : AuthorizeAttribute { // Location of the federation metadata document for

我有一个信号服务器,我需要验证客户端将从Azure AD获得的OAuth令牌。我想在AuthorizeHubConnection方法中进行验证。 我试过这个,基本上是这样的: 变量d

这将在票据中始终返回null

经过一番搜索,我发现了这篇文章:

它的作用如下:

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);

    ...
问题是它建议从Katana项目复制类:。 这看起来超级难看。另一个问题是我不知道租户id,而且我在任何地方都找不到它。因此,即使这是可行的,我也离它只有一步之遥

总而言之:我想找到一种方法,用signar验证AzureAD令牌。一开始看起来很简单。有什么简单的方法吗?

很简单:

JwtSecurityTokenHandler tokenHandler = new JwtSecurityTokenHandler();
tokenHandler.ValidateToken(token, authTokenValidationParameters, out validatedToken);
很简单:

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