Skip to content

Commit 4ddc57e

Browse files
authored
Merge pull request #436 from microsoftgraph/dotnet9
Updated to .NET9 and updated dependencies
2 parents 7ee3b50 + e2445ad commit 4ddc57e

11 files changed

Lines changed: 222 additions & 158 deletions

File tree

.editorconfig

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[*]
2+
indent_style = space
3+
indent_size = 2
4+
insert_final_newline = true
5+
6+
[*.{cs,cshtml}]
7+
indent_size = 4
8+
dotnet_diagnostic.SA1101.severity = silent
9+
dotnet_diagnostic.SA1513.severity = silent
10+
dotnet_diagnostic.SA1600.severity = silent
11+
dotnet_diagnostic.CS1591.severity = silent

.vscode/launch.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"request": "launch",
1111
"preLaunchTask": "build user-auth",
1212
// If you have changed target frameworks, make sure to update the program path.
13-
"program": "${workspaceFolder}/user-auth/GraphTutorial/bin/Debug/net8.0/GraphTutorial.dll",
13+
"program": "${workspaceFolder}/user-auth/GraphTutorial/bin/Debug/net9.0/GraphTutorial.dll",
1414
"args": [],
1515
"cwd": "${workspaceFolder}/user-auth/GraphTutorial",
1616
// For more information about the 'console' field, see https://aka.ms/VSCode-CS-LaunchJson-Console
@@ -26,7 +26,7 @@
2626
"request": "launch",
2727
"preLaunchTask": "build app-auth",
2828
// If you have changed target frameworks, make sure to update the program path.
29-
"program": "${workspaceFolder}/app-auth/GraphAppOnlyTutorial/bin/Debug/net8.0/GraphAppOnlyTutorial.dll",
29+
"program": "${workspaceFolder}/app-auth/GraphAppOnlyTutorial/bin/Debug/net9.0/GraphAppOnlyTutorial.dll",
3030
"args": [],
3131
"cwd": "${workspaceFolder}/app-auth/GraphAppOnlyTutorial",
3232
// For more information about the 'console' field, see https://aka.ms/VSCode-CS-LaunchJson-Console
@@ -39,4 +39,4 @@
3939
"request": "attach"
4040
}
4141
]
42-
}
42+
}
Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

3+
<!-- cSpell:ignore stylecop contentfiles buildtransitive -->
4+
35
<PropertyGroup>
46
<OutputType>Exe</OutputType>
5-
<TargetFramework>net8.0</TargetFramework>
7+
<TargetFramework>net9.0</TargetFramework>
68
<ImplicitUsings>enable</ImplicitUsings>
79
<Nullable>enable</Nullable>
810
<UserSecretsId>2275df63-2eb2-47b9-a11f-710535686d4b</UserSecretsId>
11+
<GenerateDocumentationFile>true</GenerateDocumentationFile>
912
</PropertyGroup>
1013

1114
<ItemGroup>
@@ -14,12 +17,20 @@
1417
</None>
1518
</ItemGroup>
1619

20+
<ItemGroup>
21+
<AdditionalFiles Include="../../stylecop.json" Link="stylecop.json" />
22+
</ItemGroup>
23+
1724
<ItemGroup>
1825
<PackageReference Include="Azure.Identity" Version="1.17.0" />
19-
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="8.0.2" />
20-
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="8.0.1" />
21-
<PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="8.0.1" />
22-
<PackageReference Include="Microsoft.Graph" Version="5.93.0" />
26+
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="9.0.10" />
27+
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="9.0.10" />
28+
<PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="9.0.10" />
29+
<PackageReference Include="Microsoft.Graph" Version="5.95.0" />
30+
<PackageReference Include="StyleCop.Analyzers" Version="1.2.0-beta.556">
31+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
32+
<PrivateAssets>all</PrivateAssets>
33+
</PackageReference>
2334
</ItemGroup>
2435

2536
</Project>
Lines changed: 43 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,87 +1,86 @@
1-
// Copyright (c) Microsoft Corporation. All rights reserved.
1+
// Copyright (c) Microsoft Corporation.
22
// Licensed under the MIT license.
33

44
using Azure.Core;
55
using Azure.Identity;
66
using Microsoft.Graph;
77
using Microsoft.Graph.Models;
88

9-
class GraphHelper
9+
namespace GraphAppOnlyTutorial;
10+
11+
public class GraphHelper
1012
{
11-
// <AppOnlyAuthConfigSnippet>
13+
/* <AppOnlyAuthConfigSnippet> */
1214
// Settings object
13-
private static Settings? _settings;
15+
private static Settings? settings;
16+
1417
// App-ony auth token credential
15-
private static ClientSecretCredential? _clientSecretCredential;
18+
private static ClientSecretCredential? clientSecretCredential;
19+
1620
// Client configured with app-only authentication
17-
private static GraphServiceClient? _appClient;
21+
private static GraphServiceClient? appClient;
1822

1923
public static void InitializeGraphForAppOnlyAuth(Settings settings)
2024
{
21-
_settings = settings;
25+
GraphHelper.settings = settings;
2226

2327
// Ensure settings isn't null
2428
_ = settings ??
25-
throw new System.NullReferenceException("Settings cannot be null");
29+
throw new NullReferenceException("Settings cannot be null");
2630

27-
_settings = settings;
31+
GraphHelper.settings = settings;
2832

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);
3435

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"]);
4241
}
43-
// </AppOnlyAuthConfigSnippet>
42+
/* </AppOnlyAuthConfigSnippet> */
4443

45-
// <GetAppOnlyTokenSnippet>
44+
/* <GetAppOnlyTokenSnippet> */
4645
public static async Task<string> GetAppOnlyTokenAsync()
4746
{
4847
// 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");
5150

5251
// 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);
5554
return response.Token;
5655
}
57-
// </GetAppOnlyTokenSnippet>
56+
/* </GetAppOnlyTokenSnippet> */
5857

59-
// <GetUsersSnippet>
58+
/* <GetUsersSnippet> */
6059
public static Task<UserCollectionResponse?> GetUsersAsync()
6160
{
6261
// 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");
6564

66-
return _appClient.Users.GetAsync((config) =>
65+
return appClient.Users.GetAsync((config) =>
6766
{
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 */
7170
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"];
7473
});
7574
}
76-
// </GetUsersSnippet>
75+
/* </GetUsersSnippet> */
7776

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()
8382
{
8483
// INSERT YOUR CODE HERE
8584
}
86-
// </MakeGraphCallSnippet>
85+
/* </MakeGraphCallSnippet> */
8786
}

app-auth/GraphAppOnlyTutorial/Program.cs

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
// Copyright (c) Microsoft Corporation. All rights reserved.
1+
// Copyright (c) Microsoft Corporation.
22
// Licensed under the MIT license.
33

4-
// <ProgramSnippet>
4+
/* <ProgramSnippet> */
5+
using GraphAppOnlyTutorial;
6+
57
Console.WriteLine(".NET Graph App-only Tutorial\n");
68

79
var settings = Settings.LoadSettings();
@@ -29,7 +31,7 @@
2931
choice = -1;
3032
}
3133

32-
switch(choice)
34+
switch (choice)
3335
{
3436
case 0:
3537
// Exit the program
@@ -52,16 +54,16 @@
5254
break;
5355
}
5456
}
55-
// </ProgramSnippet>
57+
/* </ProgramSnippet> */
5658

57-
// <InitializeGraphSnippet>
59+
/* <InitializeGraphSnippet> */
5860
void InitializeGraph(Settings settings)
5961
{
6062
GraphHelper.InitializeGraphForAppOnlyAuth(settings);
6163
}
62-
// </InitializeGraphSnippet>
64+
/* </InitializeGraphSnippet> */
6365

64-
// <DisplayAccessTokenSnippet>
66+
/* <DisplayAccessTokenSnippet> */
6567
async Task DisplayAccessTokenAsync()
6668
{
6769
try
@@ -74,9 +76,9 @@ async Task DisplayAccessTokenAsync()
7476
Console.WriteLine($"Error getting app-only access token: {ex.Message}");
7577
}
7678
}
77-
// </DisplayAccessTokenSnippet>
79+
/* </DisplayAccessTokenSnippet> */
7880

79-
// <ListUsersSnippet>
81+
/* <ListUsersSnippet> */
8082
async Task ListUsersAsync()
8183
{
8284
try
@@ -111,11 +113,11 @@ async Task ListUsersAsync()
111113
Console.WriteLine($"Error getting users: {ex.Message}");
112114
}
113115
}
114-
// </ListUsersSnippet>
116+
/* </ListUsersSnippet> */
115117

116-
// <MakeGraphCallSnippet>
118+
/* <MakeGraphCallSnippet> */
117119
async Task MakeGraphCallAsync()
118120
{
119121
await GraphHelper.MakeGraphCallAsync();
120122
}
121-
// </MakeGraphCallSnippet>
123+
/* </MakeGraphCallSnippet> */
Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,33 @@
11
// Copyright (c) Microsoft Corporation.
2-
// Licensed under the MIT License.
2+
// Licensed under the MIT license.
33

4-
// <SettingsSnippet>
4+
/* <SettingsSnippet> */
55
using Microsoft.Extensions.Configuration;
66

7+
namespace GraphAppOnlyTutorial;
8+
79
public class Settings
810
{
911
public string? ClientId { get; set; }
12+
1013
public string? ClientSecret { get; set; }
14+
1115
public string? TenantId { get; set; }
1216

1317
public static Settings LoadSettings()
1418
{
1519
// Load settings
1620
IConfiguration config = new ConfigurationBuilder()
17-
// appsettings.json is required
21+
/* appsettings.json is required */
1822
.AddJsonFile("appsettings.json", optional: false)
19-
// appsettings.Development.json" is optional, values override appsettings.json
23+
/* appsettings.Development.json" is optional, values override appsettings.json */
2024
.AddJsonFile($"appsettings.Development.json", optional: true)
21-
// User secrets are optional, values override both JSON files
25+
/* User secrets are optional, values override both JSON files */
2226
.AddUserSecrets<Program>()
2327
.Build();
2428

2529
return config.GetRequiredSection("Settings").Get<Settings>() ??
2630
throw new Exception("Could not load app settings. See README for configuration instructions.");
2731
}
2832
}
29-
// </SettingsSnippet>
33+
/* </SettingsSnippet> */

stylecop.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"$schema": "https://raw.githubusercontent.com/DotNetAnalyzers/StyleCopAnalyzers/master/StyleCop.Analyzers/StyleCop.Analyzers/Settings/stylecop.schema.json",
3+
"settings": {
4+
"documentationRules": {
5+
"companyName": "Microsoft Corporation",
6+
"copyrightText": "Copyright (c) {companyName}.\nLicensed under the MIT license.",
7+
"xmlHeader": false
8+
},
9+
"orderingRules": {
10+
"usingDirectivesPlacement": "outsideNamespace"
11+
}
12+
}
13+
}

0 commit comments

Comments
 (0)