【问题标题】:Authenticating to Azure SQL database without using UserID and Password在不使用用户 ID 和密码的情况下对 Azure SQL 数据库进行身份验证
【发布时间】:2019-12-31 23:49:42
【问题描述】:

我正在尝试使用此代码通过 c# 对我的 Azure SQL 进行身份验证。该代码有效,但我不想使用我的用户 ID 和密码。我可以使用其他任何东西进行身份验证吗?令牌?

using System;
using System.Data.SqlClient;
using System.Text;

namespace sqltest
{
    class Program
    {
        static void Main(string[] args)
        {
            try 
            { 
                SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder();
                builder.DataSource = "<server>.database.windows.net"; 
                builder.UserID = "<username>";            
                builder.Password = "<password>";     
                builder.InitialCatalog = "<database>";

                using (SqlConnection connection = new SqlConnection(builder.ConnectionString))
                {
                    Console.WriteLine("\nQuery data example:");
                    Console.WriteLine("=========================================\n");

                    StringBuilder sb = new StringBuilder();
                    sb.Append("SELECT TOP 20 pc.Name as CategoryName, p.name as ProductName ");
                    sb.Append("FROM [SalesLT].[ProductCategory] pc ");
                    sb.Append("JOIN [SalesLT].[Product] p ");
                    sb.Append("ON pc.productcategoryid = p.productcategoryid;");
                    String sql = sb.ToString();

                    using (SqlCommand command = new SqlCommand(sql, connection))
                    {
                        connection.Open();
                        using (SqlDataReader reader = command.ExecuteReader())
                        {
                            while (reader.Read())
                            {
                                Console.WriteLine("{0} {1}", reader.GetString(0), reader.GetString(1));
                            }
                        }
                    }                    
                }
            }
            catch (SqlException e)
            {
                Console.WriteLine(e.ToString());
            }
            Console.ReadLine();
        }
    }
}

【问题讨论】:

    标签: c# authentication azure-sql-database


    【解决方案1】:

    你可以使用 2 个东西。
    1. 为 Azure 资源使用托管标识
    这是推荐的方法,在这种方法中,代码将使用 azure 身份生成令牌。

    .NET Framework 4.6 或更高版本或 .NET Core 2.2 或 使用访问令牌方法需要更高的权限。

    更多信息-> https://docs.microsoft.com/en-us/azure/active-directory/managed-identities-azure-resources/tutorial-windows-vm-access-sql

    2. 将敏感信息(用户名/密码/连接字符串)存储在 azure key vault 中
    https://docs.microsoft.com/en-us/azure/key-vault/tutorial-net-create-vault-azure-web-app

    【讨论】:

      【解决方案2】:

      您可以使用 SQL 配置和管理 Azure Active Directory 身份验证。详情请参考this document

      【讨论】:

        【解决方案3】:

        如前所述,托管身份是实现此目的的方法。 Here is a similar post specific to App Service to Azure SQL 这也可以通过 ARM 来实现,托管身份也可以被授予对 Key Vault 的访问策略。

        【讨论】:

          猜你喜欢
          • 2016-12-15
          • 1970-01-01
          • 2023-01-10
          • 2018-11-09
          • 2015-04-23
          • 1970-01-01
          • 2021-11-16
          • 2021-09-09
          • 1970-01-01
          相关资源
          最近更新 更多