-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSettings.cs
More file actions
106 lines (89 loc) · 3.58 KB
/
Settings.cs
File metadata and controls
106 lines (89 loc) · 3.58 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
// <copyright file="Settings.cs" company="Moonrise Software, LLC">
// Copyright (c) Moonrise Software, LLC. All rights reserved.
// Licensed under the GNU Public License, Version 3.0 (https://www.gnu.org/licenses/gpl-3.0.html)
// See https://github.com/MoonriseSoftwareCalifornia/CosmosCMS
// for more information concerning the license and the contributors participating to this project.
// </copyright>
namespace Cosmos.MicrosoftGraph
{
using System.Reflection;
using Microsoft.Extensions.Configuration;
/// <summary>
/// Configuration settings.
/// </summary>
public class Settings
{
/// <summary>
/// Gets or sets the Entra ID client ID.
/// </summary>
public string? ClientId { get; set; }
/// <summary>
/// Gets or sets the Azure Tenant ID.
/// </summary>
public string? TenantId { get; set; }
/// <summary>
/// Gets or sets the application client secret.
/// </summary>
public string? ClientSecret { get; set; }
/// <summary>
/// Gets the Graph user scopes.
/// </summary>
public string[]? GraphUserScopes { get; private set; } = { "https://graph.microsoft.com/.default" };
/// <summary>
/// Loads the settings from default sources..
/// </summary>
/// <returns>Settings.</returns>
public static Settings LoadSettings()
{
IConfiguration config = new ConfigurationBuilder()
.AddUserSecrets(Assembly.GetExecutingAssembly())
.AddJsonFile("local.settings.json", optional: true) // appsettings.json is required
.AddJsonFile($"appsettings.Development.json", optional: true) // appsettings.Development.json" is optional, values override appsettings.json
.AddEnvironmentVariables()
.Build();
return LoadSettings(config);
}
/// <summary>
/// Loads the settings from the given configuration..
/// </summary>
/// <param name="config">Application configuration.</param>
/// <returns>Settings.</returns>
public static Settings LoadSettings(IConfiguration config)
{
var settings = new Settings();
settings.ClientId = GetValue(config, nameof(settings.ClientId));
settings.TenantId = GetValue(config, nameof(settings.TenantId));
settings.ClientSecret = GetValue(config, nameof(settings.ClientSecret));
return settings ??
throw new Exception("Could not load app settings. See README for configuration instructions.");
}
private static string GetValue(IConfiguration config, string key)
{
if (config != null)
{
var value = config[key];
if (string.IsNullOrEmpty(value))
{
value = config.GetValue<string>(key);
}
if (string.IsNullOrEmpty(value))
{
value = config.GetValue<string>(key.ToUpper());
}
if (string.IsNullOrEmpty(value))
{
value = config.GetValue<string>(key.ToLower());
}
if (string.IsNullOrEmpty(value))
{
throw new Exception($"Could not find a value for {key} in the configuration file.");
}
return value;
}
else
{
throw new ArgumentNullException(nameof(config));
}
}
}
}