如何通过powershell或C#使用本地IP远程更新azure SQL防火墙
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何通过powershell或C#使用本地IP远程更新azure SQL防火墙相关的知识,希望对你有一定的参考价值。
如何使用本地PC IP地址远程更新我的azure SQL防火墙,以便通过PowerShell或C#进行管理。
我查看了文档,但我只能从实际的服务器本身找到它,而不是远程。
我可以让visual studio弹出并询问我是否要随机地将我的IP地址添加到防火墙,但大多数时候它只是失败而且没有问我所以我正在寻找除手动登录天蓝色之外的解决方案我的IP地址更改时的门户网站。
寻找示例或指向文档的链接,以显示要使用的库。
从C#,您可以通过Microsoft.Azure.Management.Fluent访问所有Azure资源
// Create an authentication context to Azure and acquire a login token
var context = new AuthenticationContext("https://login.microsoftonline.com/tenantId");
var auth = await context.AcquireTokenAsync("https://management.core.windows.net/",
"yourClientId", new Uri("urn:ietf:wg:oauth:2.0:oob"),
new PlatformParameters(PromptBehavior.Always));
var tokenCredentials = new TokenCredentials(token);
var azureCredentials = new AzureCredentials(tokenCredentials, tokenCredentials,
AzureParts.Tenant, AzureEnvironment.AzureGlobalCloud);
// Build the client with the acquired token as a credential.
RestClient client = RestClient.Configure()
.WithEnvironment(AzureEnvironment.AzureGlobalCloud)
.WithLogLevel(HttpLoggingDelegatingHandler.Level.Basic)
.WithCredentials(azureCredentials)
.Build();
// Authenticate against azure with the correct subscription to access.
var azure = Azure.Authenticate(client, AzureParts.Tenant)
.WithSubscription("subscriptionId");
// Search for the sql server and add a firewall rule.
var sqlServer = azure.SqlServers.GetByResourceGroup("ResourceGroup", "servername");
sqlServer.FirewallRules.Define("LetMeIn").WithIPAddress("yourIp").Create();
请注意,通过AcquireTokenAsync
获取登录令牌将打开登录窗口,因此无法以自动方式使用。如果您只想登录一次,则可以使用已存储的令牌提供令牌缓存。
我们可以使用Microsoft.Azure.Management.Fluent和Microsoft.Azure.Management.ResourceManager.Fluent,有关库的更多详细信息,请参阅packages.config。我们也可以从demo code获得更多的github。在编码之前,我们需要注册Azure AD应用程序并为其分配相应的角色,更多详细信息请参阅document。然后我们可以从Azure AD App获取clientId,密钥(密钥)和租户。之后我们可以用以下格式创建Auth file
subscription=########-####-####-####-############
client=########-####-####-####-############
tenant=########-####-####-####-############
key=XXXXXXXXXXXXXXXX
managementURI=https\://management.core.windows.net/
baseURL=https\://management.azure.com/
authURL=https\://login.windows.net/
graphURL=https\://graph.windows.net/
演示代码:
var credFile = @"File Path"; // example: c:\tom\auth.txt
var resourceGroup = "resource group";
var azureSQLServer = "Azure sql server name"; //just name of the Azure sql server such as tomdemo
var firewallRull = "firewall rull name";//such as myFirewallRull
var firewallRuleIPAddress = "x.x.x.x";//your Ip address
var credentials = SdkContext.AzureCredentialsFactory.FromFile(credFile);
var azure = Azure
.Configure()
.WithLogLevel(HttpLoggingDelegatingHandler.Level.Basic)
.Authenticate(credentials)
.WithDefaultSubscription();
var sqlServer = azure.SqlServers.GetByResourceGroup(resourceGroup, azureSQLServer);
var newFirewallRule = sqlServer.FirewallRules.Define(firewallRull)
.WithIPAddress(firewallRuleIPAddress)
.Create();
packages.config
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Microsoft.Azure.KeyVault.Core" version="1.0.0" targetFramework="net461" />
<package id="Microsoft.Azure.Management.AppService.Fluent" version="1.3.0" targetFramework="net461" />
<package id="Microsoft.Azure.Management.Batch.Fluent" version="1.3.0" targetFramework="net461" />
<package id="Microsoft.Azure.Management.Cdn.Fluent" version="1.3.0" targetFramework="net461" />
<package id="Microsoft.Azure.Management.Compute.Fluent" version="1.3.0" targetFramework="net461" />
<package id="Microsoft.Azure.Management.ContainerInstance.Fluent" version="1.3.0" targetFramework="net461" />
<package id="Microsoft.Azure.Management.ContainerRegistry.Fluent" version="1.3.0" targetFramework="net461" />
<package id="Microsoft.Azure.Management.CosmosDB.Fluent" version="1.3.0" targetFramework="net461" />
<package id="Microsoft.Azure.Management.Dns.Fluent" version="1.3.0" targetFramework="net461" />
<package id="Microsoft.Azure.Management.Fluent" version="1.3.0" targetFramework="net461" />
<package id="Microsoft.Azure.Management.Graph.RBAC.Fluent" version="1.3.0" targetFramework="net461" />
<package id="Microsoft.Azure.Management.KeyVault.Fluent" version="1.3.0" targetFramework="net461" />
<package id="Microsoft.Azure.Management.Network.Fluent" version="1.3.0" targetFramework="net461" />
<package id="Microsoft.Azure.Management.Redis.Fluent" version="1.3.0" targetFramework="net461" />
<package id="Microsoft.Azure.Management.ResourceManager.Fluent" version="1.3.0" targetFramework="net461" />
<package id="Microsoft.Azure.Management.Search.Fluent" version="1.3.0" targetFramework="net461" />
<package id="Microsoft.Azure.Management.ServiceBus.Fluent" version="1.3.0" targetFramework="net461" />
<package id="Microsoft.Azure.Management.Sql.Fluent" version="1.3.0" targetFramework="net461" />
<package id="Microsoft.Azure.Management.Storage.Fluent" version="1.3.0" targetFramework="net461" />
<package id="Microsoft.Azure.Management.TrafficManager.Fluent" version="1.3.0" targetFramework="net461" />
<package id="Microsoft.Data.Edm" version="5.8.2" targetFramework="net461" />
<package id="Microsoft.Data.OData" version="5.8.2" targetFramework="net461" />
<package id="Microsoft.Data.Services.Client" version="5.8.2" targetFramework="net461" />
<package id="Microsoft.IdentityModel.Clients.ActiveDirectory" version="2.28.3" targetFramework="net461" />
<package id="Microsoft.Rest.ClientRuntime" version="2.3.9" targetFramework="net461" />
<package id="Microsoft.Rest.ClientRuntime.Azure" version="3.3.10" targetFramework="net461" />
<package id="Microsoft.Rest.ClientRuntime.Azure.Authentication" version="2.3.1" targetFramework="net461" />
<package id="Newtonsoft.Json" version="6.0.8" targetFramework="net461" />
<package id="System.ComponentModel.EventBasedAsync" version="4.0.11" targetFramework="net461" />
<package id="System.Dynamic.Runtime" version="4.0.0" targetFramework="net461" />
<package id="System.Linq.Queryable" version="4.0.0" targetFramework="net461" />
<package id="System.Net.Requests" version="4.0.11" targetFramework="net461" />
<package id="System.Spatial" version="5.8.2" targetFramework="net461" />
<package id="WindowsAzure.Storage" version="8.1.4" targetFramework="net461" />
</packages>
对于PowerShell,您需要使用New-AzureRmSqlServerFirewallRule
。
如何使用 express 框架通过 node.js 获取远程客户端的本地 IP 地址?