无法向Azure函数提供NuGet包源凭据

19

我有一个Azure函数,该函数依赖于一个私有包源。

我正在将一个名为nuget.config的文件复制到应用服务中,其内容如下:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <packageSources>
    <add key="MyPackageFeed" value="<package feed path>" />
  </packageSources>
  <packageSourceCredentials>
  <MyPackageFeed>
    <add key="Username" value="<first part of Hotmail address, before @ symbol>" />
    <add key="Password" value="<newly generated access token for username>" />
  </MyPackageFeed>
</packageSourceCredentials>
  <activePackageSource>
    <add key="All" value="(Aggregate source)" />
  </activePackageSource>
</configuration>

注意: 我使用我的Hotmail帐户电子邮件地址的第一部分作为用户名,因为这是我用于在其他地方(如Visual Studio等)进行身份验证的用户名。

这是我在Azure函数门户中日志中看到的内容:

2016-10-05T11:57:16.974 Restoring packages.
2016-10-05T11:57:16.974 Starting NuGet restore
2016-10-05T11:57:18.381 Restoring packages for D:\home\site\wwwroot\HttpTriggerSqlDb\project.json...
2016-10-05T11:57:19.322 Unable to load the service index for source <path to feed>
2016-10-05T11:57:19.322 The parameter is incorrect.

如果像 @brettsam 建议的那样将 Password 键更改为 ClearTextPassword,我现在会得到以下结果:

2016-10-05T14:03:04.479 Please provide credentials for: <path to feed>
2016-10-05T14:03:05.097 Unable to load the service index for source <path to feed>
2016-10-05T14:03:05.097 Response status code does not indicate success: 401 (Unauthorized).
2016-10-05T14:03:05.142 UserName: Password:

你得到的 Password 值是直接从你的源获取的令牌吗?还是在本地机器上使用 nuget.exe sources add|update 生成的? - brettsam
@brettsam 我从“Team Services”在线门户生成了令牌。 - Vinyl Warmth
您可以尝试在VSTS上创建替代凭据,然后使用nuget.exe sources add -name {您的源名称} -source {您的源URL} -username {替代用户名} -password {您的PAT}命令添加您的源。另一方面,似乎问题与凭据无关(无法加载源的服务索引),创建和使用Azure函数的详细步骤是什么? - starian chen-MSFT
2个回答

49

尝试使用key="ClearTextPassword"(而不是key="Password")。如果使用Password,NuGet会假定该值已加密并尝试对其进行解密。

例如,我在VSTS中创建了一个软件包源,然后创建了一个个人访问令牌并使用了它:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <packageSources>
    <add key="MyPrivateFeed" value="https://brettsam.pkgs.visualstudio.com/_packaging/stackoverflow/nuget/v3/index.json" />
  </packageSources>
  <activePackageSource>
    <add key="All" value="(Aggregate source)" />
  </activePackageSource>
  <packageSourceCredentials>
    <MyPrivateFeed>
      <add key="Username" value="brettsam" />
      <add key="ClearTextPassword" value="{PAT}" />
    </MyPrivateFeed>
  </packageSourceCredentials>
</configuration>

1
我刚刚尝试了一下,它可以工作。你能否重新发布一下你的nuget.config并确保你的PAT具有所有正确的“Packaging read”权限?最终,这是一个nuget配置问题,与Azure Functions没有完全相关,因此您应该能够在本地计算机上快速设置并进行测试,而不是通过Functions网站进行操作。这可能会帮助您缩小范围。 - brettsam
看起来我在创建PAT时遇到了问题,感谢你的所有帮助。 - Vinyl Warmth
3
如何加密PAT值? - Vista
@Vista,请检查我的回答:https://stackoverflow.com/a/77155887/3088349 - undefined

1
在我看来,有一种更好的方法来实现这个。
上面的答案并没有错,它是可行的。唯一的问题是PAT密钥被暴露了。
你可以使用NuGet命令行来完成这个任务。
如果你没有NuGet cli,安装它。
然后你可以运行以下命令,它将添加Azure DevOps feed,并自动在NuGet.Config中加密密码。
nuget sources add -name <Feed_Name> -source <Feed_URL> -username <Any_String_But_Not_Null> -password <Personal_Access_Token>

当你现在检查你的NuGet.Config文件时,你会发现它已经添加了一个加密的密码,而不是明文密码。
享受编码 ;)
用户名通常是你的组织名称,密码是你在Azure DevOps中的PAT。 enter image description here 参考:

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接