Skip to content

Commit

Permalink
Implemented config file for sensitive configuration values
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonjoh committed Oct 5, 2018
1 parent e448952 commit 4067f87
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 7 deletions.
2 changes: 2 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Set behaviour for all files, in case developers don't have core.autocrlf set.
* text=auto
9 changes: 6 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ ClientBin/
*.publishsettings
orleans.codegen.cs

# Including strong name files can present a security risk
# Including strong name files can present a security risk
# (https://github.com/github/gitignore/pull/2483#issue-259490424)
#*.snk

Expand Down Expand Up @@ -317,7 +317,7 @@ __pycache__/
# OpenCover UI analysis results
OpenCover/

# Azure Stream Analytics local run output
# Azure Stream Analytics local run output
ASALocalRun/

# MSBuild Binary and Structured Log
Expand All @@ -326,5 +326,8 @@ ASALocalRun/
# NVidia Nsight GPU debugger configuration file
*.nvuser

# MFractors (Xamarin productivity tool) working folder
# MFractors (Xamarin productivity tool) working folder
.mfractor/

# App settings
appsettings.json
3 changes: 3 additions & 0 deletions ConsoleGraphTest.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Configuration" Version="2.1.1" />
<PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="2.1.1" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="2.1.1" />
<PackageReference Include="Microsoft.Graph" Version="1.11.0" />
<PackageReference Include="Microsoft.Identity.Client" Version="2.1.0-preview" />
</ItemGroup>
Expand Down
42 changes: 39 additions & 3 deletions Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,28 @@
using Microsoft.Graph;
using System.Net.Http.Headers;
using System.Threading.Tasks;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Configuration.FileExtensions;
using Microsoft.Extensions.Configuration.Json;

namespace ConsoleGraphTest
{
class Program
{
static void Main(string[] args)
{
var clientId = "80609421-5b89-47eb-a42c-5aacd3ef8943";
var clientSecret = "szjwBD9167^=@bwmXZXXV2-";
// 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;
}

var clientId = config["applicationId"];
var clientSecret = config["applicationSecret"];
var redirectUri = "https://localhost:8042";
var authority = "https://login.microsoftonline.com/d05889e3-29af-4ce4-8312-9029d4c26b1d/v2.0";
var authority = $"https://login.microsoftonline.com/{config["tenantId"]}/v2.0";
List<string> scopes = new List<string>();
scopes.Add("https://graph.microsoft.com/.default");

Expand Down Expand Up @@ -51,5 +62,30 @@ static void Main(string[] args)

Console.WriteLine(httpResult);
}

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["tenantId"]))
{
return null;
}

return config;
}
catch (System.IO.FileNotFoundException)
{
return null;
}
}
}
}
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
# console-dotnetcore-sample

# Contributing
## Configuration

1. Rename the `appsettings.json.example` file to `appsettings.json`.
1. Edit `appsettings.json`:
1. Replace `YOUR_APP_ID_HERE` with your application ID from the App Registration Portal.
1. Replace `YOUR_APP_SECRET_HERE` with your application password from the App Registration Portal.
1. Replace `YOUR_TENANT_ID_HERE` with your tenant ID.

## Contributing

This project welcomes contributions and suggestions. Most contributions require you to agree to a
Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us
Expand Down
5 changes: 5 additions & 0 deletions appsettings.json.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"applicationId": "YOUR_APP_ID_HERE",
"applicationSecret": "YOUR_APP_SECRET_HERE",
"tenantId": "YOUR_TENANT_ID_HERE"
}

0 comments on commit 4067f87

Please sign in to comment.