-
-
Notifications
You must be signed in to change notification settings - Fork 305
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add new Windows Forms/Windows Presentation Foundation demos using Git…
…Hub authentication
- Loading branch information
1 parent
c12879c
commit 9ebebb6
Showing
17 changed files
with
593 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
samples/Sorgan/Sorgan.WinForms.Client/MainForm.Designer.cs
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
using System; | ||
using System.Threading; | ||
using System.Windows.Forms; | ||
using Dapplo.Microsoft.Extensions.Hosting.WinForms; | ||
using OpenIddict.Client; | ||
using static OpenIddict.Abstractions.OpenIddictConstants; | ||
using static OpenIddict.Abstractions.OpenIddictExceptions; | ||
|
||
namespace Sorgan.WinForms.Client; | ||
|
||
public partial class MainForm : Form, IWinFormsShell | ||
{ | ||
private readonly OpenIddictClientService _service; | ||
|
||
public MainForm(OpenIddictClientService service) | ||
{ | ||
_service = service ?? throw new ArgumentNullException(nameof(service)); | ||
|
||
InitializeComponent(); | ||
} | ||
|
||
private async void LoginButton_Click(object sender, EventArgs e) | ||
{ | ||
// Disable the login button to prevent concurrent authentication operations. | ||
LoginButton.Enabled = false; | ||
|
||
try | ||
{ | ||
using var source = new CancellationTokenSource(delay: TimeSpan.FromSeconds(90)); | ||
|
||
try | ||
{ | ||
// Ask OpenIddict to initiate the authentication flow (typically, by starting the system browser). | ||
var result = await _service.ChallengeInteractivelyAsync(new() | ||
{ | ||
CancellationToken = source.Token | ||
}); | ||
|
||
// Wait for the user to complete the authorization process. | ||
var principal = (await _service.AuthenticateInteractivelyAsync(new() | ||
{ | ||
CancellationToken = source.Token, | ||
Nonce = result.Nonce | ||
})).Principal; | ||
|
||
TaskDialog.ShowDialog(new TaskDialogPage | ||
{ | ||
Caption = "Authentication successful", | ||
Heading = "Authentication successful", | ||
Icon = TaskDialogIcon.ShieldSuccessGreenBar, | ||
Text = $"Welcome, {principal.FindFirst(Claims.Name)!.Value}." | ||
}); | ||
} | ||
|
||
catch (OperationCanceledException) | ||
{ | ||
TaskDialog.ShowDialog(new TaskDialogPage | ||
{ | ||
Caption = "Authentication timed out", | ||
Heading = "Authentication timed out", | ||
Icon = TaskDialogIcon.Warning, | ||
Text = "The authentication process was aborted." | ||
}); | ||
} | ||
|
||
catch (ProtocolException exception) when (exception.Error is Errors.AccessDenied) | ||
{ | ||
TaskDialog.ShowDialog(new TaskDialogPage | ||
{ | ||
Caption = "Authorization denied", | ||
Heading = "Authorization denied", | ||
Icon = TaskDialogIcon.Warning, | ||
Text = "The authorization was denied by the end user." | ||
}); | ||
} | ||
|
||
catch | ||
{ | ||
TaskDialog.ShowDialog(new TaskDialogPage | ||
{ | ||
Caption = "Authentication failed", | ||
Heading = "Authentication failed", | ||
Icon = TaskDialogIcon.Error, | ||
Text = "An error occurred while trying to authenticate the user." | ||
}); | ||
} | ||
} | ||
|
||
finally | ||
{ | ||
// Re-enable the login button to allow starting a new authentication operation. | ||
LoginButton.Enabled = true; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
using System.IO; | ||
using Dapplo.Microsoft.Extensions.Hosting.WinForms; | ||
using Microsoft.EntityFrameworkCore; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Hosting; | ||
using Microsoft.Extensions.Logging; | ||
using Sorgan.WinForms.Client; | ||
|
||
var host = new HostBuilder() | ||
// Note: applications for which a single instance is preferred can reference | ||
// the Dapplo.Microsoft.Extensions.Hosting.AppServices package and call this | ||
// method to automatically close extra instances based on the specified identifier: | ||
// | ||
// .ConfigureSingleInstance(options => options.MutexId = "{7113F751-8CD1-42D8-B294-E5F360497577}") | ||
// | ||
.ConfigureLogging(options => options.AddDebug()) | ||
.ConfigureServices(services => | ||
{ | ||
services.AddDbContext<DbContext>(options => | ||
{ | ||
options.UseSqlite($"Filename={Path.Combine(Path.GetTempPath(), "openiddict-sorgan-winforms-client.sqlite3")}"); | ||
options.UseOpenIddict(); | ||
}); | ||
|
||
services.AddOpenIddict() | ||
|
||
// Register the OpenIddict core components. | ||
.AddCore(options => | ||
{ | ||
// Configure OpenIddict to use the Entity Framework Core stores and models. | ||
// Note: call ReplaceDefaultEntities() to replace the default OpenIddict entities. | ||
options.UseEntityFrameworkCore() | ||
.UseDbContext<DbContext>(); | ||
}) | ||
|
||
// Register the OpenIddict client components. | ||
.AddClient(options => | ||
{ | ||
// Note: this sample uses the authorization code and refresh token | ||
// flows, but you can enable the other flows if necessary. | ||
options.AllowAuthorizationCodeFlow() | ||
.AllowRefreshTokenFlow(); | ||
|
||
// Register the signing and encryption credentials used to protect | ||
// sensitive data like the state tokens produced by OpenIddict. | ||
options.AddDevelopmentEncryptionCertificate() | ||
.AddDevelopmentSigningCertificate(); | ||
|
||
// Add the operating system integration. | ||
options.UseSystemIntegration(); | ||
|
||
// Register the System.Net.Http integration and use the identity of the current | ||
// assembly as a more specific user agent, which can be useful when dealing with | ||
// providers that use the user agent as a way to throttle requests (e.g Reddit). | ||
options.UseSystemNetHttp() | ||
.SetProductInformation(typeof(Program).Assembly); | ||
|
||
// Register the Web providers integrations. | ||
// | ||
// Note: to mitigate mix-up attacks, it's recommended to use a unique redirection endpoint | ||
// address per provider, unless all the registered providers support returning an "iss" | ||
// parameter containing their URL as part of authorization responses. For more information, | ||
// see https://datatracker.ietf.org/doc/html/draft-ietf-oauth-security-topics#section-4.4. | ||
options.UseWebProviders() | ||
.AddGitHub(options => | ||
{ | ||
options.SetClientId("fa9321227d63cda3f341") | ||
// Note: GitHub doesn't allow creating public clients and requires using a client secret. | ||
.SetClientSecret("d904b9b9ededc39da499b2ea4c13df5c7e35ddbe") | ||
// Note: GitHub doesn't support the recommended ":/" syntax and requires using "://". | ||
.SetRedirectUri("com.openiddict.sorgan.winforms.client://callback/login/github"); | ||
}); | ||
}); | ||
|
||
// Register the worker responsible for creating the database used to store tokens | ||
// and adding the registry entries required to register the custom URI scheme. | ||
// | ||
// Note: in a real world application, this step should be part of a setup script. | ||
services.AddHostedService<Worker>(); | ||
}) | ||
.ConfigureWinForms<MainForm>() | ||
.UseWinFormsLifetime() | ||
.Build(); | ||
|
||
await host.RunAsync(); |
20 changes: 20 additions & 0 deletions
20
samples/Sorgan/Sorgan.WinForms.Client/Sorgan.WinForms.Client.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>WinExe</OutputType> | ||
<TargetFramework>net8.0-windows7.0</TargetFramework> | ||
<EnableWindowsTargeting>true</EnableWindowsTargeting> | ||
<UseWindowsForms>true</UseWindowsForms> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Dapplo.Microsoft.Extensions.Hosting.AppServices" /> | ||
<PackageReference Include="Dapplo.Microsoft.Extensions.Hosting.WinForms" /> | ||
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" /> | ||
<PackageReference Include="OpenIddict.Client.SystemIntegration" /> | ||
<PackageReference Include="OpenIddict.Client.SystemNetHttp" /> | ||
<PackageReference Include="OpenIddict.Client.WebIntegration" /> | ||
<PackageReference Include="OpenIddict.EntityFrameworkCore" /> | ||
</ItemGroup> | ||
|
||
</Project> |
Oops, something went wrong.