gpt4 book ai didi

ef-code-first - MSI 可以使用 EF Code First 吗?

转载 作者:行者123 更新时间:2023-12-04 23:52:35 26 4
gpt4 key购买 nike

我已经被困在这个问题上很长一段时间了,没有运气独自推进它。

我正在尝试使用 MSI token 从 Azure 应用服务连接到 EF CodeFirst 托管数据库。

当我使用 ARM 部署应用服务时,我生成了一个输出以确保它创建了一个服务主体:

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

在 Kudu 中,环境变量显示正在安装:

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

我在 Azure 门户中提供了一个连接字符串,如下所示:

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

我已将委托(delegate)人作为所有者添加到数据库中。

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

标记被添加到 DbContext,如下所示:

正在使用以下方式添加 token :

    static async Task AttachAccessTokenToDbConnection(IDbConnection dbConnection)
{
SqlConnection sqlConnection = dbConnection as SqlConnection;
if (sqlConnection == null)
{
return;
}
string msiEndpoint = Environment.GetEnvironmentVariable("MSI_ENDPOINT");
if (string.IsNullOrEmpty(msiEndpoint))
{
return;
}

var msiSecret = Environment.GetEnvironmentVariable("MSI_SECRET");
if (string.IsNullOrEmpty(msiSecret))
{
return;
}

// To get around:
// "Cannot set the AccessToken property if 'UserID', 'UID', 'Password', or 'PWD' has been specified in connection string."
var terms = new[] {"UserID","Password","PWD=","UID=" };
string connectionString = dbConnection.ConnectionString;

foreach (var term in terms)
{
if (connectionString.Contains(term, StringComparison.InvariantCultureIgnoreCase))
{
return;
}
}

string accessToken = await AppCoreDbContextMSITokenFactory.GetAzureSqlResourceTokenAsync();
sqlConnection.AccessToken = accessToken;
}

随着追踪, token 是:

 .eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI....

使用 jwt.io 解码给出:

{
"typ": "JWT",
"alg": "RS256",
"x5t": "FSimuFrFNoC0sJXGmv13nNZceDc",
"kid": "FSimuFrFNoC0sJXGmv13nNZceDc"
}.{
"aud": "https://database.windows.net/",
"iss": "https://sts.windows.net/e6d2d4cc-b762-486e-8894-4f5f440dxxxx/",
"iat": 1522783025,
"nbf": 1522783025,
"exp": 1522786925,
"aio": "Y2NgYPjNdyJd9zrzpLavJSEzNIuPAAA=",
"appid": "d1057cea-461b-4946-89a9-d76439c2xxxx",
"appidacr": "2",
"e_exp": 262800,
"idp": "https://sts.windows.net/e6d2d4cc-b762-486e-8894-4f5f440dxxxx/",
"oid": "98f2c1f2-0a86-4ff1-92db-d43ec0edxxxx",
"sub": "98f2c1f2-0a86-4ff1-92db-d43ec0edxxxx",
"tid": "e6d2d4cc-b762-486e-8894-4f5f440dxxxx",
"uti": "59bqKWiSL0Gf0bTCI0AAAA",
"ver": "1.0"
}.[Signature]

我根据网上的一些建议添加了 Persist Security Info = True,但是没有检测到任何东西。

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

我得到的错误是:

[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 连接到数据库?在这一点上,在真正陷入困境几周之后,我开始怀疑这是否可能。

感谢您提供的任何帮助——即使只是证明它可以工作,对于初学者来说。

最佳答案

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

关于ef-code-first - MSI 可以使用 EF Code First 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50054491/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com