From 3828f2d7c4681da4c201514e16552ea1d1c4e08d Mon Sep 17 00:00:00 2001 From: Javier Calvarro Nelson Date: Wed, 16 Aug 2017 15:54:55 -0700 Subject: [PATCH] Add native WPF sample for identity and OIDC --- Identity.sln | 25 +++- samples/NativeWPFClient/App.config | 6 + samples/NativeWPFClient/App.xaml | 9 ++ samples/NativeWPFClient/App.xaml.cs | 17 +++ samples/NativeWPFClient/MainWindow.xaml | 25 ++++ samples/NativeWPFClient/MainWindow.xaml.cs | 139 ++++++++++++++++++ .../NativeWPFClient/NativeWPFClient.csproj | 104 +++++++++++++ .../Properties/AssemblyInfo.cs | 55 +++++++ .../Properties/Resources.Designer.cs | 71 +++++++++ .../NativeWPFClient/Properties/Resources.resx | 117 +++++++++++++++ .../Properties/Settings.Designer.cs | 30 ++++ .../Properties/Settings.settings | 7 + 12 files changed, 602 insertions(+), 3 deletions(-) create mode 100644 samples/NativeWPFClient/App.config create mode 100644 samples/NativeWPFClient/App.xaml create mode 100644 samples/NativeWPFClient/App.xaml.cs create mode 100644 samples/NativeWPFClient/MainWindow.xaml create mode 100644 samples/NativeWPFClient/MainWindow.xaml.cs create mode 100644 samples/NativeWPFClient/NativeWPFClient.csproj create mode 100644 samples/NativeWPFClient/Properties/AssemblyInfo.cs create mode 100644 samples/NativeWPFClient/Properties/Resources.Designer.cs create mode 100644 samples/NativeWPFClient/Properties/Resources.resx create mode 100644 samples/NativeWPFClient/Properties/Settings.Designer.cs create mode 100644 samples/NativeWPFClient/Properties/Settings.settings diff --git a/Identity.sln b/Identity.sln index 1bddfea52e..324332a8b1 100644 --- a/Identity.sln +++ b/Identity.sln @@ -1,6 +1,6 @@ Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio 15 -VisualStudioVersion = 15.0.26811.0 +VisualStudioVersion = 15.0.26815.2 MinimumVisualStudioVersion = 10.0.40219.1 Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{0F647068-6602-4E24-B1DC-8ED91481A50A}" EndProject @@ -64,9 +64,11 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "IdentityOIDCWebApplicationS EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "WebSites", "WebSites", "{1F83D453-E094-4D28-BCFA-9E537ABB5AD6}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Identity.OpenIdConnect.WebSite", "test\WebSites\Identity.OpenIdConnect.WebSite\Identity.OpenIdConnect.WebSite.csproj", "{D6FF6433-336F-42E0-8F3A-60F3787A0261}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Identity.OpenIdConnect.WebSite", "test\WebSites\Identity.OpenIdConnect.WebSite\Identity.OpenIdConnect.WebSite.csproj", "{D6FF6433-336F-42E0-8F3A-60F3787A0261}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Microsoft.AspnetCore.Identity.Service.FunctionalTests", "test\Microsoft.AspnetCore.Identity.Service.FunctionalTests\Microsoft.AspnetCore.Identity.Service.FunctionalTests.csproj", "{6E3E1BE7-CEAF-4565-AA0A-F5BE3FA7F39C}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.AspnetCore.Identity.Service.FunctionalTests", "test\Microsoft.AspnetCore.Identity.Service.FunctionalTests\Microsoft.AspnetCore.Identity.Service.FunctionalTests.csproj", "{6E3E1BE7-CEAF-4565-AA0A-F5BE3FA7F39C}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NativeWPFClient", "samples\NativeWPFClient\NativeWPFClient.csproj", "{39AA4E4D-5E62-4213-8641-BF8012D45DE4}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -530,6 +532,22 @@ Global {6E3E1BE7-CEAF-4565-AA0A-F5BE3FA7F39C}.Release|x64.Build.0 = Release|Any CPU {6E3E1BE7-CEAF-4565-AA0A-F5BE3FA7F39C}.Release|x86.ActiveCfg = Release|Any CPU {6E3E1BE7-CEAF-4565-AA0A-F5BE3FA7F39C}.Release|x86.Build.0 = Release|Any CPU + {39AA4E4D-5E62-4213-8641-BF8012D45DE4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {39AA4E4D-5E62-4213-8641-BF8012D45DE4}.Debug|Any CPU.Build.0 = Debug|Any CPU + {39AA4E4D-5E62-4213-8641-BF8012D45DE4}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU + {39AA4E4D-5E62-4213-8641-BF8012D45DE4}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU + {39AA4E4D-5E62-4213-8641-BF8012D45DE4}.Debug|x64.ActiveCfg = Debug|Any CPU + {39AA4E4D-5E62-4213-8641-BF8012D45DE4}.Debug|x64.Build.0 = Debug|Any CPU + {39AA4E4D-5E62-4213-8641-BF8012D45DE4}.Debug|x86.ActiveCfg = Debug|Any CPU + {39AA4E4D-5E62-4213-8641-BF8012D45DE4}.Debug|x86.Build.0 = Debug|Any CPU + {39AA4E4D-5E62-4213-8641-BF8012D45DE4}.Release|Any CPU.ActiveCfg = Release|Any CPU + {39AA4E4D-5E62-4213-8641-BF8012D45DE4}.Release|Any CPU.Build.0 = Release|Any CPU + {39AA4E4D-5E62-4213-8641-BF8012D45DE4}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU + {39AA4E4D-5E62-4213-8641-BF8012D45DE4}.Release|Mixed Platforms.Build.0 = Release|Any CPU + {39AA4E4D-5E62-4213-8641-BF8012D45DE4}.Release|x64.ActiveCfg = Release|Any CPU + {39AA4E4D-5E62-4213-8641-BF8012D45DE4}.Release|x64.Build.0 = Release|Any CPU + {39AA4E4D-5E62-4213-8641-BF8012D45DE4}.Release|x86.ActiveCfg = Release|Any CPU + {39AA4E4D-5E62-4213-8641-BF8012D45DE4}.Release|x86.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -565,6 +583,7 @@ Global {1F83D453-E094-4D28-BCFA-9E537ABB5AD6} = {52D59F18-62D2-4D17-8CF2-BE192445AF8E} {D6FF6433-336F-42E0-8F3A-60F3787A0261} = {1F83D453-E094-4D28-BCFA-9E537ABB5AD6} {6E3E1BE7-CEAF-4565-AA0A-F5BE3FA7F39C} = {52D59F18-62D2-4D17-8CF2-BE192445AF8E} + {39AA4E4D-5E62-4213-8641-BF8012D45DE4} = {58D94A0E-C2B7-43A7-8826-99ECBB1E0A50} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {B3F2A592-CCE0-40C2-8CA4-7B1293DED874} diff --git a/samples/NativeWPFClient/App.config b/samples/NativeWPFClient/App.config new file mode 100644 index 0000000000..2ae8254d30 --- /dev/null +++ b/samples/NativeWPFClient/App.config @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/samples/NativeWPFClient/App.xaml b/samples/NativeWPFClient/App.xaml new file mode 100644 index 0000000000..08a4b73ba3 --- /dev/null +++ b/samples/NativeWPFClient/App.xaml @@ -0,0 +1,9 @@ + + + + + diff --git a/samples/NativeWPFClient/App.xaml.cs b/samples/NativeWPFClient/App.xaml.cs new file mode 100644 index 0000000000..ece2e4d8e7 --- /dev/null +++ b/samples/NativeWPFClient/App.xaml.cs @@ -0,0 +1,17 @@ +using System; +using System.Collections.Generic; +using System.Configuration; +using System.Data; +using System.Linq; +using System.Threading.Tasks; +using System.Windows; + +namespace NativeWPFClient +{ + /// + /// Interaction logic for App.xaml + /// + public partial class App : Application + { + } +} diff --git a/samples/NativeWPFClient/MainWindow.xaml b/samples/NativeWPFClient/MainWindow.xaml new file mode 100644 index 0000000000..d943d68bd4 --- /dev/null +++ b/samples/NativeWPFClient/MainWindow.xaml @@ -0,0 +1,25 @@ + + + + diff --git a/samples/NativeWPFClient/MainWindow.xaml.cs b/samples/NativeWPFClient/MainWindow.xaml.cs new file mode 100644 index 0000000000..946a01a68b --- /dev/null +++ b/samples/NativeWPFClient/MainWindow.xaml.cs @@ -0,0 +1,139 @@ +using Microsoft.Identity.Client; +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Linq; +using System.Net.Http; +using System.Text; +using System.Threading.Tasks; +using System.Windows; +using System.Windows.Controls; +using System.Windows.Data; +using System.Windows.Documents; +using System.Windows.Input; +using System.Windows.Media; +using System.Windows.Media.Imaging; +using System.Windows.Navigation; +using System.Windows.Shapes; + +namespace NativeWPFClient +{ + /// + /// Interaction logic for MainWindow.xaml + /// + public partial class MainWindow : Window + { + private Task _currentAuthorization; + + public MainWindow() + { + InitializeComponent(); + // Local client + //DataContext = new NativeWPFClientViewModel + //{ + // BaseAddress = "https://localhost/", + // RedirectUri = "urn:ietf:wg:oauth:2.0:oob", + // Tenant = "IdentityService", + // Policy = "signinsignup", + // ClientId = "777f6733-a5ef-49d5-bc0a-877e89ed768b", + // Scopes = "read" + //}; + + DataContext = new NativeWPFClientViewModel + { + BaseAddress = "https://login.microsoftonline.com/", + RedirectUri = "urn:ietf:wg:oauth:2.0:oob", + Tenant = "jacalvarb2c.onmicrosoft.com", + Policy = "B2C_1_signinsignup", + ClientId = "42291769-0dc8-4497-9cbc-d3879783d6e7", + Scopes = "https://jacalvarb2c.onmicrosoft.com/ProtectedApi/read" + }; + + ViewModel.Result = "Hit authorize to sign in"; + } + + NativeWPFClientViewModel ViewModel => (NativeWPFClientViewModel)DataContext; + + protected override void OnInitialized(EventArgs e) + { + base.OnInitialized(e); + } + + private async void Authorize_Click(object sender, RoutedEventArgs e) + { + if (_currentAuthorization == null) + { + Authorize.IsEnabled = false; + await AuthorizeAsync(); + } + } + + private async Task AuthorizeAsync() + { + var authority = $"{ViewModel.BaseAddress}tfp/{ViewModel.Tenant}/{ViewModel.Policy}"; + var client = new PublicClientApplication(ViewModel.ClientId, authority) + { + ValidateAuthority = false + }; + try + { + var scope = new string[] { }; + var appScopes = ViewModel.Scopes.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); + + var currentAuthorization = await client.AcquireTokenAsync( + appScopes, + user: null, + behavior: UIBehavior.ForceLogin, + extraQueryParameters: string.Empty, + extraScopesToConsent: null, + authority: authority); + + ViewModel.Result = currentAuthorization.User?.Name ?? "Authenticated but no name"; + } + catch (MsalException ex) + { + if (ex.ErrorCode != "authentication_canceled") + { + // An unexpected error occurred. + string message = ex.Message; + if (ex.InnerException != null) + { + message += "Inner Exception : " + ex.InnerException.Message; + } + + MessageBox.Show(message); + } + } + finally + { + _currentAuthorization = null; + Authorize.IsEnabled = true; + } + } + } + + internal class NativeWPFClientViewModel : INotifyPropertyChanged + { + private string _result; + + public string BaseAddress { get; set; } + public string RedirectUri { get; set; } + public string Tenant { get; set; } + public string Policy { get; set; } + public string ClientId { get; set; } + public string Scopes { get; set; } + + + public string Result + { + get => _result; + set + { + _result = value; + PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Result))); + } + } + + public event PropertyChangedEventHandler PropertyChanged; + } +} diff --git a/samples/NativeWPFClient/NativeWPFClient.csproj b/samples/NativeWPFClient/NativeWPFClient.csproj new file mode 100644 index 0000000000..569e4f73e0 --- /dev/null +++ b/samples/NativeWPFClient/NativeWPFClient.csproj @@ -0,0 +1,104 @@ + + + + + Debug + AnyCPU + {39AA4E4D-5E62-4213-8641-BF8012D45DE4} + WinExe + NativeWPFClient + NativeWPFClient + v4.5.2 + 512 + {60dc8134-eba5-43b8-bcc9-bb4bc16c2548};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} + 4 + true + + + AnyCPU + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + AnyCPU + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + + + + + + + + + 4.0 + + + + + + + + MSBuild:Compile + Designer + + + MSBuild:Compile + Designer + + + App.xaml + Code + + + MainWindow.xaml + Code + + + + + Code + + + True + True + Resources.resx + + + True + Settings.settings + True + + + ResXFileCodeGenerator + Resources.Designer.cs + + + SettingsSingleFileGenerator + Settings.Designer.cs + + + + + Designer + + + + + 1.1.0-preview + + + + \ No newline at end of file diff --git a/samples/NativeWPFClient/Properties/AssemblyInfo.cs b/samples/NativeWPFClient/Properties/AssemblyInfo.cs new file mode 100644 index 0000000000..d958efd3d3 --- /dev/null +++ b/samples/NativeWPFClient/Properties/AssemblyInfo.cs @@ -0,0 +1,55 @@ +using System.Reflection; +using System.Resources; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; +using System.Windows; + +// 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("NativeWPFClient")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("NativeWPFClient")] +[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)] + +//In order to begin building localizable applications, set +//CultureYouAreCodingWith in your .csproj file +//inside a . For example, if you are using US english +//in your source files, set the to en-US. Then uncomment +//the NeutralResourceLanguage attribute below. Update the "en-US" in +//the line below to match the UICulture setting in the project file. + +//[assembly: NeutralResourcesLanguage("en-US", UltimateResourceFallbackLocation.Satellite)] + + +[assembly: ThemeInfo( + ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located + //(used if a resource is not found in the page, + // or application resource dictionaries) + ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located + //(used if a resource is not found in the page, + // app, or any theme specific resource dictionaries) +)] + + +// 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/samples/NativeWPFClient/Properties/Resources.Designer.cs b/samples/NativeWPFClient/Properties/Resources.Designer.cs new file mode 100644 index 0000000000..76b4a5f9d8 --- /dev/null +++ b/samples/NativeWPFClient/Properties/Resources.Designer.cs @@ -0,0 +1,71 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by a tool. +// Runtime Version:4.0.30319.42000 +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +namespace NativeWPFClient.Properties +{ + + + /// + /// A strongly-typed resource class, for looking up localized strings, etc. + /// + // This class was auto-generated by the StronglyTypedResourceBuilder + // class via a tool like ResGen or Visual Studio. + // To add or remove a member, edit your .ResX file then rerun ResGen + // with the /str option, or rebuild your VS project. + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "4.0.0.0")] + [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] + [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] + internal class Resources + { + + private static global::System.Resources.ResourceManager resourceMan; + + private static global::System.Globalization.CultureInfo resourceCulture; + + [global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")] + internal Resources() + { + } + + /// + /// Returns the cached ResourceManager instance used by this class. + /// + [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] + internal static global::System.Resources.ResourceManager ResourceManager + { + get + { + if ((resourceMan == null)) + { + global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("NativeWPFClient.Properties.Resources", typeof(Resources).Assembly); + resourceMan = temp; + } + return resourceMan; + } + } + + /// + /// Overrides the current thread's CurrentUICulture property for all + /// resource lookups using this strongly typed resource class. + /// + [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] + internal static global::System.Globalization.CultureInfo Culture + { + get + { + return resourceCulture; + } + set + { + resourceCulture = value; + } + } + } +} diff --git a/samples/NativeWPFClient/Properties/Resources.resx b/samples/NativeWPFClient/Properties/Resources.resx new file mode 100644 index 0000000000..9c90483af8 --- /dev/null +++ b/samples/NativeWPFClient/Properties/Resources.resx @@ -0,0 +1,117 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/samples/NativeWPFClient/Properties/Settings.Designer.cs b/samples/NativeWPFClient/Properties/Settings.Designer.cs new file mode 100644 index 0000000000..2f38c66821 --- /dev/null +++ b/samples/NativeWPFClient/Properties/Settings.Designer.cs @@ -0,0 +1,30 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by a tool. +// Runtime Version:4.0.30319.42000 +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +namespace NativeWPFClient.Properties +{ + + + [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "11.0.0.0")] + internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase + { + + private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings()))); + + public static Settings Default + { + get + { + return defaultInstance; + } + } + } +} diff --git a/samples/NativeWPFClient/Properties/Settings.settings b/samples/NativeWPFClient/Properties/Settings.settings new file mode 100644 index 0000000000..033d7a5e9e --- /dev/null +++ b/samples/NativeWPFClient/Properties/Settings.settings @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file