你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn

TokenCachePersistenceOptions 类

定义

控制令牌缓存存储的选项。

public class TokenCachePersistenceOptions
type TokenCachePersistenceOptions = class
Public Class TokenCachePersistenceOptions
继承
TokenCachePersistenceOptions
派生

示例

这是一个示例,展示了如何结合使用 TokenCachePersistenceOptions 和 AuthenticationRecord,以便在客户端应用程序执行时启用无提示身份验证。

const string TOKEN_CACHE_NAME = "MyTokenCache";
InteractiveBrowserCredential credential;
AuthenticationRecord authRecord;

// Check if an AuthenticationRecord exists on disk.
// If it does not exist, get one and serialize it to disk.
// If it does exist, load it from disk and deserialize it.
if (!File.Exists(AUTH_RECORD_PATH))
{
    // Construct a credential with TokenCachePersistenceOptions specified to ensure that the token cache is persisted to disk.
    // We can also optionally specify a name for the cache to avoid having it cleared by other applications.
    credential = new InteractiveBrowserCredential(
        new InteractiveBrowserCredentialOptions { TokenCachePersistenceOptions = new TokenCachePersistenceOptions { Name = TOKEN_CACHE_NAME } });

    // Call AuthenticateAsync to fetch a new AuthenticationRecord.
    authRecord = await credential.AuthenticateAsync();

    // Serialize the AuthenticationRecord to disk so that it can be re-used across executions of this initialization code.
    using var authRecordStream = new FileStream(AUTH_RECORD_PATH, FileMode.Create, FileAccess.Write);
    await authRecord.SerializeAsync(authRecordStream);
}
else
{
    // Load the previously serialized AuthenticationRecord from disk and deserialize it.
    using var authRecordStream = new FileStream(AUTH_RECORD_PATH, FileMode.Open, FileAccess.Read);
    authRecord = await AuthenticationRecord.DeserializeAsync(authRecordStream);

    // Construct a new client with our TokenCachePersistenceOptions with the addition of the AuthenticationRecord property.
    // This tells the credential to use the same token cache in addition to which account to try and fetch from cache when GetToken is called.
    credential = new InteractiveBrowserCredential(
        new InteractiveBrowserCredentialOptions
        {
            TokenCachePersistenceOptions = new TokenCachePersistenceOptions { Name = TOKEN_CACHE_NAME },
            AuthenticationRecord = authRecord
        });
}

// Construct our client with the credential which is connected to the token cache
// with the capability of silent authentication for the account specified in the AuthenticationRecord.
var client = new SecretClient(new Uri("https://myvault.vault.azure.net/"), credential);

构造函数

TokenCachePersistenceOptions()

控制令牌缓存存储的选项。

属性

Name

唯一标识 的名称 TokenCachePersistenceOptions

UnsafeAllowUnencryptedStorage

如果设置为 true,则如果没有 OS 级别用户加密可用,令牌缓存可能会保留为未加密的文件。 如果设置为 false,则令牌缓存将在没有 OS 级别用户加密可用的情况下引发 CredentialUnavailableException

适用于