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>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.LocalLogin = new System.Windows.Forms.Button();
this.GitHubLogin = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// LocalLogin
//
this.LocalLogin.Location = new System.Drawing.Point(258, 93);
this.LocalLogin.Name = "LocalLogin";
this.LocalLogin.Size = new System.Drawing.Size(283, 83);
this.LocalLogin.TabIndex = 0;
this.LocalLogin.Text = "Log in using the local server";
this.LocalLogin.UseVisualStyleBackColor = true;
this.LocalLogin.Click += new System.EventHandler(this.LocalLoginButton_Click);
//
// GitHubLogin
//
this.GitHubLogin.Location = new System.Drawing.Point(258, 258);
this.GitHubLogin.Name = "GitHubLogin";
this.GitHubLogin.Size = new System.Drawing.Size(283, 83);
this.GitHubLogin.TabIndex = 1;
this.GitHubLogin.Text = "Log in using GitHub";
this.GitHubLogin.UseVisualStyleBackColor = true;
this.GitHubLogin.Click += new System.EventHandler(this.GitHubLoginButton_Click);
//
// MainForm
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(800, 450);
this.Controls.Add(this.GitHubLogin);
this.Controls.Add(this.LocalLogin);
this.Name = "MainForm";
this.Text = "OpenIddict WinForms client";
this.ResumeLayout(false);
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.LocalLogin = new System.Windows.Forms.Button();
this.GitHubLogin = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// LocalLogin
//
this.LocalLogin.Location = new System.Drawing.Point(258, 93);
this.LocalLogin.Name = "LocalLogin";
this.LocalLogin.Size = new System.Drawing.Size(283, 83);
this.LocalLogin.TabIndex = 0;
this.LocalLogin.Text = "Log in using the local server";
this.LocalLogin.UseVisualStyleBackColor = true;
this.LocalLogin.Click += new System.EventHandler(this.LocalLoginButton_Click);
//
// GitHubLogin
//
this.GitHubLogin.Location = new System.Drawing.Point(258, 258);
this.GitHubLogin.Name = "GitHubLogin";
this.GitHubLogin.Size = new System.Drawing.Size(283, 83);
this.GitHubLogin.TabIndex = 1;
this.GitHubLogin.Text = "Log in using GitHub";
this.GitHubLogin.UseVisualStyleBackColor = true;
this.GitHubLogin.Click += new System.EventHandler(this.GitHubLoginButton_Click);
//
// MainForm
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(800, 450);
this.Controls.Add(this.GitHubLogin);
this.Controls.Add(this.LocalLogin);
this.Name = "MainForm";
this.Text = "OpenIddict WinForms client";
this.ResumeLayout(false);
}
}
#endregion
#endregion
private Button LocalLogin;
private Button GitHubLogin;
}
private Button LocalLogin;
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.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)
{
_service = service ?? throw new ArgumentNullException(nameof(service));
InitializeComponent();
}
InitializeComponent();
}
private async void LocalLoginButton_Click(object sender, EventArgs e)
=> await AuthenticateAsync("Local");
private async void LocalLoginButton_Click(object sender, EventArgs e)
=> await AuthenticateAsync("Local");
private async void GitHubLoginButton_Click(object sender, EventArgs e)
=> await AuthenticateAsync(Providers.GitHub);
private async void GitHubLoginButton_Click(object sender, EventArgs e)
=> await AuthenticateAsync(Providers.GitHub);
private async Task AuthenticateAsync(string provider)
{
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));
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);
// 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
TaskDialog.ShowDialog(new TaskDialogPage
{
Caption = "Authentication successful",
Heading = "Authentication successful",
Icon = TaskDialogIcon.ShieldSuccessGreenBar,
Text = $"Welcome, {principal.FindFirst(Claims.Name)!.Value}."
});
TaskDialog.ShowDialog(new TaskDialogPage
{
Caption = "Authentication successful",
Heading = "Authentication successful",
Icon = TaskDialogIcon.ShieldSuccessGreenBar,
Text = $"Welcome, {principal.FindFirst(Claims.Name)!.Value}."
});
#else
MessageBox.Show($"Welcome, {principal.FindFirst(Claims.Name)!.Value}.",
"Authentication successful", MessageBoxButtons.OK, MessageBoxIcon.Information);
MessageBox.Show($"Welcome, {principal.FindFirst(Claims.Name)!.Value}.",
"Authentication successful", MessageBoxButtons.OK, MessageBoxIcon.Information);
#endif
}
}
catch (OperationCanceledException)
{
catch (OperationCanceledException)
{
#if SUPPORTS_WINFORMS_TASK_DIALOG
TaskDialog.ShowDialog(new TaskDialogPage
{
Caption = "Authentication timed out",
Heading = "Authentication timed out",
Icon = TaskDialogIcon.Warning,
Text = "The authentication process was aborted."
});
TaskDialog.ShowDialog(new TaskDialogPage
{
Caption = "Authentication timed out",
Heading = "Authentication timed out",
Icon = TaskDialogIcon.Warning,
Text = "The authentication process was aborted."
});
#else
MessageBox.Show("The authentication process was aborted.",
"Authentication timed out", MessageBoxButtons.OK, MessageBoxIcon.Warning);
MessageBox.Show("The authentication process was aborted.",
"Authentication timed out", MessageBoxButtons.OK, MessageBoxIcon.Warning);
#endif
}
}
catch (ProtocolException exception) when (exception.Error is Errors.AccessDenied)
{
catch (ProtocolException exception) when (exception.Error is Errors.AccessDenied)
{
#if SUPPORTS_WINFORMS_TASK_DIALOG
TaskDialog.ShowDialog(new TaskDialogPage
{
Caption = "Authorization denied",
Heading = "Authorization denied",
Icon = TaskDialogIcon.Warning,
Text = "The authorization was denied by the end user."
});
TaskDialog.ShowDialog(new TaskDialogPage
{
Caption = "Authorization denied",
Heading = "Authorization denied",
Icon = TaskDialogIcon.Warning,
Text = "The authorization was denied by the end user."
});
#else
MessageBox.Show("The authorization was denied by the end user.",
"Authorization denied", MessageBoxButtons.OK, MessageBoxIcon.Warning);
MessageBox.Show("The authorization was denied by the end user.",
"Authorization denied", MessageBoxButtons.OK, MessageBoxIcon.Warning);
#endif
}
}
catch
{
catch
{
#if SUPPORTS_WINFORMS_TASK_DIALOG
TaskDialog.ShowDialog(new TaskDialogPage
{
Caption = "Authentication failed",
Heading = "Authentication failed",
Icon = TaskDialogIcon.Error,
Text = "An error occurred while trying to authenticate the user."
});
TaskDialog.ShowDialog(new TaskDialogPage
{
Caption = "Authentication failed",
Heading = "Authentication failed",
Icon = TaskDialogIcon.Error,
Text = "An error occurred while trying to authenticate the user."
});
#else
MessageBox.Show("An error occurred while trying to authenticate the user.",
"Authentication failed", MessageBoxButtons.OK, MessageBoxIcon.Error);
MessageBox.Show("An error occurred while trying to authenticate the user.",
"Authentication failed", MessageBoxButtons.OK, MessageBoxIcon.Error);
#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" />
</ItemGroup>
<ItemGroup>
<Using Remove="System.Net.Http" />
</ItemGroup>
</Project>

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

@ -1,8 +1,7 @@
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.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)
=> await AuthenticateAsync("Local");
catch (OperationCanceledException)
{
MessageBox.Show("The authentication process was aborted.",
"Authentication timed out", MessageBoxButton.OK, MessageBoxImage.Warning);
}
private async void GitHubLoginButton_Click(object sender, RoutedEventArgs e)
=> await AuthenticateAsync(Providers.GitHub);
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);
}
private async Task AuthenticateAsync(string provider)
catch
{
using var source = new CancellationTokenSource(delay: TimeSpan.FromSeconds(90));
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);
}
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" />
</ItemGroup>
<ItemGroup>
<Using Remove="System.Net.Http" />
</ItemGroup>
</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));
// Resolve the negotiated flow from the state token.
(context.GrantType, context.ResponseType) = (
context.StateTokenPrincipal.GetClaim(Claims.Private.GrantType),
context.StateTokenPrincipal.GetClaim(Claims.Private.ResponseType));
context.GrantType = context.StateTokenPrincipal.GetClaim(Claims.Private.GrantType);
context.ResponseType = context.StateTokenPrincipal.GetClaim(Claims.Private.ResponseType);
switch ((context.EndpointType, context.GrantType, context.ResponseType))
{

Loading…
Cancel
Save