Skip to main content

dotNET Desktop App

A Dotnet desktop app example for Casdoor.

How to Run the Example

Prerequisites

Initialization

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

NameDescriptionFile
DomainThe host/domain of your Casdoor serverCasdoorVariables.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. If not provided, it will be casdoor://callbackCasdoorVariables.cs
ClientSecretThe Client Secret of your Casdoor applicationCasdoorVariables.cs

If you do not set these parameters, the project will default to using the Casdoor online demo as the Casdoor server and the Casnode as the 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 running the dotNET desktop application, a new window will appear on your desktop. index

If you click the Casdoor Login button, a login window will appear on your desktop. login

After successfully logging in, a user profile window will appear on your desktop, displaying your username. user profile

You can preview the entire process in the GIF image below. preview gif

How to Integrate

Opening 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();

Using the Auth Code to Get 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);