Skip to main content

Dotnet Desktop App

An Dotnet desktop app example for Casdoor.

How to run example

Prerequisites

dotnet6 sdk

webview2 runtime (It's already preinstalled in your windows generally)

Initialization

The initialization requires 5 parameters, which are all string type:

NameDescriptionFile
DomainYour Casdoor server host/domainCasdoorVariables.cs
ClientIdThe Client ID of your Casdoor applicationCasdoorVariables.cs
AppNameThe name of your Casdoor applicationCasdoorVariables.cs
CallbackUrlThe path of the callback URL for your Casdoor application, will be casdoor://callback if not providedCasdoorVariables.cs
ClientSecretThe Client Secret of your Casdoor applicationCasdoorVariables.cs

If you don't set these parameters, this project will use the Casdoor online demo as the default Casdoor server and use the Casnode as the default Casdoor application.

Running

Visual Studio

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

Command line

  1. cd src/DesktopApp
  2. dotnet run

Preview

After you run this dotnet desktop application, a new window will be showed on your desktop. index

If you click Casdoor Login button, a login window will be showed on your desktop. login

After you login successfully, a user profile window will be showed on your desktop. It displays your user name. user profile

You can preview the whole process by the gif image below. preview gif

How to integrate

Open the login window

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

Use auth code to get the user info

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 _casdoorApi.RequestToken(
CasdoorVariables.ClientId,
CasdoorVariables.ClientSecret,
authCode
);

var user = await _casdoorApi.GetUserInfo(token);