Using Azure SQL database connection to a Managed Identity

Posts   
 
    
Posts: 7
Joined: 14-Aug-2018
# Posted on: 03-Oct-2018 19:59:28   

Is there are way to use a Azure SQL Database connection from an App Service using a managed identity?

The article below requires that you have access to the connection object to set an access token in order to use the managed identity.

https://docs.microsoft.com/en-us/azure/app-service/app-service-web-tutorial-connect-msi

Walaa avatar
Walaa
Support Team
Posts: 14950
Joined: 21-Aug-2005
# Posted on: 03-Oct-2018 23:57:16   

Is this question related to LLBLGen Pro?

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39613
Joined: 17-Aug-2003
# Posted on: 04-Oct-2018 16:16:00   

I think you mean this:

conn.AccessToken = (new AzureServiceTokenProvider()).GetAccessTokenAsync("https://database.windows.net/").Result;

?

so the ability to set that property? It's the easiest in adapter: create a partial class of the generated DataAccessAdapter class and override the CreateNewPhysicalConnection method. Then first call the base method to get the DbConnection instance, then cast it to SqlConnection and set the property with the value you want.

Frans Bouma | Lead developer LLBLGen Pro
Posts: 7
Joined: 14-Aug-2018
# Posted on: 04-Oct-2018 16:52:01   

Thank you everyone for your responses. Is there a way to do this in SelfServicing mode?

Walaa avatar
Walaa
Support Team
Posts: 14950
Joined: 21-Aug-2005
# Posted on: 04-Oct-2018 19:17:37   

Please try this: In the CommonDaoBase class, override CreateConnection as follows:

public override DbConnection CreateConnection(string connectionString)
{
    SqlConnection con = (SqlConnection)base.CreateConnection(connectionString);
    con.AccessToken= ....
    return con;
}
Posts: 7
Joined: 14-Aug-2018
# Posted on: 04-Oct-2018 19:44:32   

Great! Thank you!! I will try this out in the next several days and let you know how it went.

Posts: 7
Joined: 14-Aug-2018
# Posted on: 09-Oct-2018 22:23:56   

Thanks for the help. I implemented your suggested changes after setting up the Managed Identity and it works great. Here is the override code that I used. I put this in it's own file:

using Microsoft.Azure.Services.AppAuthentication; using SD.LLBLGen.Pro.ORMSupportClasses; using System.Data.Common; using System.Data.SqlClient;

namespace coetzeeModel.DaoClasses { public partial class CommonDaoBase : DaoBase {

    public override DbConnection CreateConnection(string connectionString)
    {
        SqlConnection con = (SqlConnection)base.CreateConnection(connectionString);
        con.AccessToken = (new AzureServiceTokenProvider()).GetAccessTokenAsync("https://database.windows.net/").Result;
        return con;
    }
}

}

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 10-Oct-2018 09:08:32   

Hi Jim,

Thanks for the feedback and the code sunglasses

David Elizondo | LLBLGen Support Team