C# MSGraph SDK and ipNamedLocation

Viewed 15

I've been working on C# app to amend the ipaddress/s of a Named Location in conditional access in AAD.

I can authenticate and return the request collection. For whatever reason I cant access the isTrusted property or the ipRanges odata.

I can see the properties and the vales when I run through in debug, but cant output them.

I think its something to do with the list type, I'm using Microsoft.Graph.NamedLocation, there is Microsoft.Graph.IpNamedLocation type available but it can be converted from Microsoft.Graph.NamedLocation, which the api call makes.

The image shows what's available during runtime.

method code image

Code Below:

private static async Task GetnamedLocations(IConfidentialClientApplication app, string[] scopes)
{
    GraphServiceClient graphServiceClient = GetAuthenticatedGraphClient(app, scopes);

    var namedlocationsList = new List<Microsoft.Graph.NamedLocation>();
        
    var namedLocations = await graphServiceClient.Identity.ConditionalAccess.NamedLocations
         .Request()
         .Filter("isof('microsoft.graph.ipNamedLocation')")
         .GetAsync();

       // var ipNamedLocations = new List<Microsoft.Graph.IpNamedLocation>();

       

    namedlocationsList.AddRange(namedLocations.CurrentPage);

    foreach (var namedLocation in namedlocationsList)
    {
        Console.WriteLine(namedLocation.Id + namedLocation.DisplayName + namedLocation.ODataType + namedLocation);

        if (namedLocation.ODataType == "#microsoft.graph.ipNamedLocation")
        {
            Console.WriteLine("Write out all the properties");
        }
          
    }

      
    Console.WriteLine(($"Named location: {namedLocations}"));
}

Any pointers gratefully received, I'm not a C# developer so be gentle :-)

1 Answers

You need to cast namedLocation to IpNamedLocation type.

foreach (var namedLocation in namedlocationsList)
{
    Console.WriteLine(namedLocation.Id + namedLocation.DisplayName + namedLocation.ODataType + namedLocation);

    if (namedLocation is IpNamedLocation ipNamedLocation)
    {
        var isTrusted = ipNamedLocation.IsTrusted;
        var ipRanges = ipNamedLocation.IpRanges;

        Console.WriteLine("Write out all the properties");
    }
}
Related