通过Microsoft.Graph邀请用户会出错

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

Expected behavior

应邀请用户然后使用更多信息更新用户https://docs.microsoft.com/en-us/graph/api/invitation-post?view=graph-rest-1.0

Actual behavior

资源

"Microsoft.Graph.Core"

信息

"Code: Unauthorized\r\nMessage: Insufficient privileges to perform requested operation by the application '00000003-0000-0000-c000-000000000000'. ControllerName=MSGraphInviteAPI, ActionName=CreateInvite, URL absolute path=/api/9cXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX/invites\r\n\r\nInner error\r\n"

堆栈跟踪

   at Microsoft.Graph.HttpProvider.SendAsync(HttpRequestMessage request, HttpCompletionOption completionOption, CancellationToken cancellationToken)
   at Microsoft.Graph.BaseRequest.SendRequestAsync(Object serializableObject, CancellationToken cancellationToken, HttpCompletionOption completionOption)
   at Microsoft.Graph.BaseRequest.SendAsync[T](Object serializableObject, CancellationToken cancellationToken, HttpCompletionOption completionOption)
   at Web.Api.Infrastructure.Infrastructure.Repositories.AzureRepository.CreateUser(CreateUserCommand user) in C:\Users\SamPrecious\source\repos\Adept\src\adept-web-api\Web.Api.Infrastructure\Infrastructure\Repositories\AzureRepository.cs:line 166

Steps to reproduce the behavior

SDK版本:netcoreapp2.2 “Microsoft.Graph”Version =“1.13.0”“Microsoft.Graph.Core”Version =“1.13.0”

IDE版本:VSCode - 1.31.1 Visual Studio Enterprise 2017 - 4.7.03056

我有这个方法(移动代码以创建一个GraphServiceClient到方法来证明一个点)客户端使用App注册进行身份验证(我得到一个访问令牌)但是当Invite的代码运行时,authenticationContext.AcquireTokenAsync再次触发,上面的堆栈跟踪出错了。客户端应用程序具有User.Invite.All,User.ReadWrite.All,Directory.ReadWrite.All,根据API文档。

为什么AppId在代码中更改为上述消息中的一个?

public async Task<string> CreateUser(CreateUserCommand user)
        {
            try
            {
                var clientCredential = new ClientCredential(_configuration["appId"], _configuration["secret"]);
                var authenticationContext = new AuthenticationContext(_configuration["authority"]);
                var authenticationResult = authenticationContext.AcquireTokenAsync("https://graph.microsoft.com", clientCredential).Result;

                var delegateAuthProvider = new DelegateAuthenticationProvider((requestMessage) =>
                {
                    requestMessage.Headers.Authorization = new AuthenticationHeaderValue("bearer", authenticationResult.AccessToken);

                    return Task.FromResult(0);
                });

                var con = new GraphServiceClient(delegateAuthProvider);

                //invite user
                Invitation invitation = new Invitation();
                invitation.InvitedUserDisplayName = user.Name;
                invitation.SendInvitationMessage = true;
                invitation.InvitedUserEmailAddress = user.Email;
                invitation.InviteRedirectUrl = "http://localhost";
                var result = await con.Invitations.Request().AddAsync(invitation);

                //update user
                var aadUser = _connection.GetConnection().ActiveDirectoryUsers.GetById(result.Id);
                var updateUser = result.InvitedUser;
                updateUser.DisplayName = user.Name;
                updateUser.Surname = user.Surname;
                updateUser.Mail = user.Email;
                updateUser.MobilePhone = user.Phone;
                await con.Users[result.InvitedUser.Id].Request().UpdateAsync(updateUser);
                return aadUser.UserPrincipalName;
            }
            catch (System.Exception)
            {

                throw;
            }


        }

CreateUserCommand:

public class CreateUserCommand
    {
        public string Name { get; set; }
        public string Surname { get; set; }
        public string Email { get; set; }
        public string Phone { get; set; }
    }

这是应用注册的捕获

权限:

IConfiguration对象中的App Id

c# azure microsoft-graph asp.net-core-2.2
1个回答
1
投票

委派权限仅在您在用户上下文中调用API时才有意义。

由于您使用的是客户端凭据,因此没有用户。仅应用应用程序权限,因此请在此处设置正确的权限。

此外,由于您具有异步功能,因此最好还等待令牌:

var authenticationResult = await authenticationContext.AcquireTokenAsync("https://graph.microsoft.com", clientCredential);
© www.soinside.com 2019 - 2024. All rights reserved.