youeee
youeee

Reputation: 105

Azure Webapp - Azure .Net SDK - Setting Site IP Restrictions

I am attempting to use the Azure .Net SDK in order to retrieve and then manipulate resources within a webapp.

I specifically wish to set Service IP restrictions as described here https://learn.microsoft.com/en-us/azure/app-service/app-service-ip-restrictions#programmatic-manipulation-of-access-restriction-rules but it has to be manipulated via the SDK. I understand generic resources may be the best option for this ?

I have the following code in which I can get the website info however I am at a loss at how I determine the resource id of child resources of get a list of the resources

            var credentials = new AzureCredentials(servicePrincipal, tenantId, AzureEnvironment.AzureGlobalCloud);
            IAzure azure = Azure.Authenticate(credentials).WithSubscription(subscriptionId);


            IWebApp webapp = servicePlan.Manager.WebApps.List().First();
IGenericResource grWebsiteByName = webapp.Manager.ResourceManager.GenericResources.GetById(" "/subscriptions/xxx-xxx-xxx/resourceGroups/MyResourceGroup/providers/Microsoft.Web/sites/MyFirstAzureWebsite-xxx");

Upvotes: 1

Views: 185

Answers (1)

Joey Cai
Joey Cai

Reputation: 20127

You could use SiteConfigResourceInner.IpSecurityRestrictions to set IP security restrictions for webapp. The below code is used to set ip restriction. And you could follow this article to get more details.

foreach (var webApp in await azure.WithSubscription(subscription.SubscriptionId).WebApps.ListByResourceGroupAsync(resourceGroup.Name))
{
    SiteConfigResourceInner siteConfigResourceInner = await webSiteManagementClient.WebApps.GetConfigurationAsync(resourceGroup.Name, webApp.Name);
    siteConfigResourceInner.IpSecurityRestrictions.Add(new IpSecurityRestriction("0.0.0.0", "0.0.0.0", "allow"));
    webApp.Inner.SiteConfig = new SiteConfig();

    foreach (PropertyInfo propertyInfo in webApp.Inner.SiteConfig.GetType().GetProperties())
    {
        var value = siteConfigResourceInner.GetType().GetProperty(propertyInfo.Name).GetValue(siteConfigResourceInner, null);
        propertyInfo.SetValue(webApp.Inner.SiteConfig, siteConfigResourceInner.GetType().GetProperty(propertyInfo.Name).GetValue(siteConfigResourceInner));
    }
}

Upvotes: 2

Related Questions