How to Invite user in Azure AD Programmaticaly usi

2019-03-04 11:30发布

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条回答
女痞
2楼-- · 2019-03-04 12:13

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

enter image description here

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;

enter image description here

查看更多
登录 后发表回答