This repository was archived by the owner on May 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathProgram.cs
More file actions
135 lines (118 loc) · 4.46 KB
/
Program.cs
File metadata and controls
135 lines (118 loc) · 4.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Security;
using System.Threading.Tasks;
using Microsoft.Identity.Client;
using Microsoft.Graph;
using Microsoft.Extensions.Configuration;
using Helpers;
using System.Threading.Tasks;
namespace graphconsoleapp
{
public class Program
{
public static void Main(string[] args)
{
Console.WriteLine("Hello World!");
var config = LoadAppSettings();
if (config == null)
{
Console.WriteLine("Invalid appsettings.json file.");
return;
}
var client = GetAuthenticatedGraphClient(config);
// request 1: create user
// var resultNewUser = CreateUserAsync(client);
// resultNewUser.Wait();
// Console.WriteLine("New user ID: " + resultNewUser.Id);
// request 2: update user
// (1/2) get the user we just created
var userToUpdate = client.Users.Request()
.Select("id")
.Filter("UserPrincipalName eq 'melissad@M365x23090844.onmicrosoft.com'")
.GetAsync()
.Result[0];
// (2/2) update the user's phone number
var resultUpdatedUser = UpdateUserAsync(client, userToUpdate.Id);
resultUpdatedUser.Wait();
Console.WriteLine("Updated user ID: " + resultUpdatedUser.Id);
// request 3: delete user
var deleteTask = DeleteUserAsync(client, userToUpdate.Id);
deleteTask.Wait();
}
private static async Task DeleteUserAsync(GraphServiceClient client, string userIdToDelete)
{
await client.Users[userIdToDelete].Request().DeleteAsync();
}
private static async Task<Microsoft.Graph.User> UpdateUserAsync(GraphServiceClient client, string userIdToUpdate)
{
Microsoft.Graph.User user = new Microsoft.Graph.User()
{
MobilePhone = "555-555-1212"
};
return await client.Users[userIdToUpdate].Request().UpdateAsync(user);
}
private static async Task<Microsoft.Graph.User> CreateUserAsync(GraphServiceClient client)
{
Microsoft.Graph.User user = new Microsoft.Graph.User()
{
AccountEnabled = true,
GivenName = "Melissa",
Surname = "Darrow",
DisplayName = "Melissa Darrow",
MailNickname = "MelissaD",
UserPrincipalName = "melissad@M365x23090844.onmicrosoft.com",
PasswordProfile = new PasswordProfile()
{
Password = "Password1!",
ForceChangePasswordNextSignIn = true
}
};
var requestNewUser = client.Users.Request();
return await requestNewUser.AddAsync(user);
}
private static IConfigurationRoot? LoadAppSettings()
{
try
{
var config = new ConfigurationBuilder()
.SetBasePath(System.IO.Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", false, true)
.Build();
if (string.IsNullOrEmpty(config["applicationId"]) ||
string.IsNullOrEmpty(config["tenantId"]))
{
return null;
}
return config;
}
catch (System.IO.FileNotFoundException)
{
return null;
}
}
private static IAuthenticationProvider CreateAuthorizationProvider(IConfigurationRoot config)
{
var clientId = config["applicationId"];
var authority = $"https://login.microsoftonline.com/{config["tenantId"]}/v2.0";
List<string> scopes = new List<string>();
scopes.Add("https://graph.microsoft.com/.default");
var cca = PublicClientApplicationBuilder.Create(clientId)
.WithAuthority(authority)
.WithDefaultRedirectUri()
.Build();
return MsalAuthenticationProvider.GetInstance(cca, scopes.ToArray());
}
private static GraphServiceClient GetAuthenticatedGraphClient(IConfigurationRoot config)
{
var authenticationProvider = CreateAuthorizationProvider(config);
var graphClient = new GraphServiceClient(authenticationProvider);
return graphClient;
}
}
}