简体   繁体   English

KeyVaultClient.AuthenticationCallback Delegate 的参数从何而来?

[英]Where do the parameters for KeyVaultClient.AuthenticationCallback Delegate come from?

I'm trying to call all the vaults in a subscription.我正在尝试调用订阅中的所有保险库。 The approach I'm using is this -我使用的方法是这样的 -

Controller控制器

var myClient = new Microsoft.Azure.KeyVault.KeyVaultClient(new KeyVaultClient.AuthenticationCallback(Helper.GetToken));
Microsoft.Azure.KeyVault.KeyVaultCredential test = new KeyVaultCredential(new KeyVaultClient.AuthenticationCallback(Helper.GetToken));

TokenCloudCredentials tokenCredentials = new TokenCloudCredentials("xxx", test.Token);

KeyVaultManagementClient client = new KeyVaultManagementClient(tokenCredentials);
VaultListResponse response = new VaultListResponse();

Helper帮手

public static async Task<string> GetToken(string authority, string resource, string scope)
{

  var clientId = ConfigurationManager.AppSettings["AuthClientId"];
  var clientRedirectURI = ConfigurationManager.AppSettings["AuthClientRedirectURI"];

  var context = new AuthenticationContext(authority, TokenCache.DefaultShared);

  result = await context.AcquireTokenAsync(resource, clientId, new Uri(clientRedirectURI), new PlatformParameters(PromptBehavior.Always)); 
 return result.AccessToken; 
}

For my controller "test.Token" always returns null but I can't help but think it may be from me not passing anything into Helper.Token in test.对于我的控制器“test.Token”总是返回 null,但我不禁想到这可能是因为我没有在测试中将任何东西传递给 Helper.Token。 I know that the Helper.Token essentially matches what the call back wants:我知道 Helper.Token 基本上匹配回调想要的内容:

public delegate Task<string> AuthenticationCallback(
string authority,
string resource,
string scope)

But where do I get authority, resource and scope from?但是我从哪里获得权限、资源和范围? Thanks!谢谢!

AuthenticationCallback is a delegate function and the value of authority/resource/scope is provide by SDK, we need to provide the delegate function to use these values to get the access token. AuthenticationCallback 是一个委托函数,authority/resource/scope 的值由 SDK 提供,我们需要提供委托函数来使用这些值来获取访问令牌。

If you are using a web app, your code will not work ,because you need to provide the client_secret or client_assertion during the oath process .如果您使用的是网络应用程序,您的代码将无法工作,因为您需要在宣誓过程中提供 client_secret 或 client_assertion。 And if you debug your application , you will find the GetToken function will not fire ,because you don't use that client to perform a query (or other operation).如果您调试应用程序,您会发现 GetToken 函数不会触发,因为您没有使用该客户端来执行查询(或其他操作)。 Please refer to below link for how to use Azure Key Vault from a Web Application :有关如何从 Web 应用程序使用 Azure Key Vault,请参阅以下链接:

https://docs.microsoft.com/en-us/azure/key-vault/key-vault-use-from-web-application https://docs.microsoft.com/en-us/azure/key-vault/key-vault-use-from-web-application

Please also click here which includes two video tutorials which helps you understand better .另请单击此处,其中包含两个视频教程,可帮助您更好地理解。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM