WSL Ubuntu 18.04执行dotnet restore遇NU1301错误,如何解决?
解决WSL中
dotnet restore无法加载私有NuGet源的问题 问题原因
该错误是因为WSL环境无法访问微软内部的Azure DevOps私有NuGet源,通常是缺少访问凭据或网络配置(代理/连通性)问题导致的。
解决方案
1. 测试网络连通性
先确认WSL能访问目标NuGet源:
# 替换[FeedName]为实际的Feed名称 curl -I https://microsoftit.pkgs.visualstudio.com/_packaging/[FeedName]/nuget/v3/index.json
- 返回
401 Unauthorized:网络连通但缺少权限凭据,跳至步骤2。 - 返回无法连接/超时:网络问题,跳至步骤3。
2. 配置私有NuGet源凭据
针对Azure DevOps私有源,需使用个人访问令牌(PAT)认证:
方法一:通过dotnet命令配置
# 添加源(替换[FeedName]) dotnet nuget add source https://microsoftit.pkgs.visualstudio.com/_packaging/[FeedName]/nuget/v3/index.json --name MicrosoftITPrivateFeed # 更新源凭据(替换用户名和PAT,PAT需拥有"Packaging Read"权限) dotnet nuget update source MicrosoftITPrivateFeed --username your-azure-devops-username --password your-personal-access-token --store-password-in-clear-text
方法二:修改NuGet.config文件
编辑WSL用户目录下的NuGet配置文件~/.nuget/NuGet/NuGet.config,添加以下内容(替换占位符):
<?xml version="1.0" encoding="utf-8"?> <configuration> <packageSources> <add key="MicrosoftITPrivateFeed" value="https://microsoftit.pkgs.visualstudio.com/_packaging/[FeedName]/nuget/v3/index.json" /> </packageSources> <packageSourceCredentials> <MicrosoftITPrivateFeed> <add key="Username" value="your-azure-devops-username" /> <add key="ClearTextPassword" value="your-personal-access-token" /> </MicrosoftITPrivateFeed> </packageSourceCredentials> </configuration>
3. 配置WSL代理(如果Windows需代理访问内部网络)
如果Windows系统通过代理访问内部资源,需为WSL设置代理:
# 临时生效(替换为你的代理地址和端口) export HTTP_PROXY=http://proxy.example.com:8080 export HTTPS_PROXY=http://proxy.example.com:8080 export NO_PROXY=localhost,127.0.0.1,.microsoft.com,.visualstudio.com # 永久生效:将上述命令添加到~/.bashrc或~/.zshrc末尾,然后执行 source ~/.bashrc
4. 验证配置并重试
执行以下命令确认源配置正确:
dotnet nuget list source
然后重新执行:
dotnet restore
内容的提问来源于stack exchange,提问作者Amit Agrawal
相关产品推荐
相关产品推荐

