我有以下简单的.NET脚本:
static void Main(string[] args)
{
var result1 = Login().Result;
var result2 = Login().Result;
}
public static async Task<AuthenticationResult> Login()
{
var options = new PublicClientApplicationOptions();
options.TenantId = "...";
options.ClientId = "...";
var app = PublicClientApplicationBuilder.CreateWithApplicationOptions(options)
.WithDefaultRedirectUri()
.Build();
AuthenticationResult result;
var accounts = await app.GetAccountsAsync();
IAccount account = accounts.FirstOrDefault();
var scopes = new string[] { "..." };
try
{
result = await app.AcquireTokenSilent(scopes, account).ExecuteAsync();
}
catch (MsalUiRequiredException)
{
result = await app.AcquireTokenInteractive(scopes).ExecuteAsync();
}
return result;
}
我希望第一个Login(...)调用执行交互式身份验证,然后第二个调用var account = await app.GetAccountsAsync()返回以前登录的帐户。但是,帐户没有任何项目。因此,令牌缓存似乎不属于这些调用。
在第一次通话中是否可以存储已登录帐户?不应该是自动的吗?
答案 0 :(得分:1)
我遇到了同样的问题,并且在使用ilspy对MSAL库进行了摸索之后,我发现缓存是自己存储在IPublicClientApplication
应用中的。
因此,如果您重复使用app
,您还将获得缓存的优势。