使用C#创建Azure NSG

时间:2018-01-22 17:23:40

标签: c# azure networking

我正在研究在Azure中自动创建NSG规则的所有方法。我可以使用Powershell,Template和REST api创建它们。是否可以仅使用C#创建NSG规则?当我谷歌这个时,绝对不会出现直接的C#。

1 个答案:

答案 0 :(得分:0)

来自此网站:https://github.com/Azure/azure-libraries-for-net

        string AuthFile = "path to authorization file";
        var credentials = SdkContext.AzureCredentialsFactory.FromFile(AuthFile);

        var azure = Microsoft.Azure.Management.Fluent.Azure.Configure().WithLogLevel(HttpLoggingDelegatingHandler.Level.Basic).Authenticate(credentials).WithDefaultSubscription();

        string frontEndNSGName = "testnsg";
        string rgName = "MyResourceGroup";

        var frontEndNSG = azure.NetworkSecurityGroups.Define(frontEndNSGName)
            .WithRegion(Region.USEast)
            .WithExistingResourceGroup(rgName)
            .DefineRule("ALLOW-SSH")
                .AllowInbound()
                .FromAnyAddress()
                .FromAnyPort()
                .ToAnyAddress()
                .ToPort(22)
                .WithProtocol(SecurityRuleProtocol.Tcp)
                .WithPriority(100)
                .WithDescription("Allow SSH")
                .Attach()
            .DefineRule("ALLOW-HTTP")
                .AllowInbound()
                .FromAnyAddress()
                .FromAnyPort()
                .ToAnyAddress()
                .ToPort(80)
                .WithProtocol(SecurityRuleProtocol.Tcp)
                .WithPriority(101)
                .WithDescription("Allow HTTP")
                .Attach()
            .Create();