Create Invite on Azure B2C with Graph API

Antoine - Mar 23 '22 - - Dev Community

Photo by Diana Polekhina on Unsplash

While reading this great post from Damien Bod, i had the idea to do the same for inviting users in Azure B2C.

Configure Permissions in Azure

Following the doc, we require the following:

  • permission User.Invite.All (permission type : Application) and grant Admin Consent

Image description

Get from your application:

  • your tenant ID
  • your Application ID
  • a Secret Value

Code

Once you have configured the permission, you can use the following code (with you IDs / Secret):

    string[] scopes = new string[1] { "https://graph.microsoft.com/.default"};
    var tenantId = "<YOUR_TENANT_ID>";

    // Values from app registration
    var clientId = "<YOUR_APPLICATION_ID>";
    var clientSecret = "<YOUR_SECRET>";

    var options = new TokenCredentialOptions
    {
        AuthorityHost = AzureAuthorityHosts.AzurePublicCloud,   
    };

    // https://docs.microsoft.com/dotnet/api/azure.identity.clientsecretcredential
    var clientSecretCredential = new ClientSecretCredential(
        tenantId, clientId, clientSecret, options);

    var _graphServiceClient = new GraphServiceClient(clientSecretCredential, scopes);


    var invitation = new Invitation()
    {
        InvitedUserEmailAddress= "<EMAIL>",
        InviteRedirectUrl ="<YOUR_APPLICATION_URL>",
        SendInvitationMessage = false,
    };

_graphServiceClient.Invitations.Request().AddAsync(invitation); 
Enter fullscreen mode Exit fullscreen mode

using the following NuGet packages :

Hope this helps !

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Terabox Video Player