|
| 1 | +# Using Microsoft dev tunnels <Badge type="warning" text="client" /><Badge type="danger" text="server" /> |
| 2 | + |
| 3 | +When developing an ASP.NET or ASP.NET Core-based web application running on your developer machine, you can usually directly connect to the server |
| 4 | +using the pre-configured `applicationUrl` (e.g `https://localhost:44359/`). If you are, however, developing an Android or iOS application |
| 5 | +using MAUI or Avalonia, the application will run either on a physical device or on a local emulator/simulator and using `localhost` may not be an option. |
| 6 | + |
| 7 | +To work around that, you can use |
| 8 | +[Microsoft dev tunnels](https://learn.microsoft.com/en-us/azure/developer/dev-tunnels/overview), that are available for both |
| 9 | +[Visual Studio Code](https://code.visualstudio.com/docs/editor/port-forwarding) and |
| 10 | +[Visual Studio](https://learn.microsoft.com/en-us/aspnet/core/test/dev-tunnels?view=aspnetcore-8.0). |
| 11 | + |
| 12 | +Explaining these tools in detail is outside the scope of this guide. For that, refer to the following links to: |
| 13 | +- [Create a dev tunnel in Visual Studio Code](https://code.visualstudio.com/docs/editor/port-forwarding). |
| 14 | +- [Create a dev tunnel in Visual Studio 2022](https://learn.microsoft.com/en-us/aspnet/core/test/dev-tunnels). |
| 15 | + |
| 16 | +## Change the issuer <Badge type="warning" text="client" /> |
| 17 | + |
| 18 | +In order to use the tunnel, you need to change the `Issuer` of the client |
| 19 | +registration in the client project to point to the tunnel URL (e.g. `MauiProgram.cs`): |
| 20 | + |
| 21 | +```csharp |
| 22 | +services.AddOpenIddict() |
| 23 | + .AddClient(options => |
| 24 | + { |
| 25 | + // ... |
| 26 | +
|
| 27 | + // Add a client registration matching the client application definition in the server project. |
| 28 | + options.AddRegistration(new OpenIddictClientRegistration |
| 29 | + { |
| 30 | + Issuer = new Uri("https://localhost:44395/", UriKind.Absolute), // [!code --] |
| 31 | + Issuer = new Uri("https://contoso.euw.devtunnels.ms/", UriKind.Absolute), // [!code ++] |
| 32 | + ProviderName = "Local", |
| 33 | + |
| 34 | + ClientId = "maui", |
| 35 | + |
| 36 | + // This sample uses protocol activations with a custom URI scheme to handle callbacks. |
| 37 | + // |
| 38 | + // For more information on how to construct private-use URI schemes, |
| 39 | + // read https://www.rfc-editor.org/rfc/rfc8252#section-7.1 and |
| 40 | + // https://www.rfc-editor.org/rfc/rfc7595#section-3.8. |
| 41 | + PostLogoutRedirectUri = new Uri("com.openiddict.sandbox.maui.client:/callback/logout/local", UriKind.Absolute), |
| 42 | + RedirectUri = new Uri("com.openiddict.sandbox.maui.client:/callback/login/local", UriKind.Absolute), |
| 43 | + |
| 44 | + Scopes = { Scopes.Email, Scopes.Profile, Scopes.OfflineAccess, "demo_api" } |
| 45 | + }); |
| 46 | + |
| 47 | + }); |
| 48 | +``` |
| 49 | + |
| 50 | +> [!WARNING] Use the tunnel domain |
| 51 | +> |
| 52 | +> One common pitfall with Microsoft dev tunnels is that they do not forward their domain name to the server but direct traffic to `localhost`. |
| 53 | +> This means that even though you connect to your server using the dev tunnels URL (e.g. `https://contoso.euw.devtunnels.ms/`), |
| 54 | +> the request URL on your server will be `https://localhost:44359/`. |
| 55 | +> |
| 56 | +> This causes a variety of issues such as: |
| 57 | +> - The request forgery protection feature not working correctly due to the antiforgery cookie |
| 58 | +created using the tunnel domain (and thus not served to `localhost`) being lost in the process. |
| 59 | +> |
| 60 | +> - The OpenIddict client stack not being able to connect to the server from a remote machine (e.g. Android emulator) due to the server metadata |
| 61 | +> (`/.well-known/openid-configuration`) containing endpoints starting with `https://localhost:44359/` instead of the dev tunnel URL. |
| 62 | +> |
| 63 | +> To fix this, you **MUST** check the _Use Tunnel Domain_ checkbox in the _Manage dev tunnel_ dialog: |
| 64 | +> |
| 65 | +>  |
| 66 | +
|
| 67 | +## Add a binding for IIS Express <Badge type="danger" text="server" /> |
| 68 | + |
| 69 | +When running your authorization server in IIS Express, you will also have edit the _applicationhost.config_ file and add a binding for your dev |
| 70 | +tunnel (otherwise you will get an error message when trying to connect to your authorization server via the tunnel URL). |
| 71 | + |
| 72 | +> [!TIP] |
| 73 | +> For example, if your application listens on `https://localhost:44359/` locally and your tunnel URL is `https://contoso.euw.devtunnels.ms/`: |
| 74 | +> |
| 75 | +> ```xml |
| 76 | +> <bindings> |
| 77 | +> <binding protocol="http" bindingInformation="*:55946:localhost" /> |
| 78 | +> <binding protocol="https" bindingInformation="*:44359:localhost" /> |
| 79 | +> <binding protocol="https" bindingInformation="*:44359:contoso.euw.devtunnels.ms" /> <!-- [!code add] --> |
| 80 | +> </bindings> |
| 81 | +> ``` |
| 82 | +
|
| 83 | +## Inspect network traffic |
| 84 | +
|
| 85 | +In case you want to trace or inspect the network traffic of your dev tunnel, simply add `-inspect` after the host name of your URL. |
| 86 | +
|
| 87 | +For example, `https://contoso.euw.devtunnels.ms/` must be changed to `https://contoso-inspect.euw.devtunnels.ms/`. |
0 commit comments