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

妖精的绣舞 提交于 2019-12-02 08:09:11

问题


I am trying to invite a user in Azure B2B Active directory. I am not able to find a way to do that using Client SDK.

Is there a possible way to do that?

Thanks for your help in advance. :)


回答1:


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;



来源:https://stackoverflow.com/questions/48093480/how-to-invite-user-in-azure-ad-programmaticaly-using-microsoft-azure-activedirec

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