Getting TypeLoadException while trying to use AppAuthentication for accessing SQL Database using Managed Identities.
Link with details on what I am trying to achieve -
https://docs.microsoft.com/en-us/azure/app-service/app-service-web-tutorial-connect-msi
Exception or Stack Trace
Exception: System.TypeInitializationException: 'The type initializer for 'System.Data.SqlClient.SqlAuthenticationProviderManager' threw an exception.'
InnerException (1 of 2): ArgumentException: Failed to instantiate an authentication provider with type 'Microsoft.Azure.Services.AppAuthentication.SqlAppAuthenticationProvider, Microsoft.Azure.Services.AppAuthentication' for 'ActiveDirectoryInteractive'.
InnerException (2 of 2): TypeLoadException: Could not load type 'Microsoft.Azure.Services.AppAuthentication.SqlAppAuthenticationProvider' from assembly 'Microsoft.Azure.Services.AppAuthentication'.
To Reproduce
Steps to reproduce the behavior:
Code Snippet
<SqlAuthenticationProviders>
<providers>
<add name="Active Directory Interactive" type="Microsoft.Azure.Services.AppAuthentication.SqlAppAuthenticationProvider, Microsoft.Azure.Services.AppAuthentication" />
</providers>
</SqlAuthenticationProviders>
Expected behavior
Application should be able to load SqlAppAuthenticationProvider, as per the documentation from AppAuthentication dll.
Setup (please complete the following information):
Information Checklist
Kindly make sure that you have added all the following information above and checkoff the required fields otherwise we will treat the issuer as an incomplete report
I am experiencing the same issue when trying to run in an Azure App Service using managed identity.
It is working fine when running locally using Active Directory account tied to my development environment.
Have also tried with the newest version 1.3.0 but still experiencing same issue.
Thanks for reporting the issue. I've tagged this so that the feature team should find it and take a look.
I am experiencing the same error "Could not load type 'Microsoft.Azure.Services.AppAuthentication.SqlAppAuthenticationProvider' from assembly 'Microsoft.Azure.Services.AppAuthentication'." Not sure what needs to be changed. It is failing locally as well.
I am experiencing the same error "Could not load type 'Microsoft.Azure.Services.AppAuthentication.SqlAppAuthenticationProvider' from assembly 'Microsoft.Azure.Services.AppAuthentication'."
I am unable to reproduce this issue anymore. Functionality seems to be working as expected in a new application I created. I do not have original test application to compare or validate this issue against. Closing the issue.
I am also receiving this error, 2 different projects. The first as an OData API I was updating to use managed identity connectivity to SQL DB. The other was a brand new SQL tutorial showing how to implement this connectivity. Not sure why this was closed when others are reporting the issue.
@elafnitzegger - I closed the issue as it seemed like issue was not reproducible anymore. I will try to upload the sample solution that is working for me. Would you be able to share the sample project where you are facing the issue to compare and get to root of this issue?
I will reopen the issue now for further troubleshooting and let admins decide on when to close.
Changing the web app target framwork to 4.7.2 seems to solve it.
The file sdk/mgmtcommon/AppAuthentication/Azure.Services.AppAuthentication/SqlAppAuthenticationProvider.cs only has contents if the .NET version is 4.7.2.
I'm not sure why 4.7.2 is required, but it should probably fail more gracefully if the target framework is wrong.
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.
#if net472
using System;
using System.Data.SqlClient;
using System.Threading.Tasks;
namespace Microsoft.Azure.Services.AppAuthentication
{
/// <summary>
/// An implementation of SqlAuthenticationProvider that implements Active Directory Interactive SQL authentication.
/// </summary>
public class SqlAppAuthenticationProvider : SqlAuthenticationProvider
{
/// <summary>
/// The principal used to acquire token. This will be of type "User" for local development scenarios, and "App" when client credentials flow is used.
/// </summary>
public Principal PrincipalUsed { get; private set; }
/// <summary>
/// If the userId parameter is a valid GUID, return an token provider connection string to use a user-assigned managed identity
/// </summary>
/// <param name="userId"></param>
/// <returns></returns>
internal static string GetConnectionStringByUserId(string userId)
{
if (Guid.TryParse(userId, out Guid unused))
{
return $"RunAs=App;AppId={userId}";
}
return default(string);
}
/// <summary>
/// Acquires an access token for SQL using AzureServiceTokenProvider with the given SQL authentication parameters.
/// </summary>
/// <param name="parameters">The parameters needed in order to obtain a SQL access token</param>
/// <returns></returns>
public override async Task<SqlAuthenticationToken> AcquireTokenAsync(SqlAuthenticationParameters parameters)
{
var appAuthParameters = new SqlAppAuthenticationParameters(parameters);
return await AcquireTokenAsync(appAuthParameters).ConfigureAwait(false);
}
/// <summary>
/// Acquires an access token for SQL using AzureServiceTokenProvider with the given SQL authentication parameters.
/// </summary>
/// <param name="parameters">The parameters needed in order to obtain a SQL access token</param>
/// <returns></returns>
internal async Task<SqlAuthenticationToken> AcquireTokenAsync(SqlAppAuthenticationParameters parameters)
{
string azureAdInstance = UriHelper.GetAzureAdInstanceByAuthority(parameters.Authority);
string connectionString = GetConnectionStringByUserId(parameters.UserId);
string tenantId = UriHelper.GetTenantByAuthority(parameters.Authority);
if (string.IsNullOrEmpty(azureAdInstance))
{
throw new ArgumentException("The Azure AD instance could not be parsed from the authority provided in SqlAuthenticationParameters");
}
if (string.IsNullOrEmpty(parameters.Resource))
{
throw new ArgumentException("A resource must be specified in SqlAuthenticationParameters");
}
AzureServiceTokenProvider tokenProvider = new AzureServiceTokenProvider(connectionString, azureAdInstance);
var authResult = await tokenProvider.GetAuthenticationResultAsync(parameters.Resource, tenantId).ConfigureAwait(false);
PrincipalUsed = tokenProvider.PrincipalUsed;
return new SqlAuthenticationToken(authResult.AccessToken, authResult.ExpiresOn);
}
/// <summary>
/// Implements virtual method in SqlAuthenticationProvider. Only Active Directory Interactive Authentication is supported.
/// </summary>
/// <param name="authenticationMethod">The SQL authentication method to check whether supported</param>
/// <returns></returns>
public override bool IsSupported(SqlAuthenticationMethod authenticationMethod)
{
return authenticationMethod == SqlAuthenticationMethod.ActiveDirectoryInteractive;
}
}
}
#endif
closing this isse. @sbhuttan do not hesitate to reach out if you need more help.
@loarabia please go ahead and close this issue
Thanks for working with Microsoft on GitHub! Tell us how you feel about your experience using the reactions on this comment.
I am also getting the same error, environment is mvc 5, asp.net with .net fx 4.8, and Microsoft.Azure.Services.AppAuthentication ver 1.5.0