I am using Managed Identity to connect to Azure Service Bus and it used to work fine.
This morning, I realized that this approach wasn't working anymore locally (using Visual Studio) and also on the deployed application (using managed identity).
I have a custom token provider class:
public class AzureServicebusManagedIdentityTokenProvider : TokenProvider
{
private const string Resource = "https://servicebus.azure.net/";
protected readonly string TenantId;
public AzureServicebusManagedIdentityTokenProvider(string tenantId = null)
{
TenantId = string.IsNullOrWhiteSpace(tenantId) ? null : tenantId;
}
public override async Task<SecurityToken> GetTokenAsync(string appliesTo, TimeSpan timeout)
{
string accessToken = await GetAccessToken(Resource);
return new JsonSecurityToken(accessToken, appliesTo);
}
private async Task<string> GetAccessToken(string resource)
{
var authProvider = new AzureServiceTokenProvider();
return await authProvider.GetAccessTokenAsync(resource, TenantId);
}
}
Then for example to send a message:
var sbMessageSender = new MessageSender(new ServiceBusConnection("<my connectionstring>")
{
TransportType = TransportType.Amqp,
TokenProvider = new AzureServicebusManagedIdentityTokenProvider("<my tenant id>")
}, "my queue name", RetryPolicy.Default);
var json = JsonConvert.SerializeObject(<message to send>);
var message = new Message(Encoding.UTF8.GetBytes(json));
await sbMessageSender.SendAsync(message);
This error is thrown:
Put token failed. status-code: 401, status-description: InvalidIssuer: Token issuer is invalid. TrackingId:5c6c17c7-7a9e-49f3-adf7-5dbfb35b3daf, SystemTracker:NoSystemTracker, Timestamp:2019-10-29T08:56:17.
I've checked that I have 'Azure Service Bus Data Owner' role and that the 'Azure App authentication' tool in visual studio is set to the appropriate account.
I am using these nuget packages:
Microsoft.Azure.Services.AppAuthentication
v1.3.1Microsoft.Azure.ServiceBus
v3.4.0
Not sure If I am doing something stupid (as it used to work ) but any help would be appreciated.
TenantID
on Azure, too or is that just for using your account locally? Have you tried catching the token and pasting it in a tool like jwt.io to see who the issuer is (since that's the error message)? – rickvdboschQueueClient
and still the same. I will contact support. anyway thanks a lot for your help. at least we double checked that I haven't missed obvious stuff. – Thomas