Browse Source

Use file-scoped namespaces in the WinForms/WPF samples

pull/1760/head
Kévin Chalet 3 years ago
parent
commit
28ec4293ef
  1. 125
      sandbox/OpenIddict.Sandbox.WinForms.Client/MainForm.Designer.cs
  2. 133
      sandbox/OpenIddict.Sandbox.WinForms.Client/MainForm.cs
  3. 4
      sandbox/OpenIddict.Sandbox.WinForms.Client/OpenIddict.Sandbox.WinForms.Client.csproj
  4. 7
      sandbox/OpenIddict.Sandbox.Wpf.Client/App.xaml.cs
  5. 81
      sandbox/OpenIddict.Sandbox.Wpf.Client/MainWindow.xaml.cs
  6. 4
      sandbox/OpenIddict.Sandbox.Wpf.Client/OpenIddict.Sandbox.Wpf.Client.csproj
  7. 5
      src/OpenIddict.Client/OpenIddictClientHandlers.cs

125
sandbox/OpenIddict.Sandbox.WinForms.Client/MainForm.Designer.cs

@ -1,73 +1,72 @@
namespace OpenIddict.Sandbox.WinForms.Client namespace OpenIddict.Sandbox.WinForms.Client;
partial class MainForm
{ {
partial class MainForm /// <summary>
{ /// Required designer variable.
/// <summary> /// </summary>
/// Required designer variable. private System.ComponentModel.IContainer components = null;
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary> /// <summary>
/// Clean up any resources being used. /// Clean up any resources being used.
/// </summary> /// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing) protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{ {
if (disposing && (components != null)) components.Dispose();
{
components.Dispose();
}
base.Dispose(disposing);
} }
base.Dispose(disposing);
}
#region Windows Form Designer generated code #region Windows Form Designer generated code
/// <summary> /// <summary>
/// Required method for Designer support - do not modify /// Required method for Designer support - do not modify
/// the contents of this method with the code editor. /// the contents of this method with the code editor.
/// </summary> /// </summary>
private void InitializeComponent() private void InitializeComponent()
{ {
this.LocalLogin = new System.Windows.Forms.Button(); this.LocalLogin = new System.Windows.Forms.Button();
this.GitHubLogin = new System.Windows.Forms.Button(); this.GitHubLogin = new System.Windows.Forms.Button();
this.SuspendLayout(); this.SuspendLayout();
// //
// LocalLogin // LocalLogin
// //
this.LocalLogin.Location = new System.Drawing.Point(258, 93); this.LocalLogin.Location = new System.Drawing.Point(258, 93);
this.LocalLogin.Name = "LocalLogin"; this.LocalLogin.Name = "LocalLogin";
this.LocalLogin.Size = new System.Drawing.Size(283, 83); this.LocalLogin.Size = new System.Drawing.Size(283, 83);
this.LocalLogin.TabIndex = 0; this.LocalLogin.TabIndex = 0;
this.LocalLogin.Text = "Log in using the local server"; this.LocalLogin.Text = "Log in using the local server";
this.LocalLogin.UseVisualStyleBackColor = true; this.LocalLogin.UseVisualStyleBackColor = true;
this.LocalLogin.Click += new System.EventHandler(this.LocalLoginButton_Click); this.LocalLogin.Click += new System.EventHandler(this.LocalLoginButton_Click);
// //
// GitHubLogin // GitHubLogin
// //
this.GitHubLogin.Location = new System.Drawing.Point(258, 258); this.GitHubLogin.Location = new System.Drawing.Point(258, 258);
this.GitHubLogin.Name = "GitHubLogin"; this.GitHubLogin.Name = "GitHubLogin";
this.GitHubLogin.Size = new System.Drawing.Size(283, 83); this.GitHubLogin.Size = new System.Drawing.Size(283, 83);
this.GitHubLogin.TabIndex = 1; this.GitHubLogin.TabIndex = 1;
this.GitHubLogin.Text = "Log in using GitHub"; this.GitHubLogin.Text = "Log in using GitHub";
this.GitHubLogin.UseVisualStyleBackColor = true; this.GitHubLogin.UseVisualStyleBackColor = true;
this.GitHubLogin.Click += new System.EventHandler(this.GitHubLoginButton_Click); this.GitHubLogin.Click += new System.EventHandler(this.GitHubLoginButton_Click);
// //
// MainForm // MainForm
// //
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(800, 450); this.ClientSize = new System.Drawing.Size(800, 450);
this.Controls.Add(this.GitHubLogin); this.Controls.Add(this.GitHubLogin);
this.Controls.Add(this.LocalLogin); this.Controls.Add(this.LocalLogin);
this.Name = "MainForm"; this.Name = "MainForm";
this.Text = "OpenIddict WinForms client"; this.Text = "OpenIddict WinForms client";
this.ResumeLayout(false); this.ResumeLayout(false);
} }
#endregion #endregion
private Button LocalLogin; private Button LocalLogin;
private Button GitHubLogin; private Button GitHubLogin;
}
} }

133
sandbox/OpenIddict.Sandbox.WinForms.Client/MainForm.cs

@ -4,97 +4,96 @@ using static OpenIddict.Abstractions.OpenIddictConstants;
using static OpenIddict.Abstractions.OpenIddictExceptions; using static OpenIddict.Abstractions.OpenIddictExceptions;
using static OpenIddict.Client.WebIntegration.OpenIddictClientWebIntegrationConstants; using static OpenIddict.Client.WebIntegration.OpenIddictClientWebIntegrationConstants;
namespace OpenIddict.Sandbox.WinForms.Client namespace OpenIddict.Sandbox.WinForms.Client;
public partial class MainForm : Form, IWinFormsShell
{ {
public partial class MainForm : Form, IWinFormsShell private readonly OpenIddictClientService _service;
public MainForm(OpenIddictClientService service)
{ {
private readonly OpenIddictClientService _service; _service = service ?? throw new ArgumentNullException(nameof(service));
public MainForm(OpenIddictClientService service) InitializeComponent();
{ }
_service = service ?? throw new ArgumentNullException(nameof(service));
InitializeComponent(); private async void LocalLoginButton_Click(object sender, EventArgs e)
} => await AuthenticateAsync("Local");
private async void LocalLoginButton_Click(object sender, EventArgs e) private async void GitHubLoginButton_Click(object sender, EventArgs e)
=> await AuthenticateAsync("Local"); => await AuthenticateAsync(Providers.GitHub);
private async void GitHubLoginButton_Click(object sender, EventArgs e) private async Task AuthenticateAsync(string provider)
=> await AuthenticateAsync(Providers.GitHub); {
using var source = new CancellationTokenSource(delay: TimeSpan.FromSeconds(90));
private async Task AuthenticateAsync(string provider) try
{ {
using var source = new CancellationTokenSource(delay: TimeSpan.FromSeconds(90)); // Ask OpenIddict to initiate the authentication flow (typically, by
// starting the system browser) and wait for the user to complete it.
try var (_, _, principal) = await _service.AuthenticateInteractivelyAsync(
{ provider, cancellationToken: source.Token);
// Ask OpenIddict to initiate the authentication flow (typically, by
// starting the system browser) and wait for the user to complete it.
var (_, _, principal) = await _service.AuthenticateInteractivelyAsync(
provider, cancellationToken: source.Token);
#if SUPPORTS_WINFORMS_TASK_DIALOG #if SUPPORTS_WINFORMS_TASK_DIALOG
TaskDialog.ShowDialog(new TaskDialogPage TaskDialog.ShowDialog(new TaskDialogPage
{ {
Caption = "Authentication successful", Caption = "Authentication successful",
Heading = "Authentication successful", Heading = "Authentication successful",
Icon = TaskDialogIcon.ShieldSuccessGreenBar, Icon = TaskDialogIcon.ShieldSuccessGreenBar,
Text = $"Welcome, {principal.FindFirst(Claims.Name)!.Value}." Text = $"Welcome, {principal.FindFirst(Claims.Name)!.Value}."
}); });
#else #else
MessageBox.Show($"Welcome, {principal.FindFirst(Claims.Name)!.Value}.", MessageBox.Show($"Welcome, {principal.FindFirst(Claims.Name)!.Value}.",
"Authentication successful", MessageBoxButtons.OK, MessageBoxIcon.Information); "Authentication successful", MessageBoxButtons.OK, MessageBoxIcon.Information);
#endif #endif
} }
catch (OperationCanceledException) catch (OperationCanceledException)
{ {
#if SUPPORTS_WINFORMS_TASK_DIALOG #if SUPPORTS_WINFORMS_TASK_DIALOG
TaskDialog.ShowDialog(new TaskDialogPage TaskDialog.ShowDialog(new TaskDialogPage
{ {
Caption = "Authentication timed out", Caption = "Authentication timed out",
Heading = "Authentication timed out", Heading = "Authentication timed out",
Icon = TaskDialogIcon.Warning, Icon = TaskDialogIcon.Warning,
Text = "The authentication process was aborted." Text = "The authentication process was aborted."
}); });
#else #else
MessageBox.Show("The authentication process was aborted.", MessageBox.Show("The authentication process was aborted.",
"Authentication timed out", MessageBoxButtons.OK, MessageBoxIcon.Warning); "Authentication timed out", MessageBoxButtons.OK, MessageBoxIcon.Warning);
#endif #endif
} }
catch (ProtocolException exception) when (exception.Error is Errors.AccessDenied) catch (ProtocolException exception) when (exception.Error is Errors.AccessDenied)
{ {
#if SUPPORTS_WINFORMS_TASK_DIALOG #if SUPPORTS_WINFORMS_TASK_DIALOG
TaskDialog.ShowDialog(new TaskDialogPage TaskDialog.ShowDialog(new TaskDialogPage
{ {
Caption = "Authorization denied", Caption = "Authorization denied",
Heading = "Authorization denied", Heading = "Authorization denied",
Icon = TaskDialogIcon.Warning, Icon = TaskDialogIcon.Warning,
Text = "The authorization was denied by the end user." Text = "The authorization was denied by the end user."
}); });
#else #else
MessageBox.Show("The authorization was denied by the end user.", MessageBox.Show("The authorization was denied by the end user.",
"Authorization denied", MessageBoxButtons.OK, MessageBoxIcon.Warning); "Authorization denied", MessageBoxButtons.OK, MessageBoxIcon.Warning);
#endif #endif
} }
catch catch
{ {
#if SUPPORTS_WINFORMS_TASK_DIALOG #if SUPPORTS_WINFORMS_TASK_DIALOG
TaskDialog.ShowDialog(new TaskDialogPage TaskDialog.ShowDialog(new TaskDialogPage
{ {
Caption = "Authentication failed", Caption = "Authentication failed",
Heading = "Authentication failed", Heading = "Authentication failed",
Icon = TaskDialogIcon.Error, Icon = TaskDialogIcon.Error,
Text = "An error occurred while trying to authenticate the user." Text = "An error occurred while trying to authenticate the user."
}); });
#else #else
MessageBox.Show("An error occurred while trying to authenticate the user.", MessageBox.Show("An error occurred while trying to authenticate the user.",
"Authentication failed", MessageBoxButtons.OK, MessageBoxIcon.Error); "Authentication failed", MessageBoxButtons.OK, MessageBoxIcon.Error);
#endif #endif
}
} }
} }
} }

4
sandbox/OpenIddict.Sandbox.WinForms.Client/OpenIddict.Sandbox.WinForms.Client.csproj

@ -24,8 +24,4 @@
<PackageReference Include="Microsoft.Extensions.Hosting" VersionOverride="7.0.0" /> <PackageReference Include="Microsoft.Extensions.Hosting" VersionOverride="7.0.0" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<Using Remove="System.Net.Http" />
</ItemGroup>
</Project> </Project>

7
sandbox/OpenIddict.Sandbox.Wpf.Client/App.xaml.cs

@ -1,8 +1,7 @@
using System.Windows; using System.Windows;
namespace OpenIddict.Sandbox.Wpf.Client namespace OpenIddict.Sandbox.Wpf.Client;
public partial class App : Application
{ {
public partial class App : Application
{
}
} }

81
sandbox/OpenIddict.Sandbox.Wpf.Client/MainWindow.xaml.cs

@ -5,57 +5,56 @@ using static OpenIddict.Abstractions.OpenIddictConstants;
using static OpenIddict.Abstractions.OpenIddictExceptions; using static OpenIddict.Abstractions.OpenIddictExceptions;
using static OpenIddict.Client.WebIntegration.OpenIddictClientWebIntegrationConstants; using static OpenIddict.Client.WebIntegration.OpenIddictClientWebIntegrationConstants;
namespace OpenIddict.Sandbox.Wpf.Client namespace OpenIddict.Sandbox.Wpf.Client;
public partial class MainWindow : Window, IWpfShell
{ {
public partial class MainWindow : Window, IWpfShell private readonly OpenIddictClientService _service;
public MainWindow(OpenIddictClientService service)
{
_service = service ?? throw new ArgumentNullException(nameof(service));
InitializeComponent();
}
private async void LocalLoginButton_Click(object sender, RoutedEventArgs e)
=> await AuthenticateAsync("Local");
private async void GitHubLoginButton_Click(object sender, RoutedEventArgs e)
=> await AuthenticateAsync(Providers.GitHub);
private async Task AuthenticateAsync(string provider)
{ {
private readonly OpenIddictClientService _service; using var source = new CancellationTokenSource(delay: TimeSpan.FromSeconds(90));
public MainWindow(OpenIddictClientService service) try
{ {
_service = service ?? throw new ArgumentNullException(nameof(service)); // Ask OpenIddict to initiate the authentication flow (typically, by
// starting the system browser) and wait for the user to complete it.
var (_, _, principal) = await _service.AuthenticateInteractivelyAsync(
provider, cancellationToken: source.Token);
InitializeComponent(); MessageBox.Show($"Welcome, {principal.FindFirst(Claims.Name)!.Value}.",
"Authentication successful", MessageBoxButton.OK, MessageBoxImage.Information);
} }
private async void LocalLoginButton_Click(object sender, RoutedEventArgs e) catch (OperationCanceledException)
=> await AuthenticateAsync("Local"); {
MessageBox.Show("The authentication process was aborted.",
"Authentication timed out", MessageBoxButton.OK, MessageBoxImage.Warning);
}
private async void GitHubLoginButton_Click(object sender, RoutedEventArgs e) catch (ProtocolException exception) when (exception.Error is Errors.AccessDenied)
=> await AuthenticateAsync(Providers.GitHub); {
MessageBox.Show("The authorization was denied by the end user.",
"Authorization denied", MessageBoxButton.OK, MessageBoxImage.Warning);
}
private async Task AuthenticateAsync(string provider) catch
{ {
using var source = new CancellationTokenSource(delay: TimeSpan.FromSeconds(90)); MessageBox.Show("An error occurred while trying to authenticate the user.",
"Authentication failed", MessageBoxButton.OK, MessageBoxImage.Error);
try
{
// Ask OpenIddict to initiate the authentication flow (typically, by
// starting the system browser) and wait for the user to complete it.
var (_, _, principal) = await _service.AuthenticateInteractivelyAsync(
provider, cancellationToken: source.Token);
MessageBox.Show($"Welcome, {principal.FindFirst(Claims.Name)!.Value}.",
"Authentication successful", MessageBoxButton.OK, MessageBoxImage.Information);
}
catch (OperationCanceledException)
{
MessageBox.Show("The authentication process was aborted.",
"Authentication timed out", MessageBoxButton.OK, MessageBoxImage.Warning);
}
catch (ProtocolException exception) when (exception.Error is Errors.AccessDenied)
{
MessageBox.Show("The authorization was denied by the end user.",
"Authorization denied", MessageBoxButton.OK, MessageBoxImage.Warning);
}
catch
{
MessageBox.Show("An error occurred while trying to authenticate the user.",
"Authentication failed", MessageBoxButton.OK, MessageBoxImage.Error);
}
} }
} }
} }

4
sandbox/OpenIddict.Sandbox.Wpf.Client/OpenIddict.Sandbox.Wpf.Client.csproj

@ -25,8 +25,4 @@
<PackageReference Include="Microsoft.Extensions.Hosting" VersionOverride="7.0.0" /> <PackageReference Include="Microsoft.Extensions.Hosting" VersionOverride="7.0.0" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<Using Remove="System.Net.Http" />
</ItemGroup>
</Project> </Project>

5
src/OpenIddict.Client/OpenIddictClientHandlers.cs

@ -1221,9 +1221,8 @@ public static partial class OpenIddictClientHandlers
Debug.Assert(context.StateTokenPrincipal is { Identity: ClaimsIdentity }, SR.GetResourceString(SR.ID4006)); Debug.Assert(context.StateTokenPrincipal is { Identity: ClaimsIdentity }, SR.GetResourceString(SR.ID4006));
// Resolve the negotiated flow from the state token. // Resolve the negotiated flow from the state token.
(context.GrantType, context.ResponseType) = ( context.GrantType = context.StateTokenPrincipal.GetClaim(Claims.Private.GrantType);
context.StateTokenPrincipal.GetClaim(Claims.Private.GrantType), context.ResponseType = context.StateTokenPrincipal.GetClaim(Claims.Private.ResponseType);
context.StateTokenPrincipal.GetClaim(Claims.Private.ResponseType));
switch ((context.EndpointType, context.GrantType, context.ResponseType)) switch ((context.EndpointType, context.GrantType, context.ResponseType))
{ {

Loading…
Cancel
Save