forked from microsoftgraph/dotnetcore-console-sample
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
157 lines (129 loc) · 6.13 KB
/
Program.cs
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
using Microsoft.Identity.Client;
using Microsoft.Graph;
using Microsoft.Extensions.Configuration;
using System.Linq;
namespace ConsoleGraphTest
{
class Program
{
private static GraphServiceClient _graphServiceClient;
private static HttpClient _httpClient;
static void Main(string[] args)
{
// Load appsettings.json
var config = LoadAppSettings();
if (null == config)
{
Console.WriteLine("Missing or invalid appsettings.json file. Please see README.md for configuration instructions.");
return;
}
//Query using Graph SDK (preferred when possible)
GraphServiceClient graphClient = GetAuthenticatedGraphClient(config);
IntuneHelperCall(config).GetAwaiter().GetResult();
}
private static async Task IntuneHelperCall(IConfigurationRoot config)
{
const string userPrincipalName = "<user>";
var graphClient = GetAuthenticatedGraphClient(config);
var intuneHelper = new IntuneHelper(graphClient);
await ListManagedDevices(intuneHelper, userPrincipalName);
WebApp app = await PublishWebApp(
intuneHelper,
"http://aka.ms/30DaysMsGraph",
"30 Days of MS Graph",
"Microsoft Corporation");
await AssignAppToAllUsers(intuneHelper, app);
DeviceConfiguration deviceConfiguration = await CreateWindowsDeviceConfiguration(
intuneHelper,
"Windows 10 Developer Configuration",
"http://aka.ms/30DaysMsGraph",
true);
await AssignDeviceConfigurationToAllDevices(intuneHelper, deviceConfiguration);
}
private static async Task ListManagedDevices(IntuneHelper intuneHelper, string userPrincipalName)
{
var managedDevices = await intuneHelper.ListManagedDevicesForUser(userPrincipalName);
Console.WriteLine($"Number of Intune managed devices for user {userPrincipalName}: {managedDevices.Count()}");
if(managedDevices.Count() > 0)
{
Console.WriteLine(managedDevices.Select(x => $"-- {x.DeviceName} : {x.Manufacturer} {x.Model}").Aggregate((x, y) => $"{x}\n{y}"));
}
}
private static async Task<WebApp> PublishWebApp(IntuneHelper intuneHelper, string url, string name, string publisher)
{
var webApp = await intuneHelper.PublishWebApp(url, name, publisher);
Console.WriteLine($"Published web app: {webApp.Id}: {webApp.DisplayName} - {webApp.AppUrl}");
return webApp;
}
private static async Task<DeviceConfiguration> CreateWindowsDeviceConfiguration(IntuneHelper intuneHelper, string displayName, string edgeHomePage, bool enableDeveloperMode)
{
var deviceConfiguration = await intuneHelper.CreateWindowsDeviceConfiguration(
displayName,
edgeHomePage,
enableDeveloperMode);
Console.WriteLine($"Created Device Configuration: {deviceConfiguration.Id}: {deviceConfiguration.DisplayName}");
return deviceConfiguration;
}
private static async Task AssignAppToAllUsers(IntuneHelper intuneHelper, MobileApp app)
{
var assignments = await intuneHelper.AssignAppToAllUsers(app);
Console.WriteLine($"App {app.DisplayName} has {assignments.Count()} assignments");
}
private static async Task AssignDeviceConfigurationToAllDevices(IntuneHelper intuneHelper, DeviceConfiguration deviceConfiguration)
{
var assignments = await intuneHelper.AssignDeviceConfigurationToAllDevices(deviceConfiguration);
Console.WriteLine($"Device Configuration {deviceConfiguration.DisplayName} has {assignments.Count()} assignments");
}
private static GraphServiceClient GetAuthenticatedGraphClient(IConfigurationRoot config)
{
var authenticationProvider = CreateAuthorizationProvider(config);
_graphServiceClient = new GraphServiceClient(authenticationProvider);
return _graphServiceClient;
}
private static HttpClient GetAuthenticatedHTTPClient(IConfigurationRoot config)
{
var authenticationProvider = CreateAuthorizationProvider(config);
_httpClient = new HttpClient(new AuthHandler(authenticationProvider, new HttpClientHandler()));
return _httpClient;
}
private static IAuthenticationProvider CreateAuthorizationProvider(IConfigurationRoot config)
{
var clientId = config["applicationId"];
var clientSecret = config["applicationSecret"];
var redirectUri = config["redirectUri"];
var authority = $"https://login.microsoftonline.com/{config["tenantId"]}";
List<string> scopes = new List<string>();
scopes.Add("https://graph.microsoft.com/.default");
var cca = new PublicClientApplication(clientId, authority);
return new DeviceCodeFlowAuthorizationProvider(cca, scopes);
}
private static IConfigurationRoot LoadAppSettings()
{
try
{
var config = new ConfigurationBuilder()
.SetBasePath(System.IO.Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", false, true)
.Build();
// Validate required settings
if (string.IsNullOrEmpty(config["applicationId"]) ||
string.IsNullOrEmpty(config["applicationSecret"]) ||
string.IsNullOrEmpty(config["redirectUri"]) ||
string.IsNullOrEmpty(config["tenantId"]))
{
return null;
}
return config;
}
catch (System.IO.FileNotFoundException)
{
return null;
}
}
}
}