Skip to content
This repository was archived by the owner on Aug 29, 2025. It is now read-only.

Commit 9d01eb7

Browse files
committed
For new and portable installations, make user config optional
1 parent dc6c019 commit 9d01eb7

File tree

3 files changed

+12
-10
lines changed

3 files changed

+12
-10
lines changed

src/Program.cs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,11 @@ static async Task<int> Main(string[] args)
3636
ConfigureAppConfiguration(configBuilder);
3737
var config = configBuilder.Build();
3838

39-
var authSettings = config.GetRequiredSection(Constants.AuthenticationSection).Get<AuthenticationOptions>();
40-
var authServiceFactory = new AuthenticationServiceFactory();
39+
var authSettings = config.GetSection(Constants.AuthenticationSection).Get<AuthenticationOptions>();
40+
var authServiceFactory = new AuthenticationServiceFactory(new PathUtility());
4141
var authStrategy = AuthenticationStrategy.DeviceCode;
4242

43-
var credential = await authServiceFactory.GetTokenCredentialAsync(authStrategy, authSettings.TenantId, authSettings.ClientId);
43+
var credential = await authServiceFactory.GetTokenCredentialAsync(authStrategy, authSettings?.TenantId, authSettings?.ClientId);
4444
var authProvider = new AzureIdentityAuthenticationProvider(credential);
4545
var defaultHandlers = KiotaClientFactory.CreateDefaultHandlers();
4646

@@ -90,9 +90,7 @@ static IHostBuilder CreateHostBuilder(string[] args) =>
9090
Host.CreateDefaultBuilder().ConfigureHostConfiguration((configHost) => {
9191
configHost.SetBasePath(Directory.GetCurrentDirectory());
9292
}).ConfigureAppConfiguration((ctx, config) => {
93-
var env = ctx.HostingEnvironment;
9493
ConfigureAppConfiguration(config);
95-
config.AddJsonFile($"app-settings.{env.EnvironmentName}.json", optional: true, true);
9694
}).ConfigureServices((ctx, services) => {
9795
var authSection = ctx.Configuration.GetSection(Constants.AuthenticationSection);
9896
services.Configure<AuthenticationOptions>(authSection);
@@ -101,10 +99,10 @@ static IHostBuilder CreateHostBuilder(string[] args) =>
10199

102100
static void ConfigureAppConfiguration(IConfigurationBuilder builder) {
103101
builder.Sources.Clear();
104-
builder.AddJsonFile("app-settings.json", optional: true);
105-
var home = new PathUtility().GetUserHomeDirectory();
106-
var userConfigPath = Path.Combine(home, Constants.ApplicationDataDirectory, "settings.json");
107-
builder.AddJsonFile(userConfigPath);
102+
builder.AddJsonFile("app-settings.json", optional: false);
103+
var dataDir = new PathUtility().GetApplicationDataDirectory();
104+
var userConfigPath = Path.Combine(dataDir, "settings.json");
105+
builder.AddJsonFile(userConfigPath, optional: true);
108106
builder.AddEnvironmentVariables(prefix: "MGC_");
109107
}
110108
}

src/app-settings.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
{
2+
"Authentication": {
3+
"ClientId": "f645f5f8-2332-496d-9d85-e714b1192f0c",
4+
"TenantId": "39aafea4-2975-4790-9c07-e616c1c35d99"
5+
},
26
"Logging": {
37
"LogLevel": {
48
"Default": "Warning"

0 commit comments

Comments
 (0)