【问题标题】:How to add Azure MSI AccessToken into EF6 DbContext to connect Azure SQL PaaS如何将 Azure MSI AccessToken 添加到 EF6 DbContext 以连接 Azure SQL PaaS
【发布时间】:2020-04-29 15:44:55
【问题描述】:

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

连接字符串:

为了执行 CRUD 查询,我使用了旧的 ADO.NET 样式查询,如下所示:

获取 MSI: 为了运行迁移,如何将 Azure MSI AccessToken 传递给 DbContext 构造函数。

对于 dbcontext,我必须使用提供者名称定义单独的连接字符串。

【问题讨论】:

标签: c# azure entity-framework


【解决方案1】:

您需要在 DI 中进行设置:

Startup.cs

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

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

数据库上下文

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

【讨论】:

  • 嗨蒂亚戈,谢谢你的解决方案,如果 EF6 不是 EF COre 和 EF6 没有 OnConfiguring() 方法来覆盖,我正在使用什么
  • 对此感到抱歉。如果以下链接对您有帮助,请查看:stackoverflow.com/q/49531754/1384539
【解决方案2】:

这就是我为遇到此问题的任何人使用 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;
    }
}

注意:我必须使用非异步 GetToken 函数。它不适用于 GetTokenAsync。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-08-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多