From 6b66de28216c595a7c8707ba88dea2a69824f31b Mon Sep 17 00:00:00 2001 From: Arnaud Dagnelies Date: Fri, 5 Jul 2024 07:34:40 +0000 Subject: [PATCH] added tutorial --- _config.yml | 1 + getting_started/dot_net.md | 45 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+) create mode 100644 getting_started/dot_net.md diff --git a/_config.yml b/_config.yml index 8526b52..1428d9f 100644 --- a/_config.yml +++ b/_config.yml @@ -13,6 +13,7 @@ navbar-links: - Javascript - Vanilla: getting_started/javascript_spa - Python - FastAPI: https://github.com/passwordless-id/fast-api-demo - Java - Spring Boot: https://github.com/passwordless-id/spring-boot-demo + - .Net / C#: getting_started/dot_net - Want more?: getting_started/want_more Docs: - The "big picture": docs/1_big_picture diff --git a/getting_started/dot_net.md b/getting_started/dot_net.md new file mode 100644 index 0000000..c44cfb6 --- /dev/null +++ b/getting_started/dot_net.md @@ -0,0 +1,45 @@ +Getting started with .Net +========================= + +> Contributed by @moberauer, source: https://github.com/moberauer/passwordless.id-ASP.NET-Core-Demo + +# Passwordless.ID - ASP.NET Core Demo + +> This [example repository](https://github.com/moberauer/passwordless.id-ASP.NET-Core-Demo) shows an integration between [ASP.NET Core](https://dotnet.microsoft.com/en-us/apps/aspnet) and [Passwordless.ID](https://passwordless.id). +> +> The project uses the .NET web sdk and just one NuGet package needs to be installed: `Microsoft.AspNetCore.Authentication.OpenIdConnect` + +Using the OpenIdConnect SDK, set the default authentication scheme to OpenId, configure it to use the authoriozation code flow, the clientId and the metadata address provided by [Passwordless.ID](https://passwordless.id). Then we also add an external cookie scheme to store the user information once sign in via [Passwordless.ID](https://passwordless.id) was successful. + + +```csharp +var builder = WebApplication.CreateBuilder(args); +builder.Services + .AddAuthentication(defaultScheme: OpenIdConnectDefaults.AuthenticationScheme) + .AddOpenIdConnect(openIdConnectOptions => + { + openIdConnectOptions.SignInScheme = IdentityConstants.ExternalScheme; + openIdConnectOptions.ResponseType = OpenIdConnectResponseType.Code; + openIdConnectOptions.ClientId = "https://localhost"; + openIdConnectOptions.MetadataAddress = "https://api.passwordless.id/.well-known/openid-configuration"; + }) + .AddExternalCookie(); +builder.Services.AddAuthorization(); +var app = builder.Build(); +``` +Now we configure the request pipeline to use the authentication and authorization middleware provided by ASP.NET Core and we map two endpoints: one which does not require authentication and one which does. + +```csharp +var app = builder.Build(); +app.UseAuthentication(); +app.UseAuthorization(); +app.MapGet("/", () => "Go to /private to authenticate"); +app.MapGet("/private", context => { + string username = context.User.FindFirst("preferred_username")?.Value ?? string.Empty; + return context.Response.WriteAsync($"Hello, {username}!"); +}).RequireAuthorization(); + +app.Run(); +``` + +When we hit the authorized endpoint the authentication and authorization middleware take care of the redirects and callbacks to [Passwordless.ID](https://passwordless.id), code retrieval, id_token retrieval and storing the userinfo in the external cookie scheme cookie named *Identity.External*. The user information including all the claims provided by [Passwordless.ID](https://passwordless.id) is then available to our endpoint code via *context.User*. \ No newline at end of file