Skip to content

Can't authenticate with a different user using ActiveDirectoryInteractive #1532

Closed

Description

@slavergne-devolutions

For any two (or more) connection strings that connect to a Microsoft Azure SQL instance using Authentication=ActiveDirectoryInteractive; where the only difference between them is the User ID value (User ID=user1; vs User ID=User2;) then only the first user is allowed to connect. The second will loop through this UI forever, that is unless you click back and select User1.

User 2 trying to authenticate, click on User 2
image

User 2 stuck in a loop, click Next brings you back here
image

To reproduce

using System;
using Microsoft.Data.SqlClient;

private static string USER_1 = "[email protected]";
private static string USER_2 = "[email protected]";

private static string SERVER = "*********.database.windows.net";
private static string DATABASE = "*****";


void Main(string[] args)
{
	TestConnection(USER_1);
	TestConnection(USER_2);
}

private void TestConnection(string username)
{
    using (SqlConnection conn = new SqlConnection(GetConnectionString(SERVER, DATABASE, username)))
    {
        conn.Open();

        SqlCommand command = new SqlCommand("SELECT SUSER_NAME();", conn);
        Console.WriteLine(command.ExecuteScalar());
    }
}

private static string GetConnectionString(string server, string database, string user)
{
	SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder();
	builder["Data Source"] = server;
	builder["Initial Catalog"] = database;
	builder.UserID = user;
	builder.Authentication = SqlAuthenticationMethod.ActiveDirectoryInteractive;
	
	string result = builder.ConnectionString.ToString();
	Console.WriteLine(result);
	return result;
}

Output for the second user I passed in my privileged account ([email protected]) yet I wasn't able to connect

Data Source=*********.database.windows.net;Initial Catalog=*****;User [email protected];Authentication=ActiveDirectoryInteractive
[email protected]

Data Source=*********.database.windows.net;Initial Catalog=*****;User [email protected];Authentication=ActiveDirectoryInteractive
[email protected]

Further technical details

  • Microsoft.Data.SqlClient version: v4.1.0 (via NuGet)
  • .NET target: v5.0.14
  • Microsoft Azure SQL
  • Operating system: Windows 10

Expected behavior

We would expect to be able to authenticate with the user specified in the connection string, independent of other connections.

Work around (does not work, see next comment)

Yes, there is a work around. If you make both connection strings different with any other value other than User ID it works are expected.

Example, we added Connect Timeout=1001; & 1002 and now I can connect with both users.

Data Source=*********.database.windows.net;Initial Catalog=*****;User [email protected];Connect Timeout=1001;Authentication=ActiveDirectoryInteractive
[email protected]

Data Source=*********.database.windows.net;Initial Catalog=*****;User [email protected];Connect Timeout=1002;Authentication=ActiveDirectoryInteractive
[email protected]

Activity

  1. slavergne-devolutions commented on Mar 3, 2022

    @slavergne-devolutions
    Author

    Actually after further testing the work around is wrong, it doesn't change anything. It's one of the many tests I did but not the one that changed things and got things working.

    Here is what I think worked.

    Step 1 connection with *********.database.windows.net with both someuser & _someuser, _someuser will not work

    Data Source=*********.database.windows.net;Initial Catalog=*****;User [email protected];Authentication=ActiveDirectoryInteractive
    [email protected]
    
    Data Source=*********.database.windows.net;Initial Catalog=*****;User [email protected];Authentication=ActiveDirectoryInteractive
    [email protected]
    

    Step 2 connection with tcp:*********.database.windows.net,1433 with both _someuser & someuser both will work
    Note: Here we inversed the users, connect with _someuser first

    Data Source=tcp:*********.database.windows.net,1433;Initial Catalog=*****;User [email protected];Authentication=ActiveDirectoryInteractive
    [email protected]
    
    Data Source=tcp:*********.database.windows.net,1433;Initial Catalog=*****;User [email protected];Authentication=ActiveDirectoryInteractive
    [email protected]
    

    Step 3 try step 1 again, everything will work

    Once both are connected, you can change the connection string like you want, setting different connection timeout or whatever.
    image

  2. JRahnama commented on Mar 4, 2022

    @JRahnama
    Contributor

    @slavergne-devolutions we will look into that and get back to you.

  3. slavergne-devolutions commented on Mar 9, 2022

    @slavergne-devolutions
    Author

    I think you can close this ticket, all is working normally this morning with my test code. I have not changed anything whatsoever, not a line of code or the version of the NuGet. My guess is the issue was due to some odd Azure AD issue.

    I'm confirming with the client to make sure it works on their side.

  4. JRahnama commented on Mar 9, 2022

    @JRahnama
    Contributor

    @slavergne-devolutions thanks for the update. Closing the issue as resolved.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions