Kirsten
Kirsten

Reputation: 18160

Azure.Identity.CredentialUnavailableException using middletier localhost for XAF Winforms

I want to upgrade my XAF 24.1.6 Winforms application to use middletier security and connect to a database in Azure.

I managed to get "Hello Middle Tier" working with the database. However the full application has many more features.

I copied the working Middle Tier to my full application but when I run it I get the following

I see the middle tier url has not quite loaded when I get the error, causing me to wonder if it is a timing issue.

Error Message

The call stack is

System.Private.CoreLib.dll!System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
Azure.Identity.dll!Azure.Identity.CredentialDiagnosticScope.FailWrapAndThrow(System.Exception ex, string additionalMessage, bool isCredentialUnavailable) Line 45
    at Azure.Identity\CredentialDiagnosticScope.cs(45)
Azure.Identity.dll!Azure.Identity.EnvironmentCredential.GetTokenImplAsync(bool async, Azure.Core.TokenRequestContext requestContext, System.Threading.CancellationToken cancellationToken) Line 87
    at Azure.Identity\EnvironmentCredential.cs(87)
Azure.Identity.dll!Azure.Identity.EnvironmentCredential.GetTokenAsync(Azure.Core.TokenRequestContext requestContext, System.Threading.CancellationToken cancellationToken) Line 79
    at Azure.Identity\EnvironmentCredential.cs(79)
Azure.Identity.dll!Azure.Identity.DefaultAzureCredential.GetTokenFromSourcesAsync(Azure.Core.TokenCredential[] sources, Azure.Core.TokenRequestContext requestContext, bool async, System.Threading.CancellationToken cancellationToken) Line 107
    at Azure.Identity\DefaultAzureCredential.cs(107)
Azure.Identity.dll!Azure.Identity.DefaultAzureCredential.GetTokenImplAsync(bool async, Azure.Core.TokenRequestContext requestContext, System.Threading.CancellationToken cancellationToken) Line 73
    at Azure.Identity\DefaultAzureCredential.cs(73)
Azure.Identity.dll!Azure.Identity.DefaultAzureCredential.GetTokenAsync(Azure.Core.TokenRequestContext requestContext, System.Threading.CancellationToken cancellationToken) Line 56
    at Azure.Identity\DefaultAzureCredential.cs(56)
Microsoft.Data.SqlClient.dll!Microsoft.Data.SqlClient.ActiveDirectoryAuthenticationProvider.AcquireTokenAsync(Microsoft.Data.SqlClient.SqlAuthenticationParameters parameters)
Microsoft.Data.SqlClient.dll!Microsoft.Data.SqlClient.SqlInternalConnectionTds.GetFedAuthToken.AnonymousMethod__1()
System.Private.CoreLib.dll!System.Threading.Tasks.Task<System.Threading.Tasks.Task<Microsoft.Data.SqlClient.SqlAuthenticationToken>>.InnerInvoke()
System.Private.CoreLib.dll!System.Threading.ExecutionContext.RunFromThreadPoolDispatchLoop(System.Threading.Thread threadPoolThread, System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state)
System.Private.CoreLib.dll!System.Threading.Tasks.Task.ExecuteWithThreadLocal(ref System.Threading.Tasks.Task currentTaskSlot, System.Threading.Thread threadPoolThread)
System.Private.CoreLib.dll!System.Threading.ThreadPoolWorkQueue.Dispatch()
System.Private.CoreLib.dll!System.Threading.PortableThreadPool.WorkerThread.WorkerThreadStart()

I have studied the help mentioned here and conclude my code is trying to authenticate a service principal using the JWT token set up in the middle tier.

The middle tier code has been copied from the working application to the problem application with adjustments to work with the different module project.

Thus the azure keys are the same.

I arent sure how to proceed troubleshooting this, other than perhaps to copy code gradually from the bad application into the good to see where it breaks.

Update Studying the Middle Tier Security documentation I see that the Middle Tier Security serveracts as a WebSocket service between the client application and the database server. I think I must be wrong about a service principal being involved. All the middle tier is doing is protecting the connection string.

Upvotes: 0

Views: 40

Answers (1)

Kirsten
Kirsten

Reputation: 18160

I had forgotten to Enable Just My Code. When I did that, the error was clear.

System.InvalidCastException: 'The AddSecuredEFCore method does not work in applications with Middle Tier Security. Use the AddEFCore method instead. In Multi-Tenant applications, ensure that the WithHostDbContext method is called with the isMiddleTier parameter set to true.'

I was able to find the method in the Win project and correct it.

Upvotes: 0

Related Questions