You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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.
---&gt; System.Net.Http.HttpRequestException: Response status code does not indicate success: 401 (Unauthorized).    at
System.Net.Http.HttpResponseMessage.EnsureSuccessStatusCode()     at
NuGet.Protocol.HttpSource.&lt;&gt;c__DisplayClass15_0`1.&lt;&lt;GetAsync&gt;b__0&gt;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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.20 11:05:58