0

我正在尝试运行 SQL 脚本,通过指定带有令牌Azure SQL Database的连接字符串来创建使用任务 Azure 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 query 实现了这一点。

但我需要自动化它,所以为了自动化,SQL 部署任务下有 4 个选项,它们是 1.sql 密码身份验证 2.活动目录集成 3.活动目录密码 4.连接字符串

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

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

4

1 回答 1

0

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

在此处输入图像描述

但是,如果您想使用 JWT TOKEN 自动执行 sql 查询,您可以编写脚本以在 aazure 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
于 2020-10-07T08:28:19.687 回答