An open API service indexing awesome lists of open source software.

https://github.com/casdoor-net/casdoor-dotnet-maui-example

.NET MAUI app and .NET MAUI library example for Casdoor
https://github.com/casdoor-net/casdoor-dotnet-maui-example

auth authentication authn cas casbin casdoor csharp dotnet example identity maui oauth oauth2 oidc saml sdk

Last synced: 3 months ago
JSON representation

.NET MAUI app and .NET MAUI library example for Casdoor

Awesome Lists containing this project

README

          

# casdoor-dotnet-maui-example
The repository contain .NET MAUI app and .NET MAUI library for demonstration [Casdoor](https://casdoor.org/) authentication by Open ID Connect.

## Demonstration

### **Android**

### **Windows**

# Requirements

- [.NET 7 SDK](https://dotnet.microsoft.com/download/dotnet/7.0) installed on your machine
- The required assets needed for your target(s) platform(s) as described [here](https://docs.microsoft.com/en-us/dotnet/maui/get-started/first-app)
- Visual Studio 2022 for Windows 17.3 or Visual Studio 2022 for Mac 17.4 (optional)

## Getting started

### Step 1: Create MAUI Application

Create your [MAUI Application](https://docs.microsoft.com/en-us/dotnet/maui/get-started/first-app).

### Step 2: Add reference

Add a reference to the `Casdoor.MauiOidcClient` in your project.

### Step 3: Add Casdoor client

Add `CasdoorClient` as singleton in the services.

```csharp
builder.Services.AddSingleton(new CasdoorClient(new()
{
Domain = "exlens.ru",
ClientId = "660682724e779be2a6f2",
Scope = "openid profile email",

#if WINDOWS
RedirectUri = "http://localhost/callback"
#else
RedirectUri = "casdoor://callback"
#endif
}));
```

### Step 4: Design UI

Add code to `MainPage` file.

**MainPage.xaml**
```xml






```

**MainPage.cs**
```csharp
namespace Casdoor.MauiOidcClient.Example
{
public partial class MainPage : ContentPage
{
int count = 0;
private readonly CasdoorClient client;
private string acsessToken;
public MainPage(CasdoorClient client)
{
InitializeComponent();
this.client = client;

#if WINDOWS
client.Browser = new WebViewBrowserAuthenticator(WebViewInstance);
#endif
}

private void OnCounterClicked(object sender, EventArgs e)
{
count++;

if (count == 1)
CounterBtn.Text = $"Clicked {count} time";
else
CounterBtn.Text = $"Clicked {count} times";

SemanticScreenReader.Announce(CounterBtn.Text);
}

private async void OnLoginClicked(object sender, EventArgs e)
{
var loginResult = await client.LoginAsync();
acsessToken = loginResult.AccessToken;
if (!loginResult.IsError)
{
NameLabel.Text = loginResult.User.Identity.Name;
EmailLabel.Text = loginResult.User.Claims.FirstOrDefault(c => c.Type == "email")?.Value;

LoginView.IsVisible = false;
HomeView.IsVisible = true;
}
else
{
await DisplayAlert("Error", loginResult.ErrorDescription, "OK");
}
}

private async void OnLogoutClicked(object sender, EventArgs e)
{
var logoutResult = await client.LogoutAsync(acsessToken);

if (!logoutResult.IsError)
{
HomeView.IsVisible = false;
LoginView.IsVisible = true;
this.Focus();
}
else
{
await DisplayAlert("Error", logoutResult.ErrorDescription, "OK");
}
}
}
}
```

### Step 5: Support Android platform

Modify `AndroidManifest.xml` file.

```xml








```

### Step 6: Launch application

**Visual Studio:** Press Ctrl + F5 to start