生产环境项目问题记录系列(二):Docker打包镜像Nuget包因权限问题还原失败
docker打包镜像遇到一个因为nuget权限验证问题导致镜像打包失败的问题,公司Nuget包用的是tfs管理的,tfs有权限验证,结果导致nuget还原失败,原有的NuGet.config文件如下:
<?xml version="1.0" encoding="utf-8"?> <configuration> <solution> <add key="disableSourceControlIntegration" value="true" /> </solution> <packageRestore> <add key="enabled" value="True" /> <add key="automatic" value="True" /> </packageRestore> <bindingRedirects> <add key="skip" value="False" /> </bindingRedirects> <packageSources> <add key="nuget.org" value="https://api.nuget.org/v3/index.json" protocolVersion="3" /> <add key="CompanyNuGet" value="*****" /> </packageSources> </configuration>
使用这个nuget配置在docker中还原遇到的第一个报错如下: ”Unable to load the service index for source ...”,OK很明显是说无法访问nuget地址,突然想到可能是权限问题,所以修改配置如下:
<?xml version="1.0" encoding="utf-8"?> <configuration> <solution> <add key="disableSourceControlIntegration" value="true" /> </solution> <packageRestore> <add key="enabled" value="True" /> <add key="automatic" value="True" /> </packageRestore> <bindingRedirects> <add key="skip" value="False" /> </bindingRedirects> <packageSources> <add key="nuget.org" value="https://api.nuget.org/v3/index.json" protocolVersion="3" /> <add key="CompanyNuGet" value="..." /> </packageSources> <packageSourceCredentials> <CompanyNuGet> <add key="Username" value="NugetReader" /> <add key="ClearTextPassword" value="i'm a secret!" /> </CompanyNuGet> </packageSourceCredentials> </configuration>
然后又报了第二个错:"/home/pi/dotnet/sdk/2.2.300/NuGet.targets(121,5): error : GSSAPI operation failed with error - An invalid status code was supplied (SPNEGO cannot find mechanisms to negotiate)."
看样子好像是验证失败,无奈网上找办法,找了半天找到了解决方案,首先我们需要在tfs中创建一个私人的Token来当做密码:
然后将创建的Token当做密码在NuGet中配置,还要再加一个重要的配置,表示使用基础验证:
<add key="ValidAuthenticationTypes" value="basic" />
最终的NuGet配置如下:
<?xml version="1.0" encoding="utf-8"?> <configuration> <solution> <add key="disableSourceControlIntegration" value="true" /> </solution> <packageRestore> <add key="enabled" value="True" /> <add key="automatic" value="True" /> </packageRestore> <bindingRedirects> <add key="skip" value="False" /> </bindingRedirects> <packageSources> <add key="nuget.org" value="https://api.nuget.org/v3/index.json" protocolVersion="3" /> <add key="private-nuget" value="nuget url" /> </packageSources> <packageSourceCredentials> <private-nuget> <add key="Username" value="NugetReader" /> <add key="ClearTextPassword" value="Token" /> <add key="ValidAuthenticationTypes" value="basic" /> </private-nuget> </packageSourceCredentials> </configuration>
然后镜像打包成功!
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步