Skip to content

Commit

Permalink
added tutorial
Browse files Browse the repository at this point in the history
  • Loading branch information
dagnelies committed Jul 5, 2024
1 parent 09e76da commit 6b66de2
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
1 change: 1 addition & 0 deletions _config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
45 changes: 45 additions & 0 deletions getting_started/dot_net.md
Original file line number Diff line number Diff line change
@@ -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*.

0 comments on commit 6b66de2

Please sign in to comment.