语言

TestTokenCreator 类

定义

负责创建用于单元测试实现的测试令牌的类,具体取决于Microsoft。IdentityModel 令牌验证。

public class TestTokenCreator
type TestTokenCreator = class
Public Class TestTokenCreator
继承
TestTokenCreator

示例

下面提供了一个示例,说明如何使用通用测试框架 Xunit 来利用此类。 但是,核心概念适用于使用任何框架进行单元测试。

该示例假设类 ClassWithMicrosoftIdentityModelDependency 公开 ValidateToken,该方法调用Microsoft。IdentityModel 库和 GetTokenValidationParameters,用于检索TokenValidationParameters测试中的代码实际使用。 请注意,请务必使用实际TokenValidationParameters验证,因为这样,单元测试才能实际确认验证是否存在差距(例如禁用某些重要验证,ValidateAudienceValidateIssuer等等)。

在下面的代码示例中,generateTokenToTest 应该是此类中的方法之一。

internal void AssertValidationException(Func{string} generateTokenToTest, Type innerExceptionType, string innerExceptionMessagePart)
{
    try
    {
        ClassWithMicrosoftIdentityModelDependency.ValidateToken(
            generateTokenToTest,
            ClassWithMicrosoftIdentityModelDependency.GetTokenValidationParameters());

        if (innerExceptionType != null || innerExceptionType != null)
            throw new TestException(
                string.Format(
                    "Expected an exception of type '{0}' containing '{1}' in the message.",
                    innerExceptionType,
                    innerExceptionMessagePart));
    }
    catch (Exception e)
    {
        Assert.Equal(typeof(SampleTestTokenValidationException), e.GetType());
        Assert.Equal(innerExceptionType, e.InnerException.GetType());

        if (!string.IsNullOrEmpty(innerExceptionMessagePart))
        {
            Assert.Contains(innerExceptionMessagePart, e.InnerException.Message);
        }
    }
}

[Fact]
public void TokenWithoutSignature()
{
    var testTokenCreator = new TestTokenCreator();
    AssertValidationException(
        testTokenCreator.CreateTokenWithNoSignature,
        typeof(ArgumentException),
        "IDX14111");
}

注解

Microsoft。IdentityModel.SampleTests.SampleTokenValidationClassTests 包含有关如何利用此类来验证依赖于Microsoft的普通令牌验证类的示例。IdentityModel 的令牌验证方法。

构造函数

名称 说明
TestTokenCreator()

负责创建用于单元测试实现的测试令牌的类,具体取决于Microsoft。IdentityModel 令牌验证。

属性

名称 说明
Audience

获取或设置在创建的令牌上标记的受众。

Issuer

获取或设置在创建的令牌上标记的颁发者。

SigningCredentials

获取或设置用于对创建的令牌进行签名的 SigningCredentials。

方法

名称 说明
CreateClaimsSetWithInstanceOverrides()

基于实例值创建一组默认声明。

CreateDefaultValidToken()

基于类IssuerAudience和SigningCredentials值创建默认的有效测试令牌。

CreateExpiredToken()

创建一个已过期的测试 JWS 令牌。

CreateJsonPayload(IDictionary<String,Object>)

基于传递 IDictionary<TKey,TValue>的声明创建 JSON 有效负载。

CreateNotYetValidToken()

创建尚无效的测试 JWS 令牌。

CreateToken(Dictionary<String,Object>)

基于传递 Dictionary<TKey,TValue>的创建令牌。

CreateToken(SecurityTokenDescriptor)

基于传递 SecurityTokenDescriptor的创建令牌。

CreateTokenDescriptorWithInstanceOverrides()

基于实例值创建默认值 SecurityTokenDescriptor 。

CreateTokenWithBadAudience()

创建一个测试 JWS 令牌,其受众与配置的实例值不匹配。

CreateTokenWithBadIssuer()

使用与配置的实例值不匹配的颁发者创建测试 JWS 令牌。

CreateTokenWithBadSignatureKey()

创建使用与配置的实例值不匹配的密钥签名的测试 JWS 令牌。

CreateTokenWithFutureIssuedAt()

创建一个将来颁发的测试 JWS 令牌。

CreateTokenWithInvalidSignature()

使用与有效负载不匹配的签名创建测试令牌。

CreateTokenWithMissingAudience()

创建包含缺失受众(aud)声明的测试 JWS 令牌。

CreateTokenWithMissingExpires()

创建缺少过期时间(exp)声明的测试 JWS 令牌。

CreateTokenWithMissingIssuedAt()

创建具有缺失的 IssuedAt(iat)声明的测试 JWS 令牌。

CreateTokenWithMissingIssuer()

创建包含缺失颁发者(iss)声明的测试 JWS 令牌。

CreateTokenWithMissingKey()

创建没有签名密钥的测试 JWS 令牌(即 alg=none、无签名)。

CreateTokenWithMissingNotBefore()

创建具有缺失 NotBefore (nbf) 声明的测试 JWS 令牌。

CreateTokenWithNoSignature()

创建不带任何签名的测试 JWS 令牌。

适用于