如何在c#中为SQL Azure启用内部Azure服务

Mic*_*ake 5 c# windows-firewall azure azure-sql-database

如何启用允许的服务:WINDOWS AZURE SERVICES,如c#中的管理门户中所示?

        _client = new SqlManagementClient(GetSubscriptionCredentials());

        var result = _client.Servers.CreateAsync(new ServerCreateParameters
        {
            AdministratorUserName = _config.ServerUserName,
            AdministratorPassword = _config.ServerPassword,
            Location = _config.Location,
            Version = "12.0"
        }, CancellationToken.None).Result;

        var sqlServerName = result.ServerName;

        // This will go away once we can enable the Azure internal firewall settings == Yes
        var ipAddress = _firewallManagement.GetPublicIP();
        var firewall = _client.FirewallRules.Create(sqlServerName, new FirewallRuleCreateParameters("Server", ipAddress, ipAddress));
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

Bru*_*ria 5

只需将0.0.0.0作为start_ip_address和end_ip_address(如下面的T-SQL)添加到sys.firewall_rules

exec sp_set_firewall_rule N'MicrosoftServices','0.0.0.0','0.0.0.0'
Run Code Online (Sandbox Code Playgroud)

不介意0.0.0.0范围,SQL Azure知道它仅适用于您订阅中的Azure IP.

select * from sys.firewall_rules

id  name    start_ip_address    end_ip_address  create_date modify_date
7   MicrosoftService    0.0.0.0 0.0.0.0 2015-07-29 13:34:55.790 2015-07-29 13:34:55.790
Run Code Online (Sandbox Code Playgroud)

Azure SQL数据库防火墙

当Azure中的应用程序尝试连接到您的数据库服务器时,防火墙会验证是否允许Azure连接.起始地址和结束地址等于0.0.0.0的防火墙设置表示允许这些连接.

https://msdn.microsoft.com/en-us/library/azure/ee621782.aspx#ConnectingFromAzure

以编程方式添加和删除SQL Azure防火墙规则

http://www.c-sharpcorner.com/uploadfile/dhananjaycoder/adding-and-deleting-sql-azure-firewall-rules-programmatically/

public void AddFirewallRule(FirewallRule rule)
        {
            using (SqlConnection conn = new SqlConnection(this.ConnectionString))
            using (SqlCommand cmd = conn.CreateCommand())
            {
                conn.Open();
                cmd.CommandText = "sp_set_firewall_rule";
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add("@name", SqlDbType.NVarChar).Value = rule.Name;
                cmd.Parameters.Add("@start_ip_address", SqlDbType.VarChar).Value = rule.startIPAddress.ToString();
                cmd.Parameters.Add("@end_ip_address", SqlDbType.VarChar).Value = rule.endIPAdress.ToString();
                cmd.ExecuteNonQuery();
            }
        }
Run Code Online (Sandbox Code Playgroud)