メインコンテンツにスキップ

.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://callbackになりますCasdoorVariables.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();
// Triggered when login succeeds, you will receive an auth code in the event handler
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);