From e4b7ba61ade3a51ba27f095fd0f2bdf545c58680 Mon Sep 17 00:00:00 2001 From: christianwade Date: Wed, 13 Dec 2017 15:08:33 -0800 Subject: [PATCH 1/2] REST API Sample --- RestApiSample/RestApiSample.sln | 25 +++++ RestApiSample/RestApiSample/App.config | 14 +++ RestApiSample/RestApiSample/Program.cs | 96 +++++++++++++++++++ .../RestApiSample/Properties/AssemblyInfo.cs | 36 +++++++ .../RestApiSample/RestApiSample.csproj | 63 ++++++++++++ RestApiSample/RestApiSample/packages.config | 5 + 6 files changed, 239 insertions(+) create mode 100644 RestApiSample/RestApiSample.sln create mode 100644 RestApiSample/RestApiSample/App.config create mode 100644 RestApiSample/RestApiSample/Program.cs create mode 100644 RestApiSample/RestApiSample/Properties/AssemblyInfo.cs create mode 100644 RestApiSample/RestApiSample/RestApiSample.csproj create mode 100644 RestApiSample/RestApiSample/packages.config diff --git a/RestApiSample/RestApiSample.sln b/RestApiSample/RestApiSample.sln new file mode 100644 index 0000000..10c0d53 --- /dev/null +++ b/RestApiSample/RestApiSample.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 15 +VisualStudioVersion = 15.0.26730.10 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RestApiSample", "RestApiSample\RestApiSample.csproj", "{AE4CB5CE-F106-4077-8792-B2C1032888E6}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {AE4CB5CE-F106-4077-8792-B2C1032888E6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {AE4CB5CE-F106-4077-8792-B2C1032888E6}.Debug|Any CPU.Build.0 = Debug|Any CPU + {AE4CB5CE-F106-4077-8792-B2C1032888E6}.Release|Any CPU.ActiveCfg = Release|Any CPU + {AE4CB5CE-F106-4077-8792-B2C1032888E6}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {4B6AE141-D5F8-465F-A524-3922EA33DF53} + EndGlobalSection +EndGlobal diff --git a/RestApiSample/RestApiSample/App.config b/RestApiSample/RestApiSample/App.config new file mode 100644 index 0000000..eb51eb6 --- /dev/null +++ b/RestApiSample/RestApiSample/App.config @@ -0,0 +1,14 @@ + + + + + + + + + + + + + + \ No newline at end of file diff --git a/RestApiSample/RestApiSample/Program.cs b/RestApiSample/RestApiSample/Program.cs new file mode 100644 index 0000000..7dcc7a7 --- /dev/null +++ b/RestApiSample/RestApiSample/Program.cs @@ -0,0 +1,96 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Microsoft.IdentityModel.Clients.ActiveDirectory; +using System.Net; +using System.Net.Http; +using System.Net.Http.Headers; +using System.Threading; + +namespace RestApiSample +{ + class Program + { + static void Main(string[] args) + { + CallRefreshAsync(); + Console.ReadLine(); + } + + private static async void CallRefreshAsync() + { + HttpClient client = new HttpClient(); + client.BaseAddress = new Uri("https://.asazure.windows.net/servers//models/"); + //todo delete client.BaseAddress = new Uri("https://southcentralus.asazure.windows.net/servers/chwade003/models/AdventureWorks2"); + + // Send refresh request + client.DefaultRequestHeaders.Accept.Clear(); + client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); + client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", await UpdateToken()); + + RefreshRequest refreshRequest = new RefreshRequest() + { + type = "full", + maxParallelism = 10 + }; + + HttpResponseMessage response = await client.PostAsJsonAsync("refreshes", refreshRequest); + response.EnsureSuccessStatusCode(); + Uri location = response.Headers.Location; + Console.WriteLine(response.Headers.Location); + + // Check the response + while (true) // Will exit while loop when exit Main() method (it's running asynchronously) + { + string output = ""; + + // Refresh token if required + client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", await UpdateToken()); + + response = await client.GetAsync(location); + if (response.IsSuccessStatusCode) + { + output = await response.Content.ReadAsStringAsync(); + } + + Console.Clear(); + Console.WriteLine(output); + + Thread.Sleep(5000); + } + } + + private static async Task UpdateToken() + { + string resourceURI = "https://*.asazure.windows.net"; + string clientID = ""; // Native app with permissions + //todo delete string clientID = "c81c4e35-f9fc-4ff8-8fdd-1c8722f3921c"; // Native app with permissions + + string authority = "https://login.windows.net/common/oauth2/authorize"; + // Authority address can optionally use tenant ID in place of "common". If service principal or B2B enabled, this is a requirement. + //string authority = "https://login.windows.net//oauth2/authorize"; + AuthenticationContext ac = new AuthenticationContext(authority); + + //Interactive login if not cached: + AuthenticationResult ar = await ac.AcquireTokenAsync(resourceURI, clientID, new Uri("urn:ietf:wg:oauth:2.0:oob"), new PlatformParameters(PromptBehavior.Auto)); + + //Username/password: + //UserPasswordCredential cred = new UserPasswordCredential("", ""); + //AuthenticationResult ar = await ac.AcquireTokenAsync(resourceURI, clientID, cred); + + //Service principal: + //ClientCredential cred = new ClientCredential("", ""); + //AuthenticationResult ar = await ac.AcquireTokenAsync(resourceURI, cred); + + return ar.AccessToken; + } + } + + class RefreshRequest + { + public string type { get; set; } + public int maxParallelism { get; set; } + } +} diff --git a/RestApiSample/RestApiSample/Properties/AssemblyInfo.cs b/RestApiSample/RestApiSample/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..63c6b02 --- /dev/null +++ b/RestApiSample/RestApiSample/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +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("RestApiSample")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("RestApiSample")] +[assembly: AssemblyCopyright("Copyright © 2017")] +[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("ae4cb5ce-f106-4077-8792-b2c1032888e6")] + +// 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/RestApiSample/RestApiSample/RestApiSample.csproj b/RestApiSample/RestApiSample/RestApiSample.csproj new file mode 100644 index 0000000..14a6b0d --- /dev/null +++ b/RestApiSample/RestApiSample/RestApiSample.csproj @@ -0,0 +1,63 @@ + + + + + Debug + AnyCPU + {AE4CB5CE-F106-4077-8792-B2C1032888E6} + Exe + RestApiSample + RestApiSample + v4.6.1 + 512 + true + + + AnyCPU + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + AnyCPU + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + ..\packages\Microsoft.IdentityModel.Clients.ActiveDirectory.3.17.2\lib\net45\Microsoft.IdentityModel.Clients.ActiveDirectory.dll + + + ..\packages\Microsoft.IdentityModel.Clients.ActiveDirectory.3.17.2\lib\net45\Microsoft.IdentityModel.Clients.ActiveDirectory.Platform.dll + + + ..\packages\Newtonsoft.Json.10.0.3\lib\net45\Newtonsoft.Json.dll + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/RestApiSample/RestApiSample/packages.config b/RestApiSample/RestApiSample/packages.config new file mode 100644 index 0000000..d101344 --- /dev/null +++ b/RestApiSample/RestApiSample/packages.config @@ -0,0 +1,5 @@ + + + + + \ No newline at end of file From f9afe64e33b7667cf3dae2a4378f08dfdbfefa79 Mon Sep 17 00:00:00 2001 From: christianwade Date: Wed, 13 Dec 2017 18:54:50 -0800 Subject: [PATCH 2/2] Ready for blog post --- RestApiSample/RestApiSample/Program.cs | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/RestApiSample/RestApiSample/Program.cs b/RestApiSample/RestApiSample/Program.cs index 7dcc7a7..824bf05 100644 --- a/RestApiSample/RestApiSample/Program.cs +++ b/RestApiSample/RestApiSample/Program.cs @@ -22,8 +22,7 @@ namespace RestApiSample private static async void CallRefreshAsync() { HttpClient client = new HttpClient(); - client.BaseAddress = new Uri("https://.asazure.windows.net/servers//models/"); - //todo delete client.BaseAddress = new Uri("https://southcentralus.asazure.windows.net/servers/chwade003/models/AdventureWorks2"); + client.BaseAddress = new Uri("https://.asazure.windows.net/servers//models//"); // Send refresh request client.DefaultRequestHeaders.Accept.Clear(); @@ -65,12 +64,10 @@ namespace RestApiSample private static async Task UpdateToken() { string resourceURI = "https://*.asazure.windows.net"; - string clientID = ""; // Native app with permissions - //todo delete string clientID = "c81c4e35-f9fc-4ff8-8fdd-1c8722f3921c"; // Native app with permissions + string clientID = ""; // Native app with necessary API permissions string authority = "https://login.windows.net/common/oauth2/authorize"; - // Authority address can optionally use tenant ID in place of "common". If service principal or B2B enabled, this is a requirement. - //string authority = "https://login.windows.net//oauth2/authorize"; + //string authority = "https://login.windows.net//oauth2/authorize"; // Authority address can optionally use tenant ID in place of "common". If service principal or B2B enabled, this is a requirement. AuthenticationContext ac = new AuthenticationContext(authority); //Interactive login if not cached: @@ -81,6 +78,7 @@ namespace RestApiSample //AuthenticationResult ar = await ac.AcquireTokenAsync(resourceURI, clientID, cred); //Service principal: + //12/19/2017: Bug disallows use of service principals. At time of writing, the fix is being rolled out to production clusters. Please retry soon if not working by the time you try it. //ClientCredential cred = new ClientCredential("", ""); //AuthenticationResult ar = await ac.AcquireTokenAsync(resourceURI, cred);