简体   繁体   English

客户端在连接到 Azure SQL 数据库时遇到问题

[英]Client having issues connecting to Azure SQL Database

I have built a C# application for my client and I am hosting the database using Microsoft Azure.我已经为我的客户构建了一个 C# 应用程序,并且我正在使用 Microsoft Azure 托管数据库。 I found that in order for the client to access the database I need to add their client IP into the firewall configurations.我发现为了让客户端访问数据库,我需要将他们的客户端 IP 添加到防火墙配置中。 Is there any way that this could be done automatically once they launch the application or if there is another more efficient authentication method which could be used since the application will be download from a website and used by anyone so I would need a method to grant access to anyone who downloads my application.有什么方法可以在他们启动应用程序后自动完成,或者是否可以使用另一种更有效的身份验证方法,因为该应用程序将从网站下载并可供任何人使用,所以我需要一种方法来授予访问权限给任何下载我的应用程序的人。 I am fairly new to Microsoft Azure so forgive me if I come off as stupid I just need some advice.我对 Microsoft Azure 还很陌生,所以如果我表现得愚蠢,请原谅我,我只需要一些建议。 Thanks in advance.提前致谢。

There's no way can add the client IP into Azure SQL database firewall automatically.无法自动将客户端 IP 添加到Azure SQL 数据库防火墙中。

But you can set the firewall range to allow the all database user connect Azure SQL database from any client IP: set the firewall range from:但是您可以设置防火墙范围以允许所有数据库用户从任何客户端 IP 连接 Azure SQL 数据库:设置防火墙范围:

0.0.0.0---255.255.255.255

在此处输入图像描述

But as @Caurav Mantri mentioned, you must need think about the database security issue to protect the SQL database.但正如@Caurav Mantri 提到的,您必须考虑数据库安全问题以保护 SQL 数据库。

Please reference:请参考:

  1. ransparent data encryption for SQL Database and Azure Synapse SQL 数据库和 Azure Synapse 的透明数据加密
  2. Always Encrypted: Protect sensitive data and store encryption keys in Azure Key Vault 始终加密:保护敏感数据并将加密密钥存储在 Azure 密钥库中

Hope this helps.希望这可以帮助。

  1. Programmatically adding each ip address:以编程方式添加每个 ip 地址:

    • using tsql:使用 tsql:

If you want to add ip address to database firewall programatically, you can run the below stored proecedure in the your Azure database.如果您想以编程方式将 ip 地址添加到数据库防火墙,您可以在 Azure 数据库中运行以下存储过程。

sp_set_database_firewall_Rule at MSDN MSDN 上的 sp_set_database_firewall_Rule

-- Create database-level firewall setting for only IP 0.0.0.4  
  EXECUTE sp_set_database_firewall_rule N'Example DB Setting 1', '0.0.0.4', '0.0.0.4'; 
  • Using Commandline:使用命令行:

You can use SQLCMD.exe to execute stored procedure sp_set_daabase_firewall_rule您可以使用 SQLCMD.exe 执行存储过程sp_set_daabase_firewall_rule

String clientIPAddress = Request.UserHostAddress;
using(SqlConnection sqlConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["SqlAzureMaster"].ConnectionString)) {
 sqlConnection.Open();
 using(SqlCommand sqlCommand = new SqlCommand("sp_set_firewall_rule", sqlConnection)) {
  sqlCommand.CommandType = System.Data.CommandType.StoredProcedure;
  sqlCommand.Parameters.Add("@name", SqlDbType.NVarChar).Value = clientIPAddress;
  sqlCommand.Parameters.Add("@start_ip_address", SqlDbType.VarChar).Value = clientIPAddress;
  sqlCommand.Parameters.Add("@end_ip_address", SqlDbType.VarChar).Value = clientIPAddress;
  sqlCommand.ExecuteNonQuery();
 }
}

2. Refreshing the cache post the firewall rules change 2.防火墙规则更改后刷新缓存

Once, you programmatically add the firewall rules, you have to update the authentication cache which is holding logins, firewall rules for the database.一次,您以编程方式添加防火墙规则,您必须更新保存登录名的身份验证缓存,数据库的防火墙规则。

You need to call below command.您需要调用以下命令。 DBCC FLUSTHAUTHCACHE on msdn msdn 上的 DBCC FLUSTHAUTHCACHE

DBCC FLUSHAUTHCACHE 

Note: Adding range of ip address for a office network:注:为办公网络添加ip地址范围:

If your client will be working from an office network, you can get the range of ip addresses for that office network and add them.如果您的客户将在办公网络中工作,您可以获得该办公网络的 ip 地址范围并添加它们。 It will avoid you to add the ip address every time to the database.它将避免您每次将 ip 地址添加到数据库中。 Database supports 128 IP configurations at a time.数据库一次支持 128 个 IP 配置。 Make sure that you are not going beyond 128 limit.确保您没有超过 128 限制。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM