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

后端 未结 1 1181
忘了有多久
忘了有多久 2021-01-25 06:47

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 f

相关标签:
1条回答
  • 2021-01-25 06:52

    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;
    

    0 讨论(0)
提交回复
热议问题