Azure托管身份不适用于DB *类

时间:2020-06-17 10:21:09

标签: azure azure-sql-database azure-managed-identity

我正在使用Azure托管身份提供对应用程序服务的Azure SQL数据库访问。我启用了身份并提供了从用户组对服务主体的访问。现在,当我尝试使用db *类从Azure令牌提供程序获取访问令牌时,它不起作用,但是如果我使用sql *类,则该服务就像一个超级按钮一样起作用。我无法找到任何有关为何如此的信息。

忽略变量名。

无效的代码:

    SqlDatabase db = new SqlDatabase(****CONNECTIONSTRING****);
                    using (DbConnection conn1 = db.CreateConnection())
                    {
                        var conn = (SqlConnection)conn1;
                        conn.AccessToken = (new Microsoft.Azure.Services.AppAuthentication.AzureServiceTokenProvider()).GetAccessTokenAsync("***").Result;
                        if (conn.State == ConnectionState.Closed) conn.Open();
                        using (DbCommand dbcmd = db.GetStoredProcCommand("dbo.AllCustomerTypeGet"))
                        {
                            //Insert Record in Debug Log table
                            InsertDebugLog(dbcmd, LoginUser, true, IsRequiredLog);
..........
........
......

有效的代码:

using (SqlConnection conn = new SqlConnection(****CONNECTIONSTRING****))
                {
                    conn.AccessToken = (new Microsoft.Azure.Services.AppAuthentication.AzureServiceTokenProvider()).GetAccessTokenAsync("***").Result;
                    if (conn.State == ConnectionState.Closed) conn.Open();

                    using (SqlCommand cmd = new SqlCommand("dbo.AllCustomerTypeGet", conn))
                    {
                        cmd.CommandType = System.Data.CommandType.StoredProcedure;
                        using (SqlDataReader rdr = cmd.ExecuteReader())
........
........
........

托管身份是要用于特定的数据库类,而不是像dbconnection,dbcommand等更通用的类吗?

0 个答案:

没有答案