azure MSI可以使用EF CodeFirst吗?

t1rydlwq  于 2023-11-21  发布在  其他
关注(0)|答案(2)|浏览(157)

我已经在这个问题上卡住了很长一段时间了,没有运气靠自己推进它。
我正在尝试使用MSI令牌从Azure应用服务连接到EF CodeFirst托管数据库。
当我使用ARM部署App Service时,我生成了一个输出,确保它创建了一个Service Principal:

  1. {
  2. "principalId":"98f2c1f2-0a86-4ff1-92db-d43ec0edxxxx","
  3. tenantId":"e6d2d4cc-b762-486e-8894-4f5f440dxxxx",
  4. "type":"SystemAssigned"
  5. }

字符串
在Kudu中,环境变量显示它正在安装:

  1. MSI_ENDPOINT = http://127.0.0.1:41239/MSI/token/
  2. MSI_SECRET = 7C1B16Fxxxxxxxxxxxxx


我在Azure Portal中提供了如下连接字符串:

  1. Data Source=nzmoebase0000bt.database.windows.net;Initial Catalog=nzmoebase0001bt;Connect Timeout=300;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=300;


我已将主体作为所有者添加到数据库中。

  1. Note: I cannot do the same for the master db.


该令牌被添加到DbContext中,如下所示:
正在使用以下方式添加令牌:

  1. static async Task AttachAccessTokenToDbConnection(IDbConnection dbConnection)
  2. {
  3. SqlConnection sqlConnection = dbConnection as SqlConnection;
  4. if (sqlConnection == null)
  5. {
  6. return;
  7. }
  8. string msiEndpoint = Environment.GetEnvironmentVariable("MSI_ENDPOINT");
  9. if (string.IsNullOrEmpty(msiEndpoint))
  10. {
  11. return;
  12. }
  13. var msiSecret = Environment.GetEnvironmentVariable("MSI_SECRET");
  14. if (string.IsNullOrEmpty(msiSecret))
  15. {
  16. return;
  17. }
  18. // To get around:
  19. // "Cannot set the AccessToken property if 'UserID', 'UID', 'Password', or 'PWD' has been specified in connection string."
  20. var terms = new[] {"UserID","Password","PWD=","UID=" };
  21. string connectionString = dbConnection.ConnectionString;
  22. foreach (var term in terms)
  23. {
  24. if (connectionString.Contains(term, StringComparison.InvariantCultureIgnoreCase))
  25. {
  26. return;
  27. }
  28. }
  29. string accessToken = await AppCoreDbContextMSITokenFactory.GetAzureSqlResourceTokenAsync();
  30. sqlConnection.AccessToken = accessToken;
  31. }


在启用跟踪的情况下,令牌为:

  1. .eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI....


使用jwt.io解码得到:

  1. {
  2. "typ": "JWT",
  3. "alg": "RS256",
  4. "x5t": "FSimuFrFNoC0sJXGmv13nNZceDc",
  5. "kid": "FSimuFrFNoC0sJXGmv13nNZceDc"
  6. }.{
  7. "aud": "https://database.windows.net/",
  8. "iss": "https://sts.windows.net/e6d2d4cc-b762-486e-8894-4f5f440dxxxx/",
  9. "iat": 1522783025,
  10. "nbf": 1522783025,
  11. "exp": 1522786925,
  12. "aio": "Y2NgYPjNdyJd9zrzpLavJSEzNIuPAAA=",
  13. "appid": "d1057cea-461b-4946-89a9-d76439c2xxxx",
  14. "appidacr": "2",
  15. "e_exp": 262800,
  16. "idp": "https://sts.windows.net/e6d2d4cc-b762-486e-8894-4f5f440dxxxx/",
  17. "oid": "98f2c1f2-0a86-4ff1-92db-d43ec0edxxxx",
  18. "sub": "98f2c1f2-0a86-4ff1-92db-d43ec0edxxxx",
  19. "tid": "e6d2d4cc-b762-486e-8894-4f5f440dxxxx",
  20. "uti": "59bqKWiSL0Gf0bTCI0AAAA",
  21. "ver": "1.0"
  22. }.[Signature]


我添加了Persist Security Info = True根据网上的几个建议,但没有什么可检测的.

  1. Data Source=nzmoebase0000bt.database.windows.net;Initial Catalog=nzmoebase0001bt;MultipleActiveResultSets=False;Persist Security Info = True;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;


我得到的错误是:

  1. [InvalidOperationException: This operation requires a connection to the 'master' database. Unable to create a connection to the 'master' database because the original database connection has been opened and credentials have been removed from the connection string. Supply an unopened connection.]


有没有人使用CodeFirst、Migrations和MSI连接到数据库?在这一点上,经过几个星期的真正卡住,我开始怀疑这是否可能。
谢谢你的任何帮助--即使只是证明它可以工作,首先。

alen0pnh

alen0pnh1#

不幸的是,据我所知,没有。一个主要的绊脚石,以一个项目,不得不回落到不安全的密码/密码加载连接字符串。

92vpleto

92vpleto2#

您可以在SQL连接上设置访问令牌,如下所示:
1.安装Microsoft.Azure.Services.AppAuthentication nuget包
1.这样设置你的上下文类:

  1. public class MyDatabaseContext : DbContext
  2. {
  3. public MyDatabaseContext(DbContextOptions<MyDatabaseContext> options)
  4. : base(options)
  5. {
  6. // Get the db connection
  7. var connection = (SqlConnection)Database.GetDbConnection();
  8. // Add the access token
  9. connection.AccessToken = new AzureServiceTokenProvider()
  10. .GetAccessTokenAsync("https://database.windows.net/")
  11. .ConfigureAwait(false).GetAwaiter().GetResult();
  12. }
  13. public DbSet<MyTable> MyTable { get; set; }
  14. }

字符串

展开查看全部

相关问题