Hanzo
PlatformHanzo IAMConnecting to IAMDesktop SDKs

.NET desktop app

Integrate Hanzo IAM in a .NET desktop app with WebView2.

The iam-dotnet-desktop-example shows Hanzo IAM sign-in in a .NET desktop app using WebView2.

Run the example

Prerequisites

Initialization

Set these 5 string parameters:

NameDescriptionFile
DomainThe host/domain of your Hanzo IAM serverHanzo IAMVariables.cs
ClientIdThe Client ID of your Hanzo IAM applicationHanzo IAMVariables.cs
AppNameThe name of your Hanzo IAM applicationHanzo IAMVariables.cs
CallbackUrlThe path of the callback URL for your Hanzo IAM application. If not provided, it will be iam://callbackHanzo IAMVariables.cs
ClientSecretThe Client Secret of your Hanzo IAM applicationHanzo IAMVariables.cs

Defaults: Hanzo IAM demo and app-casnode if not set.

Running

Visual Studio

  1. Open iam-dotnet-desktop-example.sln
  2. Press Ctrl + F5 to start

Command line

  1. cd src/DesktopApp
  2. dotnet run

Preview

index

Click Hanzo IAM Login to open the login window. After sign-in, the user profile is shown.

login user profile preview gif

Integration

Open the login window

var login = new Login();
// Triggered when login succeeds, you will receive an auth code in the event handler
login.CodeReceived += Login_CodeReceived;
login.ShowDialog();

### Exchange the auth code for user info

```csharp
public async Task<string?> RequestToken(string clientId, string clientSecret, string code)
{
    var body = new
    {
        grant_type = "authorization_code",
        client_id = clientId,
        client_secret = clientSecret,
        code
    };

    var req = new RestRequest(_requestTokenUrl).AddJsonBody(body);
    var token = await _client.PostAsync<TokenDto>(req);

    return token?.AccessToken;
}

public async Task<UserDto?> GetUserInfo(string token)
{
    var req = new RestRequest(_getUserInfoUrl).AddQueryParameter("accessToken", token);

    return await _client.GetAsync<UserDto>(req);
}

...

var token = await _iamApi.RequestToken(
    Hanzo IAMVariables.ClientId,
    Hanzo IAMVariables.ClientSecret,
    authCode,
);

var user = await _iamApi.GetUserInfo(token);

How is this guide?

Last updated on

On this page