当前位置: 首页 > news >正文

Azure App Service连接Azure SQL MI

  前言

  最近,在研究云迁移,做了个测试如何使用App Service连接SQL MI。

  正文

  1.测试项目是Net Framework v4.8,核心代码如下图:

 1 StringBuilder message = new StringBuilder();
 2 try
 3 {
 4     string sqlstr = "Server=smi-test.c5c92fb776c2.database.windows.net;Database=test;Authentication=Active Directory Managed Identity;Encrypt=True";
 5     SqlConnection connection = new SqlConnection(sqlstr);
 6 
 7     connection.Open();
 8     message.AppendLine("opened");
 9     SqlCommand cmd = new SqlCommand("SELECT TOP (1000) [ID],[Text] FROM [test].[dbo].[Table1]", connection);
10     SqlDataAdapter ada = new SqlDataAdapter(cmd);
11     DataTable dt = new DataTable();
12     ada.Fill(dt);
13     message.AppendLine("row count: " + dt.Rows.Count.ToString());
14     foreach (DataRow dr in dt.Rows)
15     {
16         message.AppendLine(dr["ID"].ToString() + dr["Text"].ToString());
17     }
18     connection.Close();
19 
20 }
21 catch (Exception ex)
22 {
23     message.AppendLine(ex.Message.ToString());
24     message.AppendLine(ex.StackTrace.ToString());
25 }

  2.SQL Server用的是SQL MI,如下图:

image

  3.这是使用的是System assigned托管标识,需要先用PowerShell命令给一下Graph API的权限,才可以使用,否则找不到,如下图:

# Script to assign permissions to an existing UMI 
# The following required Microsoft Graph permissions will be assigned: 
#   User.Read.All
#   GroupMember.Read.All
#   Application.Read.AllImport-Module Microsoft.Graph.Authentication
Import-Module Microsoft.Graph.Applications$tenantId = "xxxxxxxx-xxxx-xxxx-xxxxxxxx" # Your tenant ID
$MSIName = "appserviceforsqlmi"; # Name of your managed identity# Log in as a user with the "Privileged Role Administrator" role
Connect-MgGraph -TenantId $tenantId -Scopes "AppRoleAssignment.ReadWrite.All,Application.Read.All"# Search for Microsoft Graph
$MSGraphSP = Get-MgServicePrincipal -Filter "DisplayName eq 'Microsoft Graph'";
$MSGraphSP# Sample Output# DisplayName     Id                                   AppId                                SignInAudience      ServicePrincipalType
# -----------     --                                   -----                                --------------      --------------------
# Microsoft Graph 47d73278-e43c-4cc2-a606-c500b66883ef 00000003-0000-0000-c000-000000000000 AzureADMultipleOrgs Application$MSI = Get-MgServicePrincipal -Filter "DisplayName eq '$MSIName'" 
if($MSI.Count -gt 1)
{ 
Write-Output "More than 1 principal found with that name, please find your principal and copy its object ID. Replace the above line with the syntax $MSI = Get-MgServicePrincipal -ServicePrincipalId <your_object_id>"
Exit
}# Get required permissions
$Permissions = @("User.Read.All""GroupMember.Read.All""Application.Read.All"
)# Find app permissions within Microsoft Graph application
$MSGraphAppRoles = $MSGraphSP.AppRoles | Where-Object {($_.Value -in $Permissions)}# Assign the managed identity app roles for each permission
foreach($AppRole in $MSGraphAppRoles)
{$AppRoleAssignment = @{principalId = $MSI.IdresourceId = $MSGraphSP.IdappRoleId = $AppRole.Id}New-MgServicePrincipalAppRoleAssignment -ServicePrincipalId $AppRoleAssignment.PrincipalId -BodyParameter $AppRoleAssignment -Verbose
}

  4.使用sql语句加权限,如下图:

CREATE USER [appserviceforsqlmi] FROM EXTERNAL PROVIDER;
ALTER ROLE db_datareader ADD MEMBER [appserviceforsqlmi];

  5.然后部署app Service就行了

  总结

  这里需要注意的连接sql的时候要用Microsoft.Data.Client,不要用System的。

http://www.zskr.cn/news/5515.html

相关文章:

  • 将目标数据复制到服务器-ServerSetReplicatedTargetData()
  • 不是说 PHP 不行了吗?为什么 Swoole 还在更新?
  • qoj1831 Bruteforce
  • C++数据结构和算法:链表
  • 详细介绍:Maven入门_简介、安装与配置
  • train-labels.idx1-ubyte里是什么
  • 创建预测窗口-ScopedPredictionWindow();
  • Ability-GetCurrentActorInfo()-IsLocallyControlled()和APawn::IsLocallyControlled()
  • 应该遵守的代码规范与读《数学之美》有感
  • AT_arc171_c [ARC171C] Swap on Tree
  • 新媒体运营用AI排版工具|10分钟搞定公众号图文的全流程指南
  • ctf工具整理
  • 250915 jave se简单过完一遍
  • AT_arc183_b [ARC183B] Near Assignment
  • kubectl 常用命令的分类汇总(一)
  • 完整教程:C3P0连接池适配HGDB
  • kubectl 常用命令的分类汇总(二)
  • ECT-OS-JiuHuaShan框架的逻辑是自洽的,是基于数学表达,不替代现实的苦辣酸甜。
  • 《FastAPI零基础入门与进阶实战》第18篇:Token验证改善--CRUD中应用 - 详解
  • 【QT】创建一个简单的QT界面
  • 2025.9.15总结
  • 9.11总结
  • 真正的高手,首先是如何验证框架是数学逻辑自洽的必然,然后就可以放心去用。比如编码,几次输出,就可以断定是纯数学逻辑自洽的必然,除此之外,不可能得到这样的效果
  • Java 实现HTML转Word:从HTML材料与字符串到可编辑Word文档
  • 第02周Java:从方法传参到对象封装
  • 基于pandas自动化的csv信息提取保存的脚本
  • STM32 HAL学习笔记:GC1808(PCM1808)的使用以及使用I2S+DMA读取
  • MSTP 单域
  • 阿里云百炼平台使用避坑记录 - 详解
  • 第2周-预习作业