无法解决C#Active Directory控制台应用程序中的Nuget包Moicrosoft.Graph.Auth问题

问题描述 投票:0回答:1

我正在尝试运行此代码:

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}");
        }
    }
}

但是我遇到错误

无法在Nuget PM中搜索Microsoft.Graph.Auth

也是从命令行失败的原因-如屏幕快照所示。

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

Install-Package:软件包还原失败。回滚“ AzureActiveDirectoryMurphy”的程序包更改。在第1行:char:1+安装包Microsoft.Graph.Auth-版本1.0.0-preview.3+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~+ CategoryInfo:未指定:(:) [Install-Package],异常+ FullyQualifiedErrorId:NuGetCmdletUnhandledException,NuGet.PackageManagement.PowerShellCmdlets.InstallPackageCommand

请帮助。

t.

c# azure-active-directory microsoft-graph nuget-package
1个回答
0
投票

此问题已解决,因为安装了Microsoft.identity.Client 4.8和4.7的依赖项导致Nuget软件包安装失败。因此,我安装了Microsoft.identity.Client 4.8,然后安装了Microsoft.Graph.Auth -Version 1.0.0-preview.3安装包。]

© www.soinside.com 2019 - 2024. All rights reserved.