gpt4 book ai didi

Azure Function : DefaultAzureCredential. GetTokenAsync 在本地运行时失败

转载 作者:行者123 更新时间:2023-12-02 06:01:57 29 4
gpt4 key购买 nike

我在 .NET 6 上有一个以 dotnet 隔离模式运行的 C# azure 函数。此函数调用另一个 azure 函数,该函数使用 Azure AD 身份验证。为了生成 token ,我有以下代码:

    var audience = $"api://{appRegistrationClientId}";           
var tokenCredential = new DefaultAzureCredential();
var token = await tokenCredential.GetTokenAsync(new TokenRequestContext(new[] { $"{audience}/.default" }) { });
var apiToken = token.Token;

return apiToken;

如果我将此代码部署到 Azure,该代码可以正常运行。我可以从其他功能应用程序调用和检索数据。但是,当在 Visual Studio 上本地运行时,出现以下异常:

Azure PowerShell authentication failed due to an unknown error. Seethe troubleshooting guide for more information.https://aka.ms/azsdk/net/identity/powershellcredential/troubleshootUnhandled exception. System.ArgumentException: Startup hook assembly'Microsoft.Azure.Functions.Worker.Core' failed to load. See innerexception for details.

---> System.IO.FileNotFoundException: Could not load file or assembly 'Microsoft.Azure.Functions.Worker.Core, Culture=neutral,PublicKeyToken=null'. The system cannot find the file specified.

File name: 'Microsoft.Azure.Functions.Worker.Core, Culture=neutral,PublicKeyToken=null'

atSystem.Reflection.RuntimeAssembly.InternalLoad(ObjectHandleOnStackassemblyName, ObjectHandleOnStack requestingAssembly,StackCrawlMarkHandle stackMark, Boolean throwOnFileNotFound,ObjectHandleOnStack assemblyLoadContext, ObjectHandleOnStackretAssembly)

at System.Reflection.RuntimeAssembly.InternalLoad(AssemblyNameassemblyName, RuntimeAssembly requestingAssembly, StackCrawlMark&stackMark, Boolean throwOnFileNotFound, AssemblyLoadContextassemblyLoadContext)

atSystem.Runtime.Loader.AssemblyLoadContext.LoadFromAssemblyName(AssemblyNameassemblyName)

at System.StartupHookProvider.CallStartupHook(StartupHookNameOrPathstartupHook)

--- End of inner exception stack trace ---

at System.StartupHookProvider.CallStartupHook(StartupHookNameOrPathstartupHook)

at System.StartupHookProvider.ProcessStartupHooks()

我尝试通过异常消息提供的链接进行故障排除,但没有任何效果。我可以调用 Get-AzAccessToken -ResourceUrl https://management.core.windows.net并在 powershell 中生成 token 。

我还在 Azure 服务身份验证 -> 帐户选择选项下将我的 Azure 帐户添加到 Visual Studio。

有没有办法让这个调用在本地工作,或者有什么解决方法。我确实需要能够从我的开发机器调用此函数才能测试我自己的代码。

最佳答案

当我收到此错误时,我必须重新启动 Visual Studio 2022 并重新输入 Azure 服务身份验证(包括 MFA)的凭据。

Visual Studio > 工具 > 选项 > Azure 服务身份验证

关于Azure Function : DefaultAzureCredential. GetTokenAsync 在本地运行时失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71563064/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com