本文介绍了需要支持使用Azure Active Directory配置Azure SQL数据库-使用App Service在MVC Web应用程序中使用Entity Framework使用集成身份验证模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当前,我们需要将应用程序数据库身份验证模式从"SQL身份验证"转换为"Azure Active Directory –集成"模式.我们正在将MVC Web应用程序与Entity一起使用 框架. Web应用程序使用AAD的应用程序注册托管在Azure App服务中.我们已启用MSI.

Currently we have a requirement to Convert our application database Authentication mode from "SQL Authentication" to "Azure Active Directory – Integrated" Mode. We are using MVC web application with Entity Framework. Web application is hosted in Azure App service using an App registration for AAD. We have enabled MSI.

要求: 应用程序应该使用启用了MSI的"Azure Active Directory –集成"模式连接到数据库.

Requirement:Application should we connecting to database using "Azure Active Directory – Integration" Mode with MSI being enable.

代码更改直到现在: 我们已使数据库服务器能够使用"Azure Active Directory –集成"与MSI进行连接.为此,我们执行了一些CLI命令,并在Azure SQL DB中为应用程序服务创建了一个标识.

Code Changes till now:We have enable the database server to connect using "Azure Active Directory – Integration" with MSI. For this we have execute few CLI Commands and have created an identity for the app service in Azure SQL DB.

以下是我们针对MSI的Active Directory身份验证模式执行的命令

Below are the commands that we have executed for Active directory authentication mode with MSI

命令按顺序排列:

>>启用托管服务身份

az web应用程序身份分配--resource-group<您的资源组名称> --name<您的应用服务名称>

az webapp identity assign --resource-group <your resource group name> --name <your app service name>

运行上述命令后,将在Azure Active Directory中创建身份.从输出中复制principalId,并在step2命令中使用它.

>>在Azure Active Directory中查看身份的价值

az ad sp show --id d15254bf-3ce6-42a7-9b2c-1243e9de2728

az ad sp show --id d15254bf-3ce6-42a7-9b2c-1243e9de2728

注意:将step1输出中的PrincipalId替换为< Principalid>

>>光栅化对身份的数据库访问权限

az sql服务器ad-admin create --resource-group<您的资源组名称> --server-name<您的数据库服务器名称> --display-name<邮件ID> --object-id d15254bf-3ce6-42a7-9b2c-1243e9de2728

az sql server ad-admin create --resource-group <your resource group name> --server-name <your database server name> --display-name <mail id> --object-id d15254bf-3ce6-42a7-9b2c-1243e9de2728

>>修改连接字符串

>> Modify the Connection String

az web应用配置连接字符串集--resource-group<您的资源组名称> --name<您的应用服务名称> --settings MyDbConnection ='服务器= tcp:xxxxxxxx.database.windows.net,1433;数据库= xxxxx;' --connection-string-type SQLAzure

az webapp config connection-string set --resource-group <your resource group name> --name <your app service name> --settings MyDbConnection='Server=tcp:xxxxxxxx.database.windows.net,1433;Database=xxxxx;' --connection-string-type SQLAzure

面临的问题:

ISSUE Faced:

我们已更新了连接字符串,如下所示:

Server = tcp:xxxxxxx.database.windows.net,1433; Database = xxxxxxx; authentication ='Active Directory Integrated'; MultipleActiveResultSets = True; App = EntityFramework

通过这种连接,我们能够按预期从应用程序连接到数据库.但是,当在Azure App服务上部署了相同的消息时,我们将收到以下错误:

" 无法在Active Directory中对用户NT Authority \ Anonymous登录进行身份验证(身份验证= ActiveDirectoryIntegrated). 错误代码0x4BC;状态10指定的域名格式无效."

"Failed to authenticate the user NT Authority\Anonymous Logon in Active Directory (Authentication=ActiveDirectoryIntegrated). Error code 0x4BC; state 10 The format of the specified domain name is invalid."

为解决上述问题,我们添加了代码以获取客户端ID和Secret的访问令牌,并将此访问令牌传递给连接字符串.

使用的连接字符串:

Server = tcp:xxxxxxx.database.windows.net,1433; Database = xxxxxxxx; MultipleActiveResultSets = True; App = EntityFramework

(请注意:authentication ='Active Directory Integrated'属性已从连接字符串中删除.因为SqlConnection对象将不接受accesstoken如果身份验证属性 在连接字符串中提供)

(Please note: authentication='Active Directory Integrated' attribute has been removed from the connection string. Because the SqlConnection object will not accept accesstoken if the authentication property is provided in the connection string)

通过此更改,我也无法从本地主机运行该应用程序.下面是错误消息.

基础提供程序在打开. ---> System.Data.SqlClient.SqlException:用户"登录失败.

The underlying provider failed on Open. ---> System.Data.SqlClient.SqlException: Login failed for user ''.

我尝试了许多解决方案,但没有任何帮助.目前,我被阻止了.如果有人遇到过同样的问题,我需要一些支持. ??

非常感谢您的帮助.

推荐答案

当您遵循以下文档时,与AAD Integrated的连接就没有问题.

When you follow the below documentation, there are no issues connecting with AAD Integrated. 

配置和管理Azure Active Directory使用SQL数据库,托管实例或SQL数据仓库进行身份验证

只是为了说明配置的简单性.我不确定您是否可以在不做任何修改的情况下在两个环境中使用相同的代码库.

Just to show the simplicity of the configuration. I am not sure you are going to get the same code base to work in both environment w/o some modification.

能否请您对正在使用的两个独立环境进行审核?

Can you please provide an audit with regard to the two separate environments you are working in?


这篇关于需要支持使用Azure Active Directory配置Azure SQL数据库-使用App Service在MVC Web应用程序中使用Entity Framework使用集成身份验证模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-26 17:47