How to Invite user in Azure AD Programmaticaly using Microsoft.Azure.ActiveDirectory.GraphClient sdk

ε祈祈猫儿з 提交于 2019-12-02 07:15:47

Is there a possible way to do that?

I can't find a method to invite a user with Microsoft.Azure.ActiveDirectory.GraphClient.

But we could do that with Microsoft.Graph. And Azure official document also recommend that you use Microsoft Graph instead of Azure AD Graph API.

We strongly recommend that you use Microsoft Graph instead of Azure AD Graph API to access Azure Active Directory resources. https://docs.microsoft.com/en-us/azure/active-directory/develop/active-directory-graph-api

I also do a demo for it. I works correctly on my side.

Before that I create the Web application in the Azure directory. Add required Invite guest users to the organization permission for Microsoft Graph

Demo code:

string authority = "https://login.microsoftonline.com/{0}";
string graphResourceId = "https://graph.microsoft.com";
string tenantId = "tenant id";
string clientId = "client Id";
string secret = "sercet key";
authority = String.Format(authority, tenantId);
AuthenticationContext authContext = new AuthenticationContext(authority);
var accessToken = authContext.AcquireTokenAsync(graphResourceId, new ClientCredential(clientId, secret)).Result.AccessToken;
var graphserviceClient = new GraphServiceClient(
                new DelegateAuthenticationProvider(
                    requestMessage =>
                    {
                        requestMessage.Headers.Authorization = new AuthenticationHeaderValue("bearer", accessToken);

                        return Task.FromResult(0);
                    }));
var dic = new Dictionary<string, object> {{"@odata.type", "microsoft.graph.invitedUserMessageInfo"}};

Invitation invitation = new Invitation
            {
                InvitedUserEmailAddress = "email address",
                InvitedUserMessageInfo = new InvitedUserMessageInfo{AdditionalData = dic },
                InvitedUserDisplayName = "xxx",
                SendInvitationMessage = false,
                InviteRedirectUrl = "xxxxx"

            };
 var result = graphserviceClient.Invitations.Request().AddAsync(invitation).Result;

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!