From a78d98d2df3ce61b90543a093bd10342430b3c9b Mon Sep 17 00:00:00 2001 From: Joonas Westlin Date: Thu, 31 May 2018 21:37:53 +0300 Subject: [PATCH] Work-in-progress, adding background job, refactoring apps to Core and using the ConfigurationBuilder APIs --- .../Joonasw.AzureAdApiSample.Api.csproj | 1 + Joonasw.AzureAdApiSample.Api/appsettings.json | 6 +- .../JobSettings.cs | 11 +++ ...ureAdApiSample.ConsoleBackgroundJob.csproj | 25 +++++++ .../Program.cs | 22 ++++++ .../appsettings.json | 7 ++ .../App.config | 13 ---- .../ClientSettings.cs | 11 +++ ...zureAdApiSample.ConsoleNativeClient.csproj | 75 +++++-------------- .../Program.cs | 11 ++- .../Properties/AssemblyInfo.cs | 36 --------- .../TodoApiClient.cs | 37 +++++---- .../appsettings.json | 7 ++ .../packages.config | 5 -- Joonasw.AzureAdApiSample.sln | 6 ++ 15 files changed, 142 insertions(+), 131 deletions(-) create mode 100644 Joonasw.AzureAdApiSample.ConsoleBackgroundJob/JobSettings.cs create mode 100644 Joonasw.AzureAdApiSample.ConsoleBackgroundJob/Joonasw.AzureAdApiSample.ConsoleBackgroundJob.csproj create mode 100644 Joonasw.AzureAdApiSample.ConsoleBackgroundJob/Program.cs create mode 100644 Joonasw.AzureAdApiSample.ConsoleBackgroundJob/appsettings.json delete mode 100644 Joonasw.AzureAdApiSample.ConsoleNativeClient/App.config create mode 100644 Joonasw.AzureAdApiSample.ConsoleNativeClient/ClientSettings.cs delete mode 100644 Joonasw.AzureAdApiSample.ConsoleNativeClient/Properties/AssemblyInfo.cs create mode 100644 Joonasw.AzureAdApiSample.ConsoleNativeClient/appsettings.json delete mode 100644 Joonasw.AzureAdApiSample.ConsoleNativeClient/packages.config diff --git a/Joonasw.AzureAdApiSample.Api/Joonasw.AzureAdApiSample.Api.csproj b/Joonasw.AzureAdApiSample.Api/Joonasw.AzureAdApiSample.Api.csproj index 08ec6bc..75486df 100644 --- a/Joonasw.AzureAdApiSample.Api/Joonasw.AzureAdApiSample.Api.csproj +++ b/Joonasw.AzureAdApiSample.Api/Joonasw.AzureAdApiSample.Api.csproj @@ -2,6 +2,7 @@ netcoreapp2.1 + bee0443a-9bf6-45ea-8e8b-f49272936717 diff --git a/Joonasw.AzureAdApiSample.Api/appsettings.json b/Joonasw.AzureAdApiSample.Api/appsettings.json index 9dfbcb2..83ed610 100644 --- a/Joonasw.AzureAdApiSample.Api/appsettings.json +++ b/Joonasw.AzureAdApiSample.Api/appsettings.json @@ -13,8 +13,8 @@ } }, "Authentication": { - "Authority": "https://login.microsoftonline.com/joonasapps.onmicrosoft.com/", - "AppIdUri": "https://joonasapps.onmicrosoft.com/fa87d15c-fbc9-4fe5-95f5-0b36108eb305", - "ClientId": "6657d9d4-a9c1-4fb1-81b0-8a8d42b6a9cf" + "Authority": "https://login.microsoftonline.com/yourtenant.onmicrosoft.com/", + "AppIdUri": "https://yourtenant.onmicrosoft.com/guid-here", + "ClientId": "client-id-also-known-as-application-id" } } diff --git a/Joonasw.AzureAdApiSample.ConsoleBackgroundJob/JobSettings.cs b/Joonasw.AzureAdApiSample.ConsoleBackgroundJob/JobSettings.cs new file mode 100644 index 0000000..2a4befb --- /dev/null +++ b/Joonasw.AzureAdApiSample.ConsoleBackgroundJob/JobSettings.cs @@ -0,0 +1,11 @@ +namespace Joonasw.AzureAdApiSample.ConsoleBackgroundJob +{ + public class JobSettings + { + public string Authority { get; set; } + public string ClientId { get; set; } + public string ClientSecret { get; set; } + public string ApiResourceUri { get; set; } + public string ApiBaseUrl { get; set; } + } +} diff --git a/Joonasw.AzureAdApiSample.ConsoleBackgroundJob/Joonasw.AzureAdApiSample.ConsoleBackgroundJob.csproj b/Joonasw.AzureAdApiSample.ConsoleBackgroundJob/Joonasw.AzureAdApiSample.ConsoleBackgroundJob.csproj new file mode 100644 index 0000000..a6195a6 --- /dev/null +++ b/Joonasw.AzureAdApiSample.ConsoleBackgroundJob/Joonasw.AzureAdApiSample.ConsoleBackgroundJob.csproj @@ -0,0 +1,25 @@ + + + + Exe + netcoreapp2.1 + 7.2 + bee0443a-9bf6-45ea-8e8b-f49272936718 + + + + + + + + + + + + + + PreserveNewest + + + + diff --git a/Joonasw.AzureAdApiSample.ConsoleBackgroundJob/Program.cs b/Joonasw.AzureAdApiSample.ConsoleBackgroundJob/Program.cs new file mode 100644 index 0000000..302d5aa --- /dev/null +++ b/Joonasw.AzureAdApiSample.ConsoleBackgroundJob/Program.cs @@ -0,0 +1,22 @@ +using System; +using System.Linq; +using System.Threading.Tasks; +using Microsoft.Extensions.Configuration; + +namespace Joonasw.AzureAdApiSample.ConsoleBackgroundJob +{ + class Program + { + static async Task Main(string[] args) + { + IConfiguration config = CreateConfig(); + JobSettings settings = config.Get(); + } + + private static IConfiguration CreateConfig() => + new ConfigurationBuilder() + .AddJsonFile("appsettings.json") + .AddUserSecrets() + .Build(); + } +} diff --git a/Joonasw.AzureAdApiSample.ConsoleBackgroundJob/appsettings.json b/Joonasw.AzureAdApiSample.ConsoleBackgroundJob/appsettings.json new file mode 100644 index 0000000..5bfd237 --- /dev/null +++ b/Joonasw.AzureAdApiSample.ConsoleBackgroundJob/appsettings.json @@ -0,0 +1,7 @@ +{ + "Authority": "", + "ClientId": "", + "ClientSecret": "", + "ApiResourceUri": "", + "ApiBaseUrl": "http://localhost:2672" +} \ No newline at end of file diff --git a/Joonasw.AzureAdApiSample.ConsoleNativeClient/App.config b/Joonasw.AzureAdApiSample.ConsoleNativeClient/App.config deleted file mode 100644 index 66a3aef..0000000 --- a/Joonasw.AzureAdApiSample.ConsoleNativeClient/App.config +++ /dev/null @@ -1,13 +0,0 @@ - - - - - - - - - - - - - \ No newline at end of file diff --git a/Joonasw.AzureAdApiSample.ConsoleNativeClient/ClientSettings.cs b/Joonasw.AzureAdApiSample.ConsoleNativeClient/ClientSettings.cs new file mode 100644 index 0000000..e78c9dd --- /dev/null +++ b/Joonasw.AzureAdApiSample.ConsoleNativeClient/ClientSettings.cs @@ -0,0 +1,11 @@ +namespace Joonasw.AzureAdApiSample.ConsoleNativeClient +{ + public class ClientSettings + { + public string Authority { get; set; } + public string ClientId { get; set; } + public string RedirectUri { get; set; } + public string ApiResourceUri { get; set; } + public string ApiBaseUrl { get; set; } + } +} diff --git a/Joonasw.AzureAdApiSample.ConsoleNativeClient/Joonasw.AzureAdApiSample.ConsoleNativeClient.csproj b/Joonasw.AzureAdApiSample.ConsoleNativeClient/Joonasw.AzureAdApiSample.ConsoleNativeClient.csproj index f67ecf6..8c5742b 100644 --- a/Joonasw.AzureAdApiSample.ConsoleNativeClient/Joonasw.AzureAdApiSample.ConsoleNativeClient.csproj +++ b/Joonasw.AzureAdApiSample.ConsoleNativeClient/Joonasw.AzureAdApiSample.ConsoleNativeClient.csproj @@ -1,66 +1,25 @@ - - - + + - Debug - AnyCPU - {D3973CD4-097F-4332-8D10-39B3A1F83F47} Exe - Joonasw.AzureAdApiSample.ConsoleNativeClient - Joonasw.AzureAdApiSample.ConsoleNativeClient - v4.6.1 - 512 - true - - - AnyCPU - true - full - false - bin\Debug\ - DEBUG;TRACE - prompt - 4 + netcoreapp2.1 7.2 + bee0443a-9bf6-45ea-8e8b-f49272936719 - - AnyCPU - pdbonly - true - bin\Release\ - TRACE - prompt - 4 - - - - ..\packages\Microsoft.IdentityModel.Clients.ActiveDirectory.3.19.6\lib\net45\Microsoft.IdentityModel.Clients.ActiveDirectory.dll - - - ..\packages\Microsoft.IdentityModel.Clients.ActiveDirectory.3.19.6\lib\net45\Microsoft.IdentityModel.Clients.ActiveDirectory.Platform.dll - - - ..\packages\Newtonsoft.Json.11.0.2\lib\net45\Newtonsoft.Json.dll - - - - - - - - - - - + - - - - + + + + + + + - - + + PreserveNewest + - - \ No newline at end of file + + diff --git a/Joonasw.AzureAdApiSample.ConsoleNativeClient/Program.cs b/Joonasw.AzureAdApiSample.ConsoleNativeClient/Program.cs index 4cb0120..7908fd3 100644 --- a/Joonasw.AzureAdApiSample.ConsoleNativeClient/Program.cs +++ b/Joonasw.AzureAdApiSample.ConsoleNativeClient/Program.cs @@ -1,5 +1,6 @@ using System; using System.Threading.Tasks; +using Microsoft.Extensions.Configuration; namespace Joonasw.AzureAdApiSample.ConsoleNativeClient { @@ -7,7 +8,9 @@ class Program { static async Task Main(string[] args) { - var todoApiClient = new TodoApiClient(); + var config = CreateConfig(); + var settings = config.Get(); + var todoApiClient = new TodoApiClient(settings); await todoApiClient.ListTodosAsync(); Guid id = await todoApiClient.CreateTodoAsync(new TodoItem { @@ -19,5 +22,11 @@ static async Task Main(string[] args) await todoApiClient.ListTodosAsync(); Console.ReadLine(); } + + private static IConfiguration CreateConfig() => + new ConfigurationBuilder() + .AddJsonFile("appsettings.json") + .AddUserSecrets() + .Build(); } } diff --git a/Joonasw.AzureAdApiSample.ConsoleNativeClient/Properties/AssemblyInfo.cs b/Joonasw.AzureAdApiSample.ConsoleNativeClient/Properties/AssemblyInfo.cs deleted file mode 100644 index 20e60c0..0000000 --- a/Joonasw.AzureAdApiSample.ConsoleNativeClient/Properties/AssemblyInfo.cs +++ /dev/null @@ -1,36 +0,0 @@ -using System.Reflection; -using System.Runtime.CompilerServices; -using System.Runtime.InteropServices; - -// General Information about an assembly is controlled through the following -// set of attributes. Change these attribute values to modify the information -// associated with an assembly. -[assembly: AssemblyTitle("Joonasw.AzureAdApiSample.ConsoleNativeClient")] -[assembly: AssemblyDescription("")] -[assembly: AssemblyConfiguration("")] -[assembly: AssemblyCompany("")] -[assembly: AssemblyProduct("Joonasw.AzureAdApiSample.ConsoleNativeClient")] -[assembly: AssemblyCopyright("Copyright © 2018")] -[assembly: AssemblyTrademark("")] -[assembly: AssemblyCulture("")] - -// Setting ComVisible to false makes the types in this assembly not visible -// to COM components. If you need to access a type in this assembly from -// COM, set the ComVisible attribute to true on that type. -[assembly: ComVisible(false)] - -// The following GUID is for the ID of the typelib if this project is exposed to COM -[assembly: Guid("d3973cd4-097f-4332-8d10-39b3a1f83f47")] - -// Version information for an assembly consists of the following four values: -// -// Major Version -// Minor Version -// Build Number -// Revision -// -// You can specify all the values or you can default the Build and Revision Numbers -// by using the '*' as shown below: -// [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("1.0.0.0")] -[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/Joonasw.AzureAdApiSample.ConsoleNativeClient/TodoApiClient.cs b/Joonasw.AzureAdApiSample.ConsoleNativeClient/TodoApiClient.cs index f1d7ba4..a90ca67 100644 --- a/Joonasw.AzureAdApiSample.ConsoleNativeClient/TodoApiClient.cs +++ b/Joonasw.AzureAdApiSample.ConsoleNativeClient/TodoApiClient.cs @@ -1,6 +1,5 @@ using System; using System.Collections.Generic; -using System.Configuration; using System.Net.Http; using System.Net.Http.Headers; using System.Text; @@ -12,16 +11,17 @@ namespace Joonasw.AzureAdApiSample.ConsoleNativeClient { public class TodoApiClient { - private static readonly string Authority = ConfigurationManager.AppSettings["AzureAd:Authority"]; - private static readonly string ApiResourceUri = ConfigurationManager.AppSettings["AzureAd:ApiResourceUri"]; - private static readonly string ClientId = ConfigurationManager.AppSettings["AzureAd:ClientId"]; - private static readonly Uri RedirectUri = new Uri(ConfigurationManager.AppSettings["AzureAd:RedirectUri"]); - private static readonly string ApiBaseUrl = ConfigurationManager.AppSettings["AzureAd:ApiBaseUrl"]; private static readonly HttpClient Client = new HttpClient(); + private readonly ClientSettings _settings; + + public TodoApiClient(ClientSettings settings) + { + _settings = settings; + } public async Task ListTodosAsync() { - using (var req = new HttpRequestMessage(HttpMethod.Get, $"{ApiBaseUrl}/api/todos")) + using (var req = new HttpRequestMessage(HttpMethod.Get, $"{_settings.ApiBaseUrl}/api/todos")) { string accessToken = await GetAccessTokenAsync(); req.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessToken); @@ -48,7 +48,7 @@ private void ListTodosOnConsole(List todos) public async Task CreateTodoAsync(TodoItem todoItem) { Console.WriteLine("---Create todo item---"); - using (var req = new HttpRequestMessage(HttpMethod.Post, $"{ApiBaseUrl}/api/todos")) + using (var req = new HttpRequestMessage(HttpMethod.Post, $"{_settings.ApiBaseUrl}/api/todos")) { string accessToken = await GetAccessTokenAsync(); req.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessToken); @@ -71,7 +71,7 @@ public async Task CreateTodoAsync(TodoItem todoItem) public async Task DeleteTodoAsync(Guid id) { Console.WriteLine("---Delete todo item---"); - using (var req = new HttpRequestMessage(HttpMethod.Delete, $"{ApiBaseUrl}/api/todos/{id}")) + using (var req = new HttpRequestMessage(HttpMethod.Delete, $"{_settings.ApiBaseUrl}/api/todos/{id}")) { string accessToken = await GetAccessTokenAsync(); req.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessToken); @@ -86,13 +86,20 @@ public async Task DeleteTodoAsync(Guid id) private async Task GetAccessTokenAsync() { - var context = new AuthenticationContext(Authority); + var context = new AuthenticationContext(_settings.Authority); + + AuthenticationResult result; + try + { + result = await context.AcquireTokenSilentAsync(_settings.ApiResourceUri, _settings.ClientId); + } + catch (AdalSilentTokenAcquisitionException) + { + DeviceCodeResult deviceCodeResult = await context.AcquireDeviceCodeAsync(_settings.ApiResourceUri, _settings.ClientId); + Console.WriteLine(deviceCodeResult.Message); + result = await context.AcquireTokenByDeviceCodeAsync(deviceCodeResult); + } - var result = await context.AcquireTokenAsync( - ApiResourceUri, - ClientId, - RedirectUri, - new PlatformParameters(PromptBehavior.Auto)); return result.AccessToken; } } diff --git a/Joonasw.AzureAdApiSample.ConsoleNativeClient/appsettings.json b/Joonasw.AzureAdApiSample.ConsoleNativeClient/appsettings.json new file mode 100644 index 0000000..7cc2f5a --- /dev/null +++ b/Joonasw.AzureAdApiSample.ConsoleNativeClient/appsettings.json @@ -0,0 +1,7 @@ +{ + "Authority": "https://login.microsoftonline.com/yourtenant.onmicrosoft.com", + "ClientId": "application-id-here", + "ApiResourceUri": "https://yourtenant.onmicrosoft.com/some-guid", + "RedirectUri": "https://yourtenant.onmicrosoft.com/TodoClient", + "ApiBaseUrl": "http://localhost:2672" +} \ No newline at end of file diff --git a/Joonasw.AzureAdApiSample.ConsoleNativeClient/packages.config b/Joonasw.AzureAdApiSample.ConsoleNativeClient/packages.config deleted file mode 100644 index 207f389..0000000 --- a/Joonasw.AzureAdApiSample.ConsoleNativeClient/packages.config +++ /dev/null @@ -1,5 +0,0 @@ - - - - - \ No newline at end of file diff --git a/Joonasw.AzureAdApiSample.sln b/Joonasw.AzureAdApiSample.sln index 12bc4c7..ed6f83a 100644 --- a/Joonasw.AzureAdApiSample.sln +++ b/Joonasw.AzureAdApiSample.sln @@ -7,6 +7,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Joonasw.AzureAdApiSample.Ap EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Joonasw.AzureAdApiSample.ConsoleNativeClient", "Joonasw.AzureAdApiSample.ConsoleNativeClient\Joonasw.AzureAdApiSample.ConsoleNativeClient.csproj", "{D3973CD4-097F-4332-8D10-39B3A1F83F47}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Joonasw.AzureAdApiSample.ConsoleBackgroundJob", "Joonasw.AzureAdApiSample.ConsoleBackgroundJob\Joonasw.AzureAdApiSample.ConsoleBackgroundJob.csproj", "{309E6F8E-F357-4AF6-ACCB-2EF9BCBE0A9F}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -21,6 +23,10 @@ Global {D3973CD4-097F-4332-8D10-39B3A1F83F47}.Debug|Any CPU.Build.0 = Debug|Any CPU {D3973CD4-097F-4332-8D10-39B3A1F83F47}.Release|Any CPU.ActiveCfg = Release|Any CPU {D3973CD4-097F-4332-8D10-39B3A1F83F47}.Release|Any CPU.Build.0 = Release|Any CPU + {309E6F8E-F357-4AF6-ACCB-2EF9BCBE0A9F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {309E6F8E-F357-4AF6-ACCB-2EF9BCBE0A9F}.Debug|Any CPU.Build.0 = Debug|Any CPU + {309E6F8E-F357-4AF6-ACCB-2EF9BCBE0A9F}.Release|Any CPU.ActiveCfg = Release|Any CPU + {309E6F8E-F357-4AF6-ACCB-2EF9BCBE0A9F}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE