Dotnet Desktop App
An Dotnet desktop app example for Casdoor.
How to run example
Prerequisites
webview2 runtime (It's already preinstalled in your windows generally)
Initialization
The initialization requires 5 parameters, which are all string type:
Name | Description | File |
---|---|---|
Domain | Your Casdoor server host/domain | 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, will be casdoor://callback if not provided | CasdoorVariables.cs |
ClientSecret | The Client Secret of your Casdoor application | CasdoorVariables.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
- Open casdoor-dotnet-desktop-example.sln
- Press Ctrl + F5 to start
Command line
- cd src/DesktopApp
- dotnet run
Preview
After you run this dotnet desktop application, a new window will be showed on your desktop.
If you click Casdoor Login
button, a login window will be showed on your desktop.
After you login successfully, a user profile window will be showed on your desktop. It displays your user name.
You can preview the whole process by the gif image below.
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);