使用 Microsoft Graph API 在 Azure AD 中创建来宾用户

Sri*_*evi 1 azure-active-directory microsoft-graph-api

我尝试使用 Microsoft Graph API 创建来宾用户。我使用了该财产UserType

user.UserType =  "Guest";
Run Code Online (Sandbox Code Playgroud)

但回应显示Invalid User principal Name

我可以在门户中创建相同的用户。

Fei*_*SFT 8

要将外部用户添加到组织中,我们需要使用邀请 REST,而不是直接创建用户。以下是 REST 和代码示例(Microsoft Graph SDK)供您参考:

POST https://graph.microsoft.com/v1.0/invitations
Content-type: application/json
Content-length: 551

{
  "invitedUserEmailAddress": "yyy@test.com",
  "inviteRedirectUrl": "https://myapp.com"
}
Run Code Online (Sandbox Code Playgroud)

代码示例:

string accessToken = "";
var graphserviceClient = new GraphServiceClient(
new DelegateAuthenticationProvider(
       (requestMessage) =>
      {
          requestMessage.Headers.Authorization = new AuthenticationHeaderValue("bearer", accessToken);

          return Task.FromResult(0);
 }));

Invitation invitation = new Invitation();
invitation.SendInvitationMessage = true;
invitation.InvitedUserEmailAddress = "xxxx@hotmail.com";
invitation.InviteRedirectUrl = "http://localhost";
var result= graphserviceClient.Invitations.Request().AddAsync(invitation).Result;
Run Code Online (Sandbox Code Playgroud)