使用JWT令牌连接字符串的Active Directory身份验证问题

问题描述

我正在尝试使用任务Azure sql数据库部署通过使用Azure sql Database令牌指定连接字符串来运行sql脚本以在JWT中创建用户角色。

在Microsoft文档中,我尝试了DevOps任务中的语法,但是它引发了错误

string ConnectionString =@"Data Source=n9lxnyuzhv.database.windows.net; Initial Catalog=testdb;" sqlConnection conn = new sqlConnection(ConnectionString); conn.Accesstoken = "Your JWT token" conn.open();

错误

##[error]Invalid
 keyword,contain one or more of 'no characters','control characters','leading or trailing whitespace' or 'leading semicolons'.

Please find the Steps followed 
Created service principle in azure ad.
Granted directory consent.
Added the service principle to a AD GROUP.
Added that group as the ACTIVE DIRECTORY ADMIN in sql server so that all the members of the group can login using single sign on.

现在,我需要向不在admin组中的其他用户提供访问权限,因此尝试添加包含的用户,并且通过运行create user查询来实现这一点。

但是我需要对其进行自动化,因此为了实现自动化,sql部署任务下有4个选项,分别是 1.sql密码认证 2.活动目录集成 3,活动目录密码 4.连接字符串

我试图实现的是通过为服务主体生成JWT令牌来通过连接字符串。

但是由于某种原因,它的失败可以帮助我在Azure DevOps Pipeline的sql部署任务中为JWT TOKEN TO RUN提供连接字符串的确切语法。

解决方法

恐怕JWT令牌不能在连接字符串中使用。您可以从azure数据库UI门户检查可用的连接字符串。

enter image description here

但是,如果要使用JWT TOKEN自动执行sql查询,则可以编写脚本以在azure powershell taskpowershell task而不是Azure SQL database deployment task中运行。请参阅以下示例Powershell脚本:

steps:
- task: AzurePowerShell@5
  displayName: 'Azure PowerShell script: InlineScript'
  inputs:
    azureSubscription: 'myazureSubscription'
    ScriptType: InlineScript
    Inline: |
     
     $Token = "JWT Token"
     
     $SqlConnection = New-Object System.Data.SqlClient.SqlConnection
     
     #change the below to your server and database
     $SqlConnection.ConnectionString = "Data Source=<yourserver>.database.windows.net; Initial Catalog=<yourdatabase>"
      
     $SqlConnection.AccessToken = $Token
     $SqlConnection.Open()
     
     #get a query from a file
     #$query= get-content ".\GetProcDefs.sql"
     
     $query ="SELECT * from Persons"
      
     $SqlCmd = New-Object System.Data.SqlClient.SqlCommand
     $SqlCmd.CommandText = $query
     $SqlCmd.Connection = $SqlConnection

     $SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
     $SqlAdapter.SelectCommand = $SqlCmd
     $DataSet = New-Object System.Data.DataSet
     $SqlAdapter.Fill($DataSet)
      
     $DataSet.Tables[0]
    
     $SqlConnection.Close()
    azurePowerShellVersion: LatestVersion