如何在dotnet构建中屏蔽默认的公共NuGet仓库nuget.org
我正在尝试阻止所有构建使用默认的公开NuGet仓库。
下面是我尝试测试过的配置,但没有达到期望的结果:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<packageSources>
<clear />
<add key="Nuget Repository" value="https://nexus.sample.com/repository/nuget.org-proxy/index.json" protocolVersion="3" />
</packageSources>
<fallbackPackageFolders>
<clear />
</fallbackPackageFolders>
<packageSourceMapping>
<clear />
</packageSourceMapping>
<auditSources>
<clear />
</auditSources>
<config>
<add key="restoreSources" value="https://nexus.sample.com/repository/nuget.org-proxy/index.json" protocolVersion="3" />
</config>
</configuration>
但是当我尝试运行 dotnet restore . 时,似乎仍然同时指向nexus和 nuget.org:
2026-03-19T11:33:12.9932936Z info : Adding PackageReference for package 'Newtonsoft.Json' into project '/mnt/vss/_work/1/s/NuGetSourceTest/NuGetSourceTest.csproj'.
2026-03-19T11:33:13.3623538Z info : GET https://nexus.sample.com/repository/nuget.org-proxy-v3/v3/registration/5/newtonsoft.json/index.json
2026-03-19T11:33:13.3624203Z info : GET https://api.nuget.org/v3/registration5-gz-semver2/newtonsoft.json/index.json
2026-03-19T11:33:13.3752863Z info : OK https://nexus.sample.com/repository/nuget.org-proxy-v3/v3/registration/5/newtonsoft.json/index.json 12ms
2026-03-19T11:33:13.4715883Z info : OK https://api.nuget.org/v3/registration5-gz-semver2/newtonsoft.json/index.json 109ms
2026-03-19T11:33:13.5649146Z info : Restoring packages for /mnt/vss/_work/1/s/NuGetSourceTest/NuGetSourceTest.csproj...
2026-03-19T11:33:13.6195715Z info : GET https://api.nuget.org/v3-flatcontainer/newtonsoft.json/index.json
2026-03-19T11:33:13.6457833Z info : GET https://nexus.sample.com/repository/nuget.org-proxy-v3/v3/content/0/newtonsoft.json/index.json
2026-03-19T11:33:13.6572790Z info : OK https://nexus.sample.com/repository/nuget.org-proxy-v3/v3/content/0/newtonsoft.json/index.json 11ms
2026-03-19T11:33:13.6746362Z info : GET https://nexus.sample.com/repository/nuget.org-proxy-v3/v3/content/0/newtonsoft.json/13.0.4/newtonsoft.json.13.0.4.nupkg
2026-03-19T11:33:13.6857291Z info : OK https://nexus.sample.com/repository/nuget.org-proxy-v3/v3/content/0/newtonsoft.json/13.0.4/newtonsoft.json.13.0.4.nupkg 11ms
2026-03-19T11:33:13.7315254Z info : OK https://api.nuget.org/v3-flatcontainer/newtonsoft.json/index.json 111ms
2026-03-19T11:33:14.2220303Z info : Installed Newtonsoft.Json 13.0.4 from
2026-03-19T11:33:14.2353684Z info : GET https://api.nuget.org/v3/vulnerabilities/index.json
2026-03-19T11:33:14.2377678Z info : GET https://api.nuget.org/v3/vulnerabilities/index.json
2026-03-19T11:33:14.2378204Z info : OK https://api.nuget.org/v3/vulnerabilities/index.json 2ms
2026-03-19T11:33:14.2429360Z info : GET https://api.nuget.org/v3-vulnerabilities/2026.03.19.05.41.43/vulnerability.base.json
2026-03-19T11:33:14.2437575Z info : GET https://api.nuget.org/v3-vulnerabilities/2026.03.19.05.41.43/2026.03.19.05.41.43/vulnerability.update.json
解决方案
默认的NuGet仓库是:https://api.nuget.org/v3/index.json
默认的NuGet配置文件是C:\Users{User}\AppData\Roaming\NuGet\NuGet.Config
所以你必须先修改它。或者你也可以通过在解决方案根文件夹放置NuGet.Config文件来覆盖此配置。要查看实际应用了哪些配置,请在解决方案上右键点击 -> 为解决方案管理NuGet包 -> 右上角有齿轮图标。
备选方案
如果你在使用 DotNetCoreCLI@2 ,你可以尝试使用 includeNuGetOrg 并将其设置为 false
includeNuGetOrg: true #布尔值。可选。当selectOrConfig = select且 command = restore时使用。使用来自NuGet.org的包。默认值:true
站内所有文章版权归属LeftHeroAI导航站,无授权禁止任何主体转载、抄袭、复制内容,亦不得私自架设镜像站点。一经侵权,本站将通过法律途径追责。