Skip to content

Commit

Permalink
docs: improve README
Browse files Browse the repository at this point in the history
  • Loading branch information
hsluoyz committed Dec 1, 2023
1 parent 32a9c83 commit 1cd7f3d
Showing 1 changed file with 31 additions and 16 deletions.
47 changes: 31 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Casdoor .NET SDK

[![Actions Status](https://github.com/casdoor/casdoor-dotnet-sdk/workflows/Build/badge.svg)](https://github.com/casdoor/casdoor-dotnet-sdk/actions)
[![GitHub](https://img.shields.io/github/license/casdoor/casdoor-dotnet-sdk)](https://github.com/casdoor/casdoor-dotnet-sdk/blob/master/LICENSE)

Expand All @@ -7,27 +8,32 @@ The Casdoor's SDK for .NET/ASP.NET Core, which will allow you to easily connect
**Note:** This SDK is currently in beta and is not yet ready for use.

## Packages

This SDK is built using the following packages for different platforms:

| Package Name | Develop (MyGet) | Release (NuGet) | Description | Supported frameworks |
|---------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------|----------------------|----------------------------------------|
| Package Name | Develop (MyGet) | Release (NuGet) | Description | Supported frameworks |
|----------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------------------|----------------------|----------------------------------------|
| `Casdoor.Client` | [![MyGet](https://img.shields.io/casdoor.myget/casdoor/v/Casdoor.Client?label=Version)](https://www.myget.org/feed/casdoor/package/nuget/Casdoor.Client) | [![NuGet](https://img.shields.io/nuget/vpre/Casdoor.Client)](https://www.nuget.org/packages/Casdoor.Client) | SDK for .NET | .NET Standard 2.0/.NET 4.6.1 and newer |
| `Casdoor.AspNetCore` | [![MyGet](https://img.shields.io/casdoor.myget/casdoor/v/Casdoor.AspNetCore?label=Version)](https://www.myget.org/feed/casdoor/package/nuget/Casdoor.AspNetCore) | [![NuGet](https://img.shields.io/nuget/vpre/Casdoor.AspNetCore)](https://www.nuget.org/packages/Casdoor.AspNetCore) | SDK for ASP.NET Core | .NET Core 3.1 and newer |
| `Casdoor.Native` | `wait develop` | `wait publish` | SDK for WPF or Maui | - |
| `Casdoor.Native` | `wait develop` | `wait publish` | SDK for WPF or Maui | - |

## Casdoor Client

Casdoor.Client is a base package for the specific platform SDKs. It contains follow features.

- CasdoorClient: A API client implementation for the Casdoor. You can use to call the Casdoor APIs.

##### Usage

```cs
var httpClient = new HttpClient();
var client = new CasdoorClient(HttpClient, new CasdoorOptions{
Endpoint = "https://door.casdoor.com",
OrganizationName = "build-in",
ApplicationName = "app-build-in",
OrganizationName = "casbin", // your Casdoor organization
ApplicationName = "app-example", // your Casdoor application
ApplicationType = "native", // webapp, webapi or native
ClientId = "<your client id>",
ClientSecret = "<your client secret>",
ClientId = "b800a86702dd4d29ec4d", // your Casdoor application's client ID
ClientSecret = "1219843a8db4695155699be3a67f10796f2ec1d5", // your Casdoor application's client secret
});

// Request tokens (You should enable credentials flow in your Casdoor application)
Expand All @@ -38,7 +44,7 @@ var currentUser = client.ParseJwtToken(token.AccessToken);

// Request user info
var users = await client.GetUsersAsync();
// Request roles
// Request roles
var roles = await client.GetRolesAsync();
// Request Permissions
var persmissions = await client.GetPermissionsAsync();
Expand All @@ -48,7 +54,7 @@ var policy = new CasdoorPermissionRule()
Id = $"{currentUser.Owner}/{persmissions.First().Name}",
V0 = $"{currentUser.Owner}/{currentUser.Name}",
V1 = "example-resource",
V2 = "example-action"
V2 = "example-action"
};

var isPermissionAvaliable = await client.EnforceAsync(policy);
Expand All @@ -58,17 +64,20 @@ var isPermissionAvaliable = await client.EnforceAsync(policy);
- Extensions (Updating)

## Casdoor AspNetCore

### Getting started

1. Add the following settings about casdoor in your appsettings.json file:

```json5
{
"Casdoor": {
"Endpoint": "<your casdoor host>",
"OrganizationName": "<your organization name>",
"ApplicationName": "<your application name>",
"Endpoint": "<your Casdoor endpoint>",
"OrganizationName": "<your Casdoor organization>",
"ApplicationName": "<your Casdoor application>",
"ApplicationType": "webapp", // webapp or webapi (webapi is not yet supported)
"ClientId": "<your client id>",
"ClientSecret": "<your client secret>",
"ClientId": "<your Casdoor application's client ID>",
"ClientSecret": "<your Casdoor application's client secret>",

// Optional: The callback path that the client will be redirected to
// after the user has authenticated. default is "/casdoor/signin-callback"
Expand All @@ -86,6 +95,7 @@ var isPermissionAvaliable = await client.EnforceAsync(policy);
```

2. Add the following code to your webapp:

```csharp
builder.Services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
.AddCasdoor(builder.Configuration.GetSection("Casdoor"))
Expand All @@ -95,13 +105,18 @@ builder.Services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationSc
3. Now you can use the Casdoor authentication scheme in your webapp!

### Samples
#### 1. [Mvc sample](https://github.com/casdoor/casdoor-dotnet-sdk/tree/master/samples/MvcApp)
It is a mvc webapp that uses Casdoor authentication.

#### 1. [MVC sample](https://github.com/casdoor/casdoor-dotnet-sdk/tree/master/samples/MvcApp)

It is a MVC webapp that uses Casdoor authentication.
The default settings use the public demo Casdoor and Casnode configuration, you can directly start the webapp by running:

```bash
dotnet run
```

Or change the settings in the appsettings.json according to your deployed casdoor configuration.

## License

This project is licensed under the [Apache 2.0 license](LICENSE).

0 comments on commit 1cd7f3d

Please sign in to comment.