You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
108 lines
5.1 KiB
108 lines
5.1 KiB
using System.IO;
|
|
using Dapplo.Microsoft.Extensions.Hosting.Wpf;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Microsoft.Extensions.Hosting;
|
|
using Microsoft.Extensions.Logging;
|
|
using OpenIddict.Client;
|
|
using OpenIddict.Sandbox.Wpf.Client;
|
|
using static OpenIddict.Abstractions.OpenIddictConstants;
|
|
|
|
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 = "{C587B9EA-A870-4CF3-8B00-33DF67FCA143}")
|
|
//
|
|
.ConfigureLogging(options => options.AddDebug())
|
|
.ConfigureServices(services =>
|
|
{
|
|
services.AddDbContext<DbContext>(options =>
|
|
{
|
|
options.UseSqlite($"Filename={Path.Combine(Path.GetTempPath(), "openiddict-sandbox-wpf-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);
|
|
|
|
// Add a client registration matching the client application definition in the server project.
|
|
options.AddRegistration(new OpenIddictClientRegistration
|
|
{
|
|
Issuer = new Uri("https://localhost:44395/", UriKind.Absolute),
|
|
ProviderName = "Local",
|
|
|
|
ClientId = "wpf",
|
|
|
|
// This sample uses protocol activations with a custom URI scheme to handle callbacks.
|
|
//
|
|
// For more information on how to construct private-use URI schemes,
|
|
// read https://www.rfc-editor.org/rfc/rfc8252#section-7.1 and
|
|
// https://www.rfc-editor.org/rfc/rfc7595#section-3.8.
|
|
RedirectUri = new Uri("com.openiddict.sandbox.wpf.client:/callback/login/local", UriKind.Absolute),
|
|
|
|
Scopes = { Scopes.Email, Scopes.Profile, Scopes.OfflineAccess, "demo_api" }
|
|
});
|
|
|
|
// 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("8abc54b6d5f4e39d78aa")
|
|
.SetClientSecret("f37ef38bdb18a0f5f2d430a8edbed4353c012dc3")
|
|
// Note: GitHub doesn't support the recommended ":/" syntax and requires using "://".
|
|
.SetRedirectUri("com.openiddict.sandbox.wpf.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>();
|
|
})
|
|
.ConfigureWpf(options =>
|
|
{
|
|
options.UseApplication<App>();
|
|
options.UseWindow<MainWindow>();
|
|
})
|
|
.UseWpfLifetime()
|
|
.Build();
|
|
|
|
await host.RunAsync();
|