WSL中执行dotnet restore访问Azure DevOps私有源遇401未授权错误
在WSL Ubuntu 22.04中解决dotnet restore访问Azure DevOps Server 2019私有源401未授权问题
问题场景
在WSL(Ubuntu 22.04)环境下运行ASP.NET Core应用,执行dotnet restore命令时,尝试从nuget.org和Azure DevOps Server 2019私有源还原NuGet包,已配置NuGet.config但始终触发401未授权错误。
已配置的NuGet.config
<?xml version="1.0" encoding="utf-8"?> <configuration> <packageSources> <add key="Nuget" value="https://api.nuget.org/v3/index.json"/> <add key="MyFeed" value="https://devops.myfeed.com/tfs/company/_packaging/CompanyPackages/nuget/v3/index.json"/> </packageSources> <packageSourceCredentials> <MyFeed> <add key="Username" value="DOMAIN/User"/> <add key="ClearTextPassword" value="PATforPackages"/> </MyFeed> </packageSourceCredentials> </configuration>
错误日志
1>/usr/lib/dotnet/sdk/8.0.107/NuGet.targets(156,5): error : Unable to load the service index for source https://devops.myfeed.com/tfs/company/_packaging/CompanyPackages/nuget/v3/index.json. ......... /usr/lib/dotnet/sdk/8.0.107/NuGet.targets(156,5): error : Response status code does not indicate success: 401 (Unauthorized). [/mnt/c/Users/..../MyProject.proj.csproj] Assembly loaded during TaskRun (NuGet.Build.Tasks.RestoreTask): System.Diagnostics.StackTrace, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a (location: /usr/lib/dotnet/shared/Microsoft.NETCore.App/8.0.7/System.Diagnostics.StackTrace.dll, MVID: 7d4e9037-cc6f-43b1-8255-6608ac20f6b1, AppDomain: [Default]) NuGet.Protocol.Core.Types.FatalProtocolException: Unable to load the service index for source https://devops.myfeed.com/tfs/company/_packaging/CompanyPackages/nuget/v3/index.json. ---> System.Net.Http.HttpRequestException: Response status code does not indicate success: 401 (Unauthorized). at System.Net.Http.HttpResponseMessage.EnsureSuccessStatusCode() at NuGet.Protocol.HttpSource.<>c__DisplayClass15_0`1.<<GetAsync>b__0>d.MoveNext() --- End of stack trace from previous location --- at NuGet.Common.ConcurrencyUtilities.ExecuteWithFileLockedAsync[T](String filePath, Func`2 action, CancellationToken token) at NuGet.Common.ConcurrencyUtilities.ExecuteWithFileLockedAsync[T](String filePath, Func`2 action, CancellationToken token) at NuGet.Protocol.HttpSource.GetAsync[T](HttpSourceCachedRequest request, Func`2 processAsync, ILogger log, CancellationToken token) at NuGet.Protocol.ServiceIndexResourceV3Provider.GetServiceIndexResourceV3(SourceRepository source, DateTime utcNow, ILogger log, CancellationToken token) --- End of inner exception stack trace --- at NuGet.Protocol.ServiceIndexResourceV3Provider.GetServiceIndexResourceV3(SourceRepository source, DateTime utcNow, ILogger log, CancellationToken token) at NuGet.Protocol.ServiceIndexResourceV3Provider.TryCreate(SourceRepository source, CancellationToken token) at NuGet.Protocol.Core.Types.SourceRepository.GetResourceAsync[T](CancellationToken token) at NuGet.Protocol.Providers.VulnerabilityInfoResourceV3Provider.TryCreate(SourceRepository source, CancellationToken token) at NuGet.Protocol.Core.Types.SourceRepository.GetResourceAsync[T](CancellationToken token) at NuGet.Commands.VulnerabilityInformationProvider.GetVulnerabilityInfoAsync() at NuGet.Commands.VulnerabilityInformationProvider.GetVulnerabilityInformationAsync(CancellationToken cancellationToken) at NuGet.Commands.Restore.Utility.AuditUtility.GetAllVulnerabilityDataAsync(CancellationToken cancellationToken) at NuGet.Commands.Restore.Utility.AuditUtility.CheckPackageVulnerabilitiesAsync(CancellationToken cancellationToken) at NuGet.Commands.RestoreCommand.PerformAuditAsync(EnabledValue enableAudit, IEnumerable`1 graphs, TelemetryActivity telemetry, CancellationToken token) at NuGet.Commands.RestoreCommand.PerformAuditAsync(EnabledValue enableAudit, IEnumerable`1 graphs, TelemetryActivity telemetry, CancellationToken token) at NuGet.Commands.RestoreCommand.ExecuteAsync(CancellationToken token) at NuGet.Commands.RestoreCommand.PerformAuditAsync(EnabledValue enableAudit, IEnumerable`1 graphs, TelemetryActivity telemetry, CancellationToken token) at NuGet.Commands.RestoreCommand.PerformAuditAsync(EnabledValue enableAudit, IEnumerable`1 graphs, TelemetryActivity telemetry, CancellationToken token) at NuGet.Commands.Restore.Utility.AuditUtility.CheckPackageVulnerabilitiesAsync(CancellationToken cancellationToken) at NuGet.Commands.RestoreCommand.ExecuteAsync(CancellationToken token) at NuGet.Commands.RestoreRunner.ExecuteAsync(RestoreSummaryRequest summaryRequest, CancellationToken token) at NuGet.Commands.RestoreRunner.ExecuteAsync(RestoreSummaryRequest summaryRequest, CancellationToken token) at NuGet.Commands.RestoreRunner.ExecuteAndCommitAsync(RestoreSummaryRequest summaryRequest, IRestoreProgressReporter progressReporter, CancellationToken token) at NuGet.Commands.RestoreRunner.CompleteTaskAsync(List`1 restoreTasks) at NuGet.Commands.RestoreRunner.RunAsync(IEnumerable`1 restoreRequests, RestoreArgs restoreArgs, CancellationToken token) at NuGet.Commands.RestoreRunner.RunAsync(RestoreArgs restoreContext, CancellationToken token) at NuGet.Build.Tasks.BuildTasksUtility.RestoreAsync(DependencyGraphSpec dependencyGraphSpec, Boolean interactive, Boolean recursive, Boolean noCache, Boolean ignoreFailedSources, Boolean disableParallel, Boolean force, Boolean forceEvaluate, Boolean hideWarningsAndErrors, Boolean restorePC, Boolean cleanupAssetsForUnsupportedProjects, ILogger log, CancellationToken cancellationToken) at NuGet.Build.Tasks.RestoreTask.ExecuteAsync(ILogger log)
解决方法
在NuGet.config的packageSourceCredentials节点下的MyFeed配置中添加valid-authentication-types项,指定认证类型为basic,同时确保使用Personal Access Token(PAT)作为密码。修改后的完整配置如下:
<?xml version="1.0" encoding="utf-8"?> <configuration> <packageSources> <add key="Nuget" value="https://api.nuget.org/v3/index.json"/> <add key="MyFeed" value="https://devops.myfeed.com/tfs/company/_packaging/CompanyPackages/nuget/v3/index.json"/> </packageSources> <packageSourceCredentials> <MyFeed> <add key="Username" value="DOMAIN/User"/> <add key="ClearTextPassword" value="PATforPackages"/> <add key="valid-authentication-types" value="basic" /> </MyFeed> </packageSourceCredentials> </configuration>
修改完成后重新执行dotnet restore命令,即可正常访问私有源完成包还原。
内容的提问来源于stack exchange,提问作者pampua84
相关产品推荐
相关产品推荐

