0
votes

I am trying to run this code:

using System;
using Microsoft.Graph;
using Microsoft.Graph.Auth;
using Microsoft.Identity.Client;
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;

namespace AzureActiveDirectoryMurphy
{
    class Program
    {
        //3. Copy the following code as is to your application.

        // Register your app on the Azure AD application registration portal 
        // Remember to :
        // 1. Check the redirect uri starting with "msal"
        // 2. Set "Treat application as public client" to "Yes"
        const string clientId = "XXXXXX";
        const string tenant = "XXXX.onmicrosoft.com";
        const string redirectUri = "XXXXXX://auth";

        // Change the following between each call to create/update user if not deleting the user
        private static string givenName = "test99";
        private static string surname = "user99";

        private static void Main(string[] args)
        {
            // Initialize and prepare MSAL
            // What we want to do 
            string[] scopes = new string[] { "user.read", "user.readwrite.all" };

            IPublicClientApplication app = PublicClientApplicationBuilder.Create(clientId)
                .WithAuthority(new Uri($"https://login.microsoftonline.com/{tenant}"))
                .WithRedirectUri(redirectUri)
                .Build();

            // Initialize the Graph SDK authentication provider
            InteractiveAuthenticationProvider authenticationProvider = new InteractiveAuthenticationProvider(app, scopes);
            GraphServiceClient graphServiceClient = new GraphServiceClient(authenticationProvider);

            // Get information from Graph about the currently signed-In user
            Console.WriteLine("--Fetching details of the currently signed-in user--");
            GetMeAsync(graphServiceClient).GetAwaiter().GetResult();
            Console.WriteLine("---------");

            // Create a new user
            Console.WriteLine($"--Creating a new user in the tenant '{tenant}'--");
            User newUser = CreateUserAsync(graphServiceClient).Result;
            PrintUserDetails(newUser);
            Console.WriteLine("---------");

            // Update an existing user
            if (newUser != null)
            {
                Console.WriteLine("--Updating the detail of an existing user--");
                User updatedUser = UpdateUserAsync(graphServiceClient, userId: newUser.Id, jobTitle: "Program Manager").Result;
                PrintUserDetails(updatedUser);
                Console.WriteLine("---------");
            }

            // List existing users
            Console.WriteLine("--Listing all users in the tenant--");
            List<User> users = GetUsersAsync(graphServiceClient).Result;

            users.ForEach(u => PrintUserDetails(u));
            Console.WriteLine("---------");

            // Delete this user
            Console.WriteLine("--Deleting a user in the tenant--");

            if (newUser != null)
            {
                 DeleteUserAsync(graphServiceClient, newUser?.Id).GetAwaiter().GetResult(); ;
            }

            Console.WriteLine("---------");

            // List existing users after deletion
            Console.WriteLine("--Listing all users in the tenant after deleting a user.--");
            users = GetUsersAsync(graphServiceClient).Result;
            users.ForEach(u => PrintUserDetails(u));
            Console.WriteLine("---------");

            Console.WriteLine("Press any key to exit");
            Console.ReadKey();
        }
    }    

    private static async Task GetMeAsync(GraphServiceClient graphServiceClient)
    {
        // Call /me Api
        var me = await graphServiceClient.Me.Request().GetAsync();
        Console.WriteLine($"Display Name from /me->{me.DisplayName}");

        var directreports = await graphServiceClient.Me.DirectReports.Request().GetAsync();

        foreach (User user in directreports.CurrentPage)
        {
            Console.WriteLine($"Report's Display Name ->{user.DisplayName}");
        }
    }

    private static async Task<User> CreateUserAsync(GraphServiceClient graphServiceClient)
    {
        User newUserObject = null;

        string displayname = $"{givenName} {surname}";
        string mailNickName = $"{givenName}{surname}";
        string upn = $"{mailNickName}{tenant}";
        string password = "p@$$w0rd!";

        try
        {
            newUserObject = await graphServiceClient.Users.Request().AddAsync(new User
            {
                AccountEnabled = true,
                DisplayName = displayname,
                MailNickname = mailNickName,
                GivenName = givenName,
                Surname = surname,
                PasswordProfile = new PasswordProfile
                {
                    Password = password
                },
                UserPrincipalName = upn
            });
        }
        catch (ServiceException e)
        {
            Console.WriteLine("We could not add a new user: " + e.Error.Message);
            return null;
        }

        return newUserObject;
    }

    private static void PrintUserDetails(User user)
    {
        if (user != null)
        {
            Console.WriteLine($"DisplayName-{user.DisplayName}, MailNickname- {user.MailNickname}, GivenName-{user.GivenName}, Surname-{user.Surname}, Upn-{user.UserPrincipalName}, JobTitle-{user.JobTitle}, Id-{user.Id}");
        }
        else
        {
            Console.WriteLine("The provided User is null!");
        }
    }

    private static async Task<User> UpdateUserAsync(GraphServiceClient graphServiceClient, string userId, string jobTitle)
    {
        User updatedUser = null;

        try
        {
            // Update the user.
            updatedUser = await graphServiceClient.Users[userId].Request().UpdateAsync(new User
            {
                JobTitle = jobTitle
            });
        }
        catch (ServiceException e)
        {
            Console.WriteLine($"We could not update details of the user with Id {userId}: " + $"{e}");
        }

        return updatedUser;
    }

    private static async Task<List<User>> GetUsersAsync(GraphServiceClient graphServiceClient)
    {
        List<User> allUsers = new List<User>();

        try
        {
            IGraphServiceUsersCollectionPage users = await graphServiceClient.Users.Request().Top(5).GetAsync();

            // When paginating
            //while(users.NextPageRequest != null)
            //{
            //    users = await users.NextPageRequest.GetAsync();
            //}

            if (users?.CurrentPage.Count > 0)
            {
                foreach (User user in users)
                {
                    allUsers.Add(user);
                }
            }
        }
        catch (ServiceException e)
        {
            Console.WriteLine("We could not retrieve the user's list: " + $"{e}");
            return null;
        }

        return allUsers;
    }

    private static async Task DeleteUserAsync(GraphServiceClient graphServiceClient, string userId)
    {
        try
        {
            await graphServiceClient.Users[userId].Request().DeleteAsync();
        }
        catch (ServiceException e)
        {
            Console.WriteLine($"We could not delete the user with Id-{userId}: " + $"{e}");
        }
    }
}

But I'm getting an error

Unable to search Microsoft.Graph.Auth in Nuget PM

and also from command line its failing - shown in the screenshot.

Install-Package Microsoft.Graph.Auth -Version 1.0.0-preview.3

Install-Package : Package restore failed. Rolling back package changes for 'AzureActiveDirectoryMurphy'.
At line:1 char:1
+ Install-Package Microsoft.Graph.Auth -Version 1.0.0-preview.3
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Install-Package], Exception
+ FullyQualifiedErrorId : NuGetCmdletUnhandledException,NuGet.PackageManagement.PowerShellCmdlets.InstallPackageCommand

Please help.

t.

1
Is your nuget up to date? This package has a SemVer 2.0.0 package version. This package will only be available to download with SemVer 2.0.0 compatible NuGet clients, such as Visual Studio 2017 (version 15.3) and above or NuGet client 4.3.0 and above.Stefan
I am using Visual studio 2019 preview and NuGet version is : Version : 5.5.0.6293AllTech
Okay, that seems okay. Although 2019 is available as release version. It might be helpful to upgrade, because, I am not sure if the package relies on features of the latest .net (core) versions. I could be that the package targets a newer framework than is available on your system.Stefan
So, what should be the next step.AllTech
Hi, I just tested this myself on a non-preview 2019 .net Core application - it works perfectly. So; next step would be to provide some details about your setup / application. What kind of project are you using? Meanwhile you should update your visual studio to the release version.Stefan

1 Answers

2
votes

This issue is resolved as Nuget package Install was failing because of dependency was there for Microsoft.identity.Client 4.8 and 4.7 was installed . So,i installed Microsoft.identity.Client 4.8 and then Install-Package Microsoft.Graph.Auth -Version 1.0.0-preview.3 worked.