TestTokenCreator 類別
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
一個負責建立用於單元測試實作的測試標記的類別,依據 Microsoft。IdentityModel 代幣驗證。
public class TestTokenCreator
type TestTokenCreator = class
Public Class TestTokenCreator
- 繼承
-
TestTokenCreator
範例
以下舉例說明如何利用一個通用的測試框架 Xunit 來利用這個類別。 不過,核心概念仍適用於使用任何框架進行單元測試。
範例中想像一個類別 ClassWithMicrosoftIdentityModelDependency,該類別會暴露 ValidateToken,一個呼叫 Microsoft 的方法。IdentityModel 函式庫和 GetTokenValidationParameters 用來檢索TokenValidationParameters被測試程式碼,實際上是用到。 請注意,使用實 TokenValidationParameters 數很重要,因為這樣可以讓單元測試實際確認驗證是否有漏洞(例如某些重要的驗證被停用、 ValidateAudience等等 ValidateIssuer)。
在以下程式碼範例中,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 的憑證驗證方法。
建構函式
| 名稱 | Description |
|---|---|
| TestTokenCreator() |
一個負責建立用於單元測試實作的測試標記的類別,依據 Microsoft。IdentityModel 代幣驗證。 |
屬性
| 名稱 | Description |
|---|---|
| Audience |
在創建的代幣上標記或設定受眾。 |
| Issuer |
取得或設定發行者在所建立的代幣上蓋章。 |
| SigningCredentials |
取得或設定用於簽署所建立令牌的 SigningCredentials。 |