Is it possible to migrate the SQL server authentication mode from SQL AUTHENTICATION to AZURE AD AUTHENTICATION, in .Net framework running App service uses Entity framework to connect SQL server ?
Asked
Active
Viewed 62 times
-1

Alberto Morillo
- 13,893
- 2
- 24
- 30

Vigneash Selvaraj
- 57
- 1
- 5
-
What do you mean by "migrate"? You aren't clear what you're goal or question is here. – Thom A Jul 24 '23 at 08:04
-
Could you please provide what you have tried and any error you got? – Bhavani Jul 24 '23 at 08:38
1 Answers
0
By default, Azure SQL allows SQL Authentication and Azure AD authentication, but for Azure AD Authentication to work you need to provision an Azure Active Directory administrator for your Azure SQL logical server in the Azure portal or by using PowerShell. Here you will find how to do that in the Azure Portal.
Once you have done the previous step, you can use token-based authentication for Azure SQL Database using Azure Active Directory (Azure AD). Below you will find an EF sample code on how to do the token-based authentication and you will find more details on this StackOverflow forum thread.
class Program
{
static void Main()
{
SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder();
builder["Data Source"] = "brucesqlserver.database.windows.net";
builder["Initial Catalog"] = "brucedb";
builder["Connect Timeout"] = 30;
string accessToken = TokenFactory.GetAccessToken();
if (accessToken == null)
{
Console.WriteLine("Fail to acuire the token to the database.");
}
using (SqlConnection connection = new SqlConnection(builder.ConnectionString))
{
try
{
connection.AccessToken = accessToken;
//working with EF
using (var model = new BruceDbContext(connection))
{
var users= model.Users.ToList();
Console.WriteLine($"Results:{Environment.NewLine}{JsonConvert.SerializeObject(users)}");
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
Console.WriteLine("Please press any key to stop");
Console.ReadKey();
}
}

Alberto Morillo
- 13,893
- 2
- 24
- 30
-
Can this be used for .NetFramework projects ? I hops the sample suits for .Net core – Vigneash Selvaraj Jul 25 '23 at 10:04
-
-
Another example using passwordless identties and EF Core https://learn.microsoft.com/en-us/azure/azure-sql/database/azure-sql-dotnet-entity-framework-core-quickstart?view=azuresql&tabs=visual-studio%2Cservice-connector%2Cportal#add-the-code-to-connect-to-azure-sql-database – Alberto Morillo Jul 25 '23 at 12:31
-
https://stackoverflow.com/questions/54187241/ef-core-connection-to-azure-sql-with-managed-identity – Alberto Morillo Jul 25 '23 at 12:32