2

I want a sample application in c#.net which can create users in Office 365 using Microsoft API .

I wish to do it in code, not using Powershell.

Kiquenet
  • 14,494
  • 35
  • 148
  • 243
bijupranavam
  • 35
  • 2
  • 9

1 Answers1

8

You can use the Microsoft Graph API - Create User:

Register a Native Client App on Azure AD, assign the "Microsoft Graph" > "Read and Write Directory Data" permission.

enter image description here

            string authority = "https://login.windows.net/yourdomain.onmicrosoft.com";

            string clientId = "{app_client_id}";

            Uri redirectUri = new Uri("http://localhost");

            string resourceUrl = "https://graph.microsoft.com";

            HttpClient client = new HttpClient();

            AuthenticationContext authenticationContext = new AuthenticationContext(authority, false);

            AuthenticationResult authenticationResult = authenticationContext.AcquireToken(resourceUrl,
                clientId, redirectUri, PromptBehavior.Always);

            client.DefaultRequestHeaders.Add("Authorization", "Bearer " + authenticationResult.AccessToken);

            string content = @"{
                'accountEnabled': true,
                'displayName': 'testuser',
                'mailNickname': 'test',
                'passwordProfile': {
                    'forceChangePasswordNextSignIn': true,
                    'password': 'pass@wd12345'
                },
                'userPrincipalName': 'testuser@yourdomain.onmicrosoft.com'
            }";

            var httpContent = new StringContent(content, Encoding.GetEncoding("utf-8"), "application/json");

            var response = client.PostAsync("https://graph.microsoft.com/v1.0/users", httpContent).Result;

            Console.WriteLine(response.Content.ReadAsStringAsync().Result);
Jeffrey Chen
  • 4,650
  • 1
  • 18
  • 22
  • An unhandled exception of type 'System.FormatException' occurred in Microsoft.IdentityModel.Clients.ActiveDirectory.dll – bijupranavam May 05 '16 at 06:29
  • Additional information: Index (zero based) must be greater than or equal to zero and less than the size of the argument list. – bijupranavam May 05 '16 at 06:30
  • Please ensure your replace the "app_client_id" with the actual app client id (e.g. dcd68e75-54d4-xxxx-9dfb-xxxx3833ec1a, without '{' and '}'). And ensure you replace the 'yourdomain', with your actual domain name. – Jeffrey Chen May 05 '16 at 07:47
  • you need to login with the tenant admin account. – Jeffrey Chen May 05 '16 at 08:32
  • you can cache the access token to avoid login repeatedly. BTW, this is just a sample to demo how to create a user through Microsoft Graph API. – Jeffrey Chen May 05 '16 at 11:20
  • My aim is to create users dynamically in office 365 using c#.net for my project. Is it possible by this method by modifying this code? – bijupranavam May 05 '16 at 12:17
  • Sorry leave all that....I misunderstood the requirement.my actual requirement is create alias for groups or email account in office 365 using c#.net code using api. – bijupranavam May 06 '16 at 05:27
  • 1
    If it answered your question about how to create a user through Microsoft Graph API, I suggest you closing this question. The answer will also help others who have the similar question. For the second one, you can ask another question about how to create alias for group. – Jeffrey Chen May 06 '16 at 05:34
  • Is Azure AD required to achieve connectivity to D365? Is there a "free" way of doing this aince Azure AD is a paid service? – Rafiki Jun 09 '17 at 20:47
  • Assign / remove licenses to user accounts ? – Kiquenet Jun 25 '18 at 15:15
  • 1
    I feel like this answer has a wealth of knowledge rather in comparison to all the useless GET commands Microsoft uses to demonstrate the Graph API. This is a very good answer. – Danijel-James W Nov 10 '21 at 22:15