【发布时间】:2015-07-10 22:13:57
【问题描述】:
我有一个本机客户端应用程序,我正在尝试使用 AcquireTokenSilent(),因此没有提示用户信用。但是,经过多次(多次)尝试,我只是迷失了问题所在。
我创建了自己的 PrivateTokenCacheClass,它继承自 TokenCache 类。
这是我的代码:
public class PrivateTokenCacheClass : TokenCache
{
public string CacheFilePath = @"C:\Users\blahblah\Desktop\token.txt";
public AADTokenCache()
{
CacheFilePath = @"C:\Users\blahblah\Desktop\token.txt";
this.BeforeAccess = BeforeAccessNotification;
this.AfterAccess = AfterAccessNotification;
}
public void BeforeAccessNotification(TokenCacheNotificationArgs args)
{
this.Deserialize(File.ReadAllBytes(CacheFilePath));
}
public void AfterAccessNotification(TokenCacheNotificationArgs args)
{
if(this.HasStateChanged)
{
File.WriteAllBytes(CacheFilePath, this.Serialize());
this.HasStateChanged = false;
}
}
}
现在,在我的 AcquireToken 方法中,我这样做:
private static string acquireAuthToken()
{
PrivateTokenCacheClass pvtCache = new PrivateTokenCacheClass();
AuthenticationContext authContext = new AuthenticationContext(authority, pvtCache);
string token = authContext.AcquireTokenSilent(resourceUri, clientID, UserIdentifier.AnyUser).AccessToken.ToString();
}
token.txt 文件包含我之前(10 分钟前)使用此调用获得的令牌(纯文本):
string token = authContext.AcquireToken(resourceUri, clientID, redirectUri).AccessToken.ToString();
任何帮助将不胜感激!
【问题讨论】:
标签: c# access-token azure-active-directory adal