应用程序无法使用托管身份连接到数据库

发布于 2025-01-31 15:45:04 字数 1740 浏览 4 评论 0原文

我有一项Azure应用服务,需要授予对SQL数据库的访问权限(我的帐户下的Azure Portal)。我想通过A 托管身份授予其访问权限,因此我已经打开了系统分配的托管身份。

托管身份已打开。”

在SQL Server的访问控制(IAM)下,我添加了应用程序服务作为贡献者。

已添加为贡献者“

Server=tcp:virtualscorecard.database.windows.net,1433;Initial Catalog=GolfScore; Persist Security Info=False; MultipleActiveResultSets=False; Encrypt=True; TrustServerCertificate=False; Connection Timeout=30; Authentication=Active Directory Managed Identity;

但是当我尝试连接时,我会在日志流中获得以下错误:

[错误] Microsoft.EntityFrameWorkCore.Query:发生异常 迭代询问上下文类型的查询结果 'golfscoreapi.dbcontexts.userprofilecontext'.microsoft.data.sqlclient.sqlexception (0x80131904):用户登录失败''。 当前未配置该服务器以接受此令牌。

我在连接字符串中尝试了很多变化。我唯一可以工作的是,如果我在连接字符串中添加用户名和密码。但是托管身份应该使这一不必要。

我在做什么错?

更新1

我已经知道我需要将作为用户的应用服务添加到数据库中。为此,我必须首先分配Azure Active Directory Admin。但是,当我尝试时,我无法选择任何有意义的东西。与我相关的唯一条目,无法选择,因为“不允许Microsoft帐户成员”。

更新2:

我通过在Azure Active Directory中创建一个组并使自己成为其中的成员来工作。然后将该组设置为数据库上的Azure Active Directory管理员,使自己能够将我的应用服务(golfscoreapi)添加为数据库上的dbreader

我唯一无法获得的是为什么我需要为特定的IP地址添加防火墙规则。这似乎是最佳的,因为(据我所知)IP地址可能会更改,例如,如果更改了应用程序服务计划。

I have an Azure App Service which I need to grant access to a SQL Database (also in Azure Portal under my account). I want to grant it access via a Managed Identity, so I have turned on system assigned Managed Identity.

System assigned managed identity turned on.

Under Access control (IAM) for the SQL server, I have added the App Service as a contributer.

App Service has been added as contributer

Server=tcp:virtualscorecard.database.windows.net,1433;Initial Catalog=GolfScore; Persist Security Info=False; MultipleActiveResultSets=False; Encrypt=True; TrustServerCertificate=False; Connection Timeout=30; Authentication=Active Directory Managed Identity;

But when I try to connect, I get the following error in the log stream:

[Error] Microsoft.EntityFrameworkCore.Query: An exception occurred
while iterating over the results of a query for context type
'GolfScoreAPI.DbContexts.UserProfileContext'.Microsoft.Data.SqlClient.SqlException
(0x80131904): Login failed for user ''.
The server is not currently configured to accept this token.

I have tried quite a lot of variations in the connection string. The only one that I have gotten to work, is if I add the username and password in the connection string. But Managed Identities should make this unnecessary.

What am I doing wrong?

Update 1

I have been made aware that I need to add my App Service as a user to the database. To do that, I must first assign an Azure Active Directory admin. But I cannot select anything meaningful when I try that. The only entry related to me, cannot be selected because "Microsoft Account members are not allowed."

Microsoft Account members are not allowed.

Update 2:

I got it to work, by creating a group in Azure Active Directory and making myself an member of it. Then setting that group as Azure Active Directory admin on the database, making myself able to add my App Service (GolfScoreAPI) as a dbreader on the database.

The only thing I still don't get, is why I need to add a firewall rule for the specific IP address. This seems sub-optimal, as that IP address might change (to my knowledge), for instance if the App Service plan is changed.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

千紇 2025-02-07 15:45:04

是的,重要的作品缺失了。您已经从应用程序服务启用了身份,而SQL Server呢?:)。您需要在SQL Server中添加 Azure Active Directory Admin 。参考: - https:/ 。

来到SQL Server下一步。

  1. 将一个域 /单个管理帐户(在AAD注册)中添加为SQL Server AAD管理员。比以下步骤
  2. 登录到具有身份验证类型的登录名的SQL Server实例:-AAD -Universal带有MFA
  3. 输入域 /单个管理员帐户详细信息,在登录框弹出时给出密码。
    在SQL Server DB中运行以下命令。
    从外部提供商创建用户[<< sidentity_name_of_appservice>>];
    Alter角色DB_DDLADMIN/ DB_DATAREADER/ DB_DATAWRITER添加成员[< sidentity_name_of_appservice>>];
  4. 转到App Service&收集IP地址&添加SQL Server的防火墙,以避免防火墙问题。
  5. 在应用程序服务中进行必要的代码更改以使用默认凭据,如图像所示
  6. 单个位置中所有步骤的摘要i.sstatic.net/uimz5.png“ alt =”在此处输入图像描述”>

注: - 如果您使用Visual Studio进行开发,因此在运行本地实例以连接时到Azure SQL,请确保作为用户添加Visual Studio订阅电子邮件ID。

如果您正在使用Visual Studio,请确保

yes important piece is missing. You have enabled the Identity from the App service, what about the SQL server :). You need to add an Azure Active Directory admin in sql server. Reference :- https://learn.microsoft.com/en-us/azure/search/search-howto-managed-identities-sql

You have already done with Below step.

enter image description here

Coming to next step for SQL Server.

  1. Add a Domain /Individual admin account(registered with AAD) as a SQL server AAD admin. Than follow below steps
  2. Login to the SQL server Instance with Login with Authentication type :- AAD -Universal with MFA
  3. Enter the Domain /Individual admin account details, give the password upon login box popup.
    Run the Below Command in the sql server db.
    CREATE USER [<<Identity_name_of_AppService>>] FROM EXTERNAL PROVIDER;
    ALTER ROLE db_ddladmin/ db_datareader/ db_datawriter ADD MEMBER [<<Identity_name_of_AppService>>];
  4. Go to the App service & collect the IP address & add in the firewall of sql server to avoid firewall issue.
  5. Do the necessary code change in the app service to use the default credential , as shown in image
    enter image description here
  6. Summary of all steps in single place enter image description here

Note:- If you are using Visual studio for development, so while running the Local instance to connect to the Azure SQL, make sure to add your Visual studio subscription Email id as a user.

If you are using the Visual Studio, then make sure

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文