TestTokenCreator 类
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
负责创建用于单元测试实现的测试令牌的类,具体取决于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。 |