当已经配置回复地址但应用程序未注册时,应用程序没有已注册的回复地址。

5
我正在制作一个WPF应用程序,它必须从Azure Key Vault获取密钥,但我总是遇到这个错误:

AADSTS500113:未为应用程序注册回复地址。

这是我使用的代码:

public class KeyProvider
{
    public string BaseUrl => "https://my-key-vault.vault.azure.net";
    public Uri RedirectUri => new Uri("urn:ietf:wg:oauth:2.0:oob");
    public KeyVaultClient KeyVaultClient { get; private set; }

    public KeyProvider()
    {
        KeyVaultClient = new KeyVaultClient(GetAccessToken);
    }

    public async Task<string> GetSecretAsync(string key)
    {
        SecretBundle secret = await KeyVaultClient.GetSecretAsync(BaseUrl, key);
        return secret.Value;
    }

    private async Task<string> GetAccessToken(string azureTenantId, string clientId, string redirectUri)
    {
        AuthenticationContext context = new AuthenticationContext(azureTenantId);
        AuthenticationResult tokenResult = await context.AcquireTokenAsync("https://vault.azure.net", clientId, RedirectUri, new PlatformParameters(PromptBehavior.RefreshSession));

        return tokenResult.AccessToken;
    }
}

当我调试GetAccessToken时,我发现redirectUri(从方法传递的参数)是一个空字符串。
这是我在Azure门户中的配置。

我错过了什么吗?

3个回答

4
在你的代码中,重定向uri是http协议。但是在你的应用程序中,它是https协议。请尝试将它们设置为相同的协议。

更新:

权限:

enter image description here

平台:

enter image description here

为密钥保管库中的用户添加访问策略。

enter image description here

代码:

class Program
{
    public static string clientId = "d4c9b2ed-****-****-****-30a787f7c928";
    public static string redirectUri = "https://localhost/";
    public static string baseUrl = "https://jackkv.vault.azure.net/";

    public static async Task<string> AuthenticationCallback(string authority, string resource, string scope)
    {
        var result = await new AuthenticationContext(authority).AcquireTokenAsync(resource, clientId, new Uri(redirectUri), new PlatformParameters(PromptBehavior.RefreshSession));
        return result.AccessToken;
    }


    static void Main(string[] args)
    {

        Console.ReadLine();

        KeyVaultClient kvc = new KeyVaultClient(AuthenticationCallback);
        var secret = kvc.GetSecretAsync(baseUrl, "testSecret").Result;

        Console.WriteLine(secret.Value);

        Console.ReadLine();
    }
}

结果:

enter image description here


0

如果有人遇到同样的问题,我在工作中尝试设置Active Directory登录时也遇到了这个问题。我多次与IT部门确认,在Azure App Services中redirectURI已正确配置。

结果发现我们需要将login.microsoft.com添加到允许的CORS列表中。在允许该站点后,应用程序按预期工作。


0

我找到了这个错误。我基于Stack Overflow上一个可能已过时的答案。这是我的代码:

public string ClientID => "01234567-89ab-cdef-0123-456789abcdef";

// skipped some lines...

private async Task<string> GetAccessToken(string authority, string resource, string redirectUri)
{
    AuthenticationContext context = new AuthenticationContext(authority);
    AuthenticationResult tokenResult = await context.AcquireTokenAsync(resource, ClientID, RedirectUri, new PlatformParameters(PromptBehavior.RefreshSession));

    return tokenResult.AccessToken;
}

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