跳到主内容

.NET desktop app

The casdoor-dotnet-desktop-example shows Casdoor sign-in in a .NET desktop app using WebView2.

Run the example

先决条件

初始化

Set these 5 string parameters:

名称描述文件
Domain您的Casdoor服务器的主机/域CasdoorVariables.cs
Clientid您的 Casdoor 应用程序的客户端 IDCasdoorVariables.cs
AppName您的Casdoor应用程序的名称CasdoorVariables.cs
CallbackURL您的Casdoor应用程序的回调URL路径。 如果未提供,它将是casdoor://callbackCasdoorVariables.cs
ClientSecret您的Casdoor应用程序的客户端密钥CasdoorVariables.cs

Defaults: Casdoor demo and app-casnode if not set.

运行中

Visual Studio

  1. 打开 casdoor-dotnet-desktop-example.sln
  2. Ctrl + F5 开始

Command line

  1. cd src/DesktopApp
  2. dotnet run

预览

index

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

登录 user profile 预览 gif

Integration

Open the login window

var login = new Login();
// 当登录成功时触发,您将在事件处理器中收到一个授权码
login.CodeReceived += Login_CodeReceived;
login.ShowDialog();

Exchange the auth code for 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);