简体   繁体   中英

Microsoft.AnalysisServices.AdomdClient doesn't work in .Net Core application (Authentication issue)

I've an issue using Microsoft.AnalysisServices.AdomdClient.NetCore.retail.amd64 library in.Net Core Web API application.

Fun fact : The same code works perfectly fine in.Net 4.7.2 application, but fails in.Net Core (currently I'm testing.Net 5 version).

The app has Windows authentication (IIS express) and uses connection string to Analysis service with Integrated Security=SSPI parameter. On connection.Open() method I'm getting an

Authentication Exception
"Unable to obtain authentication token using the credentials provided". Inner exception: "MsalServiceException": Federated service at url returned

error.

Code:

Startup.cs
services.AddAuthentication(IISDefaults.AuthenticationScheme);

Controller private method:

private DataTable GetData(string connectionString, string databaseName, string daxQuery, 
    Dictionary<string, string> parameters)
    {
    var dt = new DataTable();
    using(var conn = new AdomdConnection(connectionString))
    {
        //fails here
        conn.Open();
        conn.ChangeDatabase(databaseName);
        using(vad cmd = new AdomdCommand(daxQuery, conn))
        {
            if(parameters.Any())
            {
                foreach(var param in parameters)
                {
                    cmd.Parameters(param.Key, param.Value);
                }
            }
            using (var adapter = AdomDataAdapter(cmd))
            {
                adapter.Fill(dt);
            }
        }
        
        return dt;
    }
}

Update : I've tried the same code in console applications.

.Net 4.7.2 app works as expected.

.Net 4 fails with different error:

AcquireTokenByIntegratedWindowsAuth is not supported on .net core without adding.WithUserName() because MSAL cannot determine the username (UPN) of the currently logged in user. Please use.WithUserName() before calling ExecuteAsync(). For more details see https://github.com/AzureAD/microsoft-authentication-library-for-do.net/wiki/Integrated-Windows-Authentication

It seems that .net core version can't get username and use it to get Azure token for Analysis service authentication. Moreover, if I use MSAL library to get the token it is not clear how to use the token with AdomdClient library.

I've already written to library owners, but haven't get any response.

After investigation I've found that Microsoft.AnalysisServices.AdomdClient.NetCore.retail.amd64 has some hidden dependency. It works as expectected if you add Microsoft.Identity.Client nuget package to project.

Another possible solution - don't use AdomdClient and use OleDb connection:

using (var con = new OleDbConnection(connectionString))
{
    con.Open();
    con.ChangeDatabase(databaseName);
    using (var cmd = new OleDbCommand(query, con))
    {
        using (OleDbDataAdapter adapter = new OleDbDataAdapter(cmd))
        {
            // Here we should increase timeout because DAX query execution can be bigger than default 30 seconds
            adapter.SelectCommand.CommandTimeout = _timeoutInSeconds;
            adapter.Fill(dt);
        }
    }
    return dt;
}

OleDb works as well, you can query Tabular model without a problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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