dotnet nuget add source命令未将凭据写入NuGet.config
解决NuGet配置文件未包含凭据的问题
问题原因
使用dotnet nuget add source命令添加带凭据的源时,即便加上--store-password-in-clear-text参数,凭据默认也不会写入当前目录的NuGet.config,而是存储在用户级别的NuGet配置文件中:
- Windows系统路径:
%APPDATA%\NuGet\NuGet.Config - Linux/macOS系统路径:
~/.nuget/NuGet/NuGet.Config
验证凭据是否已存储
执行以下命令查看源的详细信息,确认凭据是否已成功存储:
dotnet nuget list source -v
输出内容会显示对应源的凭据存储状态。
解决方法
方法1:手动编辑当前目录的NuGet.config
如果需要将凭据写入当前目录的配置文件(比如团队共享场景),手动添加<packageSourceCredentials>节点即可:
<?xml version="1.0" encoding="utf-8"?> <configuration> <packageSources> <!--To inherit the global NuGet package sources remove the <clear/> line below --> <clear /> <add key="nuget-remote" value="https://jfrog.io/artifactory/api/nuget/v3/nuget-virtual" /> </packageSources> <packageSourceCredentials> <nuget-remote> <add key="Username" value="sada@fi.com" /> <add key="ClearTextPassword" value="A35F261kIewr73fnmVE2WBb" /> </nuget-remote> </packageSourceCredentials> </configuration>
方法2:使用nuget.exe命令强制写入指定配置文件
使用官方nuget.exe(而非dotnet nuget)命令,通过-configfile参数指定将凭据写入当前目录的配置文件:
nuget sources add -name nuget-remote -source https://jfrog.io/artifactory/api/nuget/v3/nuget-virtual -username "sada@fi.com" -password "A35F261kIewr73fnmVE2WBb" -StorePasswordInClearText -configfile ./NuGet.config
注意事项
- 明文存储密码存在安全风险,仅建议在受控非生产环境或必要场景下使用。
- 若使用Windows系统,也可通过Visual Studio内的NuGet包管理器UI配置源,并选择将凭据存储到当前项目的配置文件中。
内容的提问来源于stack exchange,提问作者wehelpdox
相关产品推荐
相关产品推荐

