Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/301.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 得到一个;无法转换";尝试使用缓存令牌时出错_C#_.net_Azure Ad Graph Api - Fatal编程技术网

C# 得到一个;无法转换";尝试使用缓存令牌时出错

C# 得到一个;无法转换";尝试使用缓存令牌时出错,c#,.net,azure-ad-graph-api,C#,.net,Azure Ad Graph Api,我在尝试使用缓存令牌时遇到以下错误。我使用提供的列表作为示例: 无法从“System.Collections.Generic.IEnumerable”转换为“Microsoft.Identity.Client.IAccount”` static async Task Auth() { var clientApp=PublicClientApplicationBuilder.Create(ConfigurationManager.AppSettings[“clientId”]).Build();

我在尝试使用缓存令牌时遇到以下错误。我使用提供的列表作为示例:

无法从“System.Collections.Generic.IEnumerable”转换为“Microsoft.Identity.Client.IAccount”`

static async Task Auth()
{
var clientApp=PublicClientApplicationBuilder.Create(ConfigurationManager.AppSettings[“clientId”]).Build();
字符串[]范围=新字符串[]{“user.read”};
字符串标记=null;
var app=PublicClientApplicationBuilder.Create(ConfigurationManager.AppSettings[“clientId]”).Build();
AuthenticationResult=null;
var accounts=wait app.GetAccountsAsync();
结果=等待应用程序。AcquireTokenSilent(范围、帐户)
.ExecuteAsync();
token=result.AccessToken;
GraphServiceClient graphClient=新GraphServiceClient(
"https://graph.microsoft.com/v1.0",
新的DelegateAuthenticationProvider(
异步(请求消息)=>
{
requestMessage.Headers.Authorization=新的AuthenticationHeaderValue(“承载者”,令牌);
}));
返回图形客户端;
}

导致错误的调用就是这个

result = await app.AcquireTokenSilent(scopes, accounts)
                  .ExecuteAsync();
AcquireTokenSilent
需要两个变量,
IEnumerable
(scops)和
string
(loginHint)。您为
loginHist
传递的是一个帐户数组,而它应该是单个字符串(单个帐户)

的返回类型是
IEnumerable
,这就是它在调用时抛出错误的原因

在一个实例中,查找的帐户来自列表

// Get the account
IAccount account = await application.GetAccountAsync(accountIdentifier).ConfigureAwait(false);

// Special case for guest users as the Guest oid / tenant id are not surfaced.
if (account == null)
{
    if (loginHint == null)
        throw new ArgumentNullException(nameof(loginHint));

    var accounts = await application.GetAccountsAsync().ConfigureAwait(false);
    account = accounts.FirstOrDefault(a => a.Username == loginHint);
}
将此行添加到代码中:

var accounts = await app.GetAccountsAsync();
IAccount account = accounts.FirstOrDefault(a => a.Username == loginHint); // <--- Get Account from accounts
result = await app.AcquireTokenSilent(scopes, account) // Use account instead of accounts.
                  .ExecuteAsync();
token = result.AccessToken;
var accounts=wait app.GetAccountsAsync();

IAccount account=accounts.FirstOrDefault(a=>a.Username==loginHint);//嗯,
IEnumerable
是遍历集合的工具,
IAccount
是单个项名称“loginHint”在当前上下文中不存在
loginHint应该是一个字符串变量。。您正在使用的用户名。请检查我发送的链接,该链接包含关于如何执行您尝试执行的操作的完整代码
// Get the account
IAccount account = await application.GetAccountAsync(accountIdentifier).ConfigureAwait(false);

// Special case for guest users as the Guest oid / tenant id are not surfaced.
if (account == null)
{
    if (loginHint == null)
        throw new ArgumentNullException(nameof(loginHint));

    var accounts = await application.GetAccountsAsync().ConfigureAwait(false);
    account = accounts.FirstOrDefault(a => a.Username == loginHint);
}
var accounts = await app.GetAccountsAsync();
IAccount account = accounts.FirstOrDefault(a => a.Username == loginHint); // <--- Get Account from accounts
result = await app.AcquireTokenSilent(scopes, account) // Use account instead of accounts.
                  .ExecuteAsync();
token = result.AccessToken;