Adding Application Security Group to Azure VM

Viewed 313

I need to add an existing ASG (Application Security Group ) to my existing NetworkInterface.

In my code below I can find my ASG and NetworkInterface... but I have no clue how to attach both of them together.

public void AddASG(string servername, string ASGName)
{
    IAzure azure = ConnectAzure();
    var ASG = azure.ApplicationSecurityGroups.List().Where(y => y.Name == ASGName).First();

    var nic = azure.NetworkInterfaces.List()
        .Where(y => y.Name.ToUpper().Contains(servername))
        .Select(x => x).First();

    nic.IPConfigurations?????;
}
1 Answers

It seems that such a feature is not possible currently. Please have a look at the same feature request, It's still open.

As a workaround, you could make this REST API request in order to associate the desired ASG to your Network Interface.

PUT https://management.azure.com/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Network/networkInterfaces/{networkInterfaceName}?api-version=2018-11-01

Send a JSON request with the format:

 {
  "location": "xxx",
  "properties": {
    "ipConfigurations": [
    {
      "name": "primary",
      "properties": 
      {
        "privateIPAllocationMethod": "Dynamic",
        "subnet": {
          "id": "/subscriptions/xxxxx/resourceGroups/xxx/providers/Microsoft.Network/virtualNetworks/xxx/subnets/xxx"
      },
        "applicationSecurityGroups" : [
          { 
            "id": "/subscriptions/xxx/resourceGroups/xxx/providers/Microsoft.Network/applicationSecurityGroups/xxx"
          }
        ]
      }
    }
    ]
 }
}

enter image description here Alternatively, you also could execute PowerShell scripts from C#

Hope this could help you.

Related