|
1 | | -// Copyright (c) Microsoft Corporation. All rights reserved. |
| 1 | +// Copyright (c) Microsoft Corporation. |
2 | 2 | // Licensed under the MIT license. |
3 | 3 |
|
4 | 4 | using Azure.Core; |
5 | 5 | using Azure.Identity; |
6 | 6 | using Microsoft.Graph; |
7 | 7 | using Microsoft.Graph.Models; |
8 | 8 |
|
9 | | -class GraphHelper |
| 9 | +namespace GraphAppOnlyTutorial; |
| 10 | + |
| 11 | +public class GraphHelper |
10 | 12 | { |
11 | | - // <AppOnlyAuthConfigSnippet> |
| 13 | + /* <AppOnlyAuthConfigSnippet> */ |
12 | 14 | // Settings object |
13 | | - private static Settings? _settings; |
| 15 | + private static Settings? settings; |
| 16 | + |
14 | 17 | // App-ony auth token credential |
15 | | - private static ClientSecretCredential? _clientSecretCredential; |
| 18 | + private static ClientSecretCredential? clientSecretCredential; |
| 19 | + |
16 | 20 | // Client configured with app-only authentication |
17 | | - private static GraphServiceClient? _appClient; |
| 21 | + private static GraphServiceClient? appClient; |
18 | 22 |
|
19 | 23 | public static void InitializeGraphForAppOnlyAuth(Settings settings) |
20 | 24 | { |
21 | | - _settings = settings; |
| 25 | + GraphHelper.settings = settings; |
22 | 26 |
|
23 | 27 | // Ensure settings isn't null |
24 | 28 | _ = settings ?? |
25 | | - throw new System.NullReferenceException("Settings cannot be null"); |
| 29 | + throw new NullReferenceException("Settings cannot be null"); |
26 | 30 |
|
27 | | - _settings = settings; |
| 31 | + GraphHelper.settings = settings; |
28 | 32 |
|
29 | | - if (_clientSecretCredential == null) |
30 | | - { |
31 | | - _clientSecretCredential = new ClientSecretCredential( |
32 | | - _settings.TenantId, _settings.ClientId, _settings.ClientSecret); |
33 | | - } |
| 33 | + clientSecretCredential ??= new ClientSecretCredential( |
| 34 | + GraphHelper.settings.TenantId, GraphHelper.settings.ClientId, GraphHelper.settings.ClientSecret); |
34 | 35 |
|
35 | | - if (_appClient == null) |
36 | | - { |
37 | | - _appClient = new GraphServiceClient(_clientSecretCredential, |
38 | | - // Use the default scope, which will request the scopes |
39 | | - // configured on the app registration |
40 | | - new[] {"https://graph.microsoft.com/.default"}); |
41 | | - } |
| 36 | + appClient ??= new GraphServiceClient( |
| 37 | + clientSecretCredential, |
| 38 | + /* Use the default scope, which will request the scopes |
| 39 | + configured on the app registration */ |
| 40 | + ["https://graph.microsoft.com/.default"]); |
42 | 41 | } |
43 | | - // </AppOnlyAuthConfigSnippet> |
| 42 | + /* </AppOnlyAuthConfigSnippet> */ |
44 | 43 |
|
45 | | - // <GetAppOnlyTokenSnippet> |
| 44 | + /* <GetAppOnlyTokenSnippet> */ |
46 | 45 | public static async Task<string> GetAppOnlyTokenAsync() |
47 | 46 | { |
48 | 47 | // Ensure credential isn't null |
49 | | - _ = _clientSecretCredential ?? |
50 | | - throw new System.NullReferenceException("Graph has not been initialized for app-only auth"); |
| 48 | + _ = clientSecretCredential ?? |
| 49 | + throw new NullReferenceException("Graph has not been initialized for app-only auth"); |
51 | 50 |
|
52 | 51 | // Request token with given scopes |
53 | | - var context = new TokenRequestContext(new[] {"https://graph.microsoft.com/.default"}); |
54 | | - var response = await _clientSecretCredential.GetTokenAsync(context); |
| 52 | + var context = new TokenRequestContext(["https://graph.microsoft.com/.default"]); |
| 53 | + var response = await clientSecretCredential.GetTokenAsync(context); |
55 | 54 | return response.Token; |
56 | 55 | } |
57 | | - // </GetAppOnlyTokenSnippet> |
| 56 | + /* </GetAppOnlyTokenSnippet> */ |
58 | 57 |
|
59 | | - // <GetUsersSnippet> |
| 58 | + /* <GetUsersSnippet> */ |
60 | 59 | public static Task<UserCollectionResponse?> GetUsersAsync() |
61 | 60 | { |
62 | 61 | // Ensure client isn't null |
63 | | - _ = _appClient ?? |
64 | | - throw new System.NullReferenceException("Graph has not been initialized for app-only auth"); |
| 62 | + _ = appClient ?? |
| 63 | + throw new NullReferenceException("Graph has not been initialized for app-only auth"); |
65 | 64 |
|
66 | | - return _appClient.Users.GetAsync((config) => |
| 65 | + return appClient.Users.GetAsync((config) => |
67 | 66 | { |
68 | | - // Only request specific properties |
69 | | - config.QueryParameters.Select = new[] { "displayName", "id", "mail" }; |
70 | | - // Get at most 25 results |
| 67 | + /* Only request specific properties */ |
| 68 | + config.QueryParameters.Select = ["displayName", "id", "mail"]; |
| 69 | + /* Get at most 25 results */ |
71 | 70 | config.QueryParameters.Top = 25; |
72 | | - // Sort by display name |
73 | | - config.QueryParameters.Orderby = new[] { "displayName" }; |
| 71 | + /* Sort by display name */ |
| 72 | + config.QueryParameters.Orderby = ["displayName"]; |
74 | 73 | }); |
75 | 74 | } |
76 | | - // </GetUsersSnippet> |
| 75 | + /* </GetUsersSnippet> */ |
77 | 76 |
|
78 | | - #pragma warning disable CS1998 |
79 | | - // <MakeGraphCallSnippet> |
80 | | - // This function serves as a playground for testing Graph snippets |
81 | | - // or other code |
82 | | - public async static Task MakeGraphCallAsync() |
| 77 | +#pragma warning disable CS1998 |
| 78 | + /* <MakeGraphCallSnippet> */ |
| 79 | + /* This function serves as a playground for testing Graph snippets |
| 80 | + or other code */ |
| 81 | + public static async Task MakeGraphCallAsync() |
83 | 82 | { |
84 | 83 | // INSERT YOUR CODE HERE |
85 | 84 | } |
86 | | - // </MakeGraphCallSnippet> |
| 85 | + /* </MakeGraphCallSnippet> */ |
87 | 86 | } |
0 commit comments