dotNET Desktop App
A Dotnet desktop app example for Casdoor.
How to Run the Example
Prerequisites
- dotNET 6 SDK
- WebView2 Runtime (It is usually preinstalled on Windows)
Initialization
The initialization requires 5 parameters, all of which are of type string:
Name | Description | File |
---|---|---|
Domain | The host/domain of your Casdoor server | CasdoorVariables.cs |
ClientId | The Client ID of your Casdoor application | CasdoorVariables.cs |
AppName | The name of your Casdoor application | CasdoorVariables.cs |
CallbackUrl | The path of the callback URL for your Casdoor application. If not provided, it will be casdoor://callback | CasdoorVariables.cs |
ClientSecret | The Client Secret of your Casdoor application | CasdoorVariables.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
- Open
casdoor-dotnet-desktop-example.sln
- Press
Ctrl + F5
to start
Command Line
cd src/DesktopApp
dotnet run
Preview
After running the dotNET desktop application, a new window will appear on your desktop.
If you click the Casdoor Login
button, a login window will appear on your desktop.
After successfully logging in, a user profile window will appear on your desktop, displaying your username.
You can preview the entire process in the GIF image below.
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);