DEV Community

Cover image for Create Invite on Azure B2C with Graph API
Antoine
Antoine

Posted on

2 2

Create Invite on Azure B2C with Graph API

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 !

Image of Timescale

Timescale – the developer's data platform for modern apps, built on PostgreSQL

Timescale Cloud is PostgreSQL optimized for speed, scale, and performance. Over 3 million IoT, AI, crypto, and dev tool apps are powered by Timescale. Try it free today! No credit card required.

Try free

Top comments (1)

Collapse
 
rafaelcaviquioli profile image
Rafael Caviquioli

After opening the invitation and inputting the verification code received by email, it redirects to my application login page, but does not ask the user to set its password.

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay