如何通过Powershell或C#使用本地IP远程更新Azure SQL防火墙 [英] How to remotely update azure SQL firewall with local IP via powershell or C#
问题描述
如何使用本地PC的IP地址远程更新我的Azure SQL防火墙,以便通过Powershell或C#进行管理.
How can I remotely update my azure SQL firewall with my local PCs IP address for administration either via powershell or C#.
我看过文档,但只能从实际服务器本身而不是远程那里找到方法.
I have had a look over the documentation but I can only find out how to do it from the actual server itself, not remotely.
我可以让Visual Studio弹出并询问我是否要将我的IP地址随机添加到防火墙,但是大多数情况下它只是失败了并且不问我,因此我正在寻找除手动登录外的解决方案每当我的IP地址更改时,就进入Azure门户.
I can get visual studio to pop up and ask me if I want to add my IP address to the firewall randomly, but most the time it simply fails and does not ask me so I am looking for a solution other than manually logging into the azure portal whenever my IP address changes.
查找示例或指向显示要使用的库的文档的链接.
Looking for either an example or a link to documentation that shows which libraries to use.
推荐答案
From C# you can access all Azure resources via Microsoft.Azure.Management.Fluent like this
// 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
获取登录令牌将打开一个登录窗口,因此不能以自动方式使用.如果您只想登录一次,则可以为令牌缓存提供一个已经存储的令牌.
Note that acquiring the login token via AcquireTokenAsync
will open a login window and thus can not be used in an automated fashion. You could provide a token-cache with an already stored token if you want to only login once.
这篇关于如何通过Powershell或C#使用本地IP远程更新Azure SQL防火墙的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!