如何将Azure MSI令牌添加到EF6 DbContext中以连接Azure SQL PaaS

pexxcrt2  于 2023-11-21  发布在  其他
关注(0)|答案(3)|浏览(118)

使用MVC和EF6代码优先方法,我能够集成Azure MSI令牌并执行CRUD操作,但我如何执行迁移,我必须将令牌注入DBContext:
连接字符串:

对于执行CRUD查询,我使用遗留的ADO.NET风格查询,如下所示,它可以工作:

获取MSI:

为了运行迁移,如何将Azure MSI令牌传递到DbContext constructor.

对于dbcontext,我必须定义具有提供程序名称的单独连接字符串。

kiayqfof

kiayqfof1#

您需要在DI中设置:
Startup.cs

public void ConfigureServices(IServiceCollection services)
    {
        //code ignored for simplicity
        services.AddDbContext<AzureProvider>();

        services.AddTransient<IDBAuthTokenService, AzureSqlAuthTokenService>();
    }

字符串
DbContext

public partial class AzureProvider: DbContext
{
    public IConfiguration Configuration { get; }
    public IDBAuthTokenService authTokenService { get; set; }

    public AzureProvider(IConfiguration configuration, IDBAuthTokenService tokenService, DbContextOptions<AzureProvider> options)
        : base(options)
    {
        Configuration = configuration;
        authTokenService = tokenService;
    }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        SqlConnection connection = new SqlConnection();
        connection.ConnectionString = Configuration.GetConnectionString("defaultConnection");
        connection.AccessToken = authTokenService.GetToken().Result;

        optionsBuilder.UseSqlServer(connection);
    }
}

public class AzureSqlAuthTokenService : IDBAuthTokenService
{
    public async Task<string> GetToken()
    {
        AzureServiceTokenProvider provider = new AzureServiceTokenProvider();
        var token = await provider.GetAccessTokenAsync("https://database.windows.net/");

        return token;
    }
}


EF Core Connection to Azure SQL with Managed Identity

xzlaal3s

xzlaal3s2#

这就是我如何使用Entity Framework 6和.NET Framework为遇到这个问题的任何人进行Azure SQL的AAD令牌身份验证。

public class MyContext : DbContext
{
    public MyContext() : base(CustomAzureSQLAuthProvider.GetTokenConnection("MyConnectionStringName"), true)
    { 
    }
   
    //...
}

public class CustomAzureSQLAuthProvider
{
    private static readonly string[] azureSqlScopes = new[]
    {
        "https://database.windows.net//.default"
    };

    private static readonly TokenCredential credential = new DefaultAzureCredential();

    public static DbConnection GetTokenConnection(string connectionStringName)
    {
        var connectionStringSettings = ConfigurationManager.ConnectionStrings[connectionStringName];

        var dbConnection = DbProviderFactories
            .GetFactory(connectionStringSettings.ProviderName)
            .CreateConnection();
        dbConnection.ConnectionString = connectionStringSettings.ConnectionString;

        var tokenRequestContext = new TokenRequestContext(azureSqlScopes);
        var tokenResult = credential.GetToken(tokenRequestContext, default);
        SqlConnection sqlConnection = dbConnection as SqlConnection;
        sqlConnection.AccessToken = tokenResult.Token;
        return sqlConnection;
    }
}

字符串
注意:我不得不使用非pixecGetToken函数。它不能与GetTokenAsync一起工作。

inb24sb2

inb24sb23#

我能够使用Azure.Identity来检索令牌:

var tokenRequestContext = new TokenRequestContext(new[] { "https://database.windows.net" });
var accessToken = new DefaultAzureCredential().GetToken(tokenRequestContext).Token;
...
sqlConnection.AccessToken = accessToken;

字符串
在VS2022中调试并登录到Azure时(工具>选项> Azure服务身份验证)。

相关问题