简体   繁体   中英

Trouble connecting to Sql Azure Db from C# using Active Directory password

I am setting up an azure database to which the CRM 360 Data Export Service (DES) needs to connect. I can do this manually using SSMS and logging on using my azure account.

在此处输入图像描述

And then setting up the DES service account as an external provider.

CREATE USER DynamicsDataExportService FROM EXTERNAL PROVIDER

All good, however I wish to automate the process, so I need to logon to the service with in C# and connect and run the code that way.

I am running core 2.2 with System.Data.SqlClient 4.7 using the following code:

        var cs = "data source=tcp:dvzxfsdg-sql.database.windows.net,1433;initial catalog=dfsdfs-sqldb; Authentication=Active Directory Integrated";
        using (var connection = new SqlConnection(cs))
        {
            connection.Open();

Annoyingly It gives the error

"System.ArgumentException: 'Keyword not supported: 'authentication'.'".

I am pretty sure the mistake I am making is fairly basic, but I cannot seem to the get to the bottom of it. So any pointers are gratefully received.

You might want to try using the SQLConnectionStringBuilder to properly format your connection string. This helps avoid typos and issues you might be running into with a plaintext connection string. After that, you can actually set the AuthenticationMethod directly on the builder.

SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder ()
    {
        DataSource = "ServerName",
        InitialCatalog = "DatabaseName",
        etc...
    }

builder.Authentication = SqlAuthenticationMethod.ActiveDirectoryInteractive;
SqlConnection sqlConnection = new SqlConnection(builder.ConnectionString);

If the SQL db is an Azure SQL db, then you can view the overview page of the resource and find the "Connection Strings" blade as well. This might help troubleshoot the issue you're having with the connection string you're providing.

Check out this answer about building connection strings for more info

the problem here is Authentication=Active Directory Integrated in the connection string. If you want to use AD user, you should rather use:

"data source=tcp:dvzxfsdg-sql.database.windows.net,1433;initial catalog=dfsdfs-sqldb; Integrated Security=true ";

if you want to keep specifying user name and password:

"data source=tcp:dvzxfsdg-sql.database.windows.net,1433;initial catalog=dfsdfs-sqldb; User ID=myUsername;Password=myPassword; ";

I finally realised that dotnet core 2.0 did not support this type of logon, so eventually had to shell out to a framework version of the app. The same code worked perfectly then. Thank you to all for trying to help. Much appreciated.

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