mirror of
https://github.com/andwati/zitadel.git
synced 2026-10-27 01:32:15 +00:00
123 lines
6.6 KiB
Plaintext
123 lines
6.6 KiB
Plaintext
---
|
|
title: Integrate ZITADEL APIs into a .NET Application
|
|
description: "Fetch organization details using ZITADEL management API and .NET SDK with a service account and valid key JSON"
|
|
sidebar_label: Example .NET
|
|
---
|
|
|
|
This integration guide shows you how to integrate **ZITADEL** into your .NET application.
|
|
It demonstrates how to fetch some data from the ZITADEL management API.
|
|
|
|
At the end of the guide you should have an application able to read the details of your organization.
|
|
|
|
If you need any other information about the .NET SDK go to the [documentation](https://github.com/smartive/zitadel-net) of the SDK itself.
|
|
|
|
## Prerequisites
|
|
|
|
The client [SDK](https://github.com/zitadel/zitadel-net) will handle all necessary OAuth 2.0 requests and send the required headers to the ZITADEL API.
|
|
All that is required, is a service account with an Org Owner (or another role, depending on the needed api requests) role assigned and its key JSON.
|
|
|
|
However, we recommend you read the guide on [how to access ZITADEL API](/guides/integrate/zitadel-apis/access-zitadel-apis) and the associated guides for a basic knowledge of :
|
|
|
|
- [Recommended Authorization Flows](/guides/integrate/login/oidc/oauth-recommended-flows)
|
|
- [Service Accounts](/guides/integrate/service-accounts/authenticate-service-accounts)
|
|
|
|
> Be sure to have a valid key JSON and that its service account is either ORG_OWNER or at least ORG_OWNER_VIEWER before you continue with this guide.
|
|
|
|
## .NET Setup
|
|
|
|
### Create a .NET application
|
|
|
|
Use the IDE of your choice or the command line to create a new application.
|
|
|
|
```bash
|
|
dotnet new web
|
|
```
|
|
|
|
### Install the package
|
|
|
|
Install the package via nuget
|
|
|
|
```bash
|
|
dotnet add package Zitadel.Api
|
|
```
|
|
|
|
### Create example application
|
|
|
|
Change the program.cs file to the content below. This will create a application for the management api and call its `GetMyUsers` function.
|
|
The SDK will make sure you will have access to the API by retrieving a Bearer Token using JWT Profile with the provided scopes (`openid` and `urn:zitadel:iam:org:project:id:zitadel:aud`).
|
|
|
|
Make sure to fill the const `apiUrl`, and `personalAccessToken` with your own instance data. The used vars below are from a test instance, to show you how it should look.
|
|
The apiURL is the domain of your instance you can find it on the instance detail in the Customer Portal or in the Management Console
|
|
|
|
```csharp
|
|
// This file contains two examples:
|
|
// 1. An example with a service account "personal access token" to access the ZITADEL API.
|
|
// 2. An example with a service account "jwt profile key" to access the ZITADEL API.
|
|
|
|
using Zitadel.Api;
|
|
using Zitadel.Credentials;
|
|
|
|
const string apiUrl = "https://zitadel-libraries-l8boqa.zitadel.cloud";
|
|
const string personalAccessToken = "ge85fvmgTX4XAhjpF0XGpelB2vn9LZanJaqmUQDuf7iTpKVowb44LFl-86pqY2mfJCEoIOk";
|
|
|
|
// or create the token provider directly:
|
|
// new StaticTokenProvider(token)
|
|
var client = Clients.AuthService(new(apiUrl, ITokenProvider.Static(personalAccessToken)));
|
|
var result = await client.GetMyUserAsync(new());
|
|
Console.WriteLine($"User: {result.User}");
|
|
|
|
// This adds the urn:zitadel:iam:org:project:id:zitadel:aud scope to the authorization request, enabling access to ZITADEL APIs.
|
|
const string apiProject = "zitadel";
|
|
var serviceAccount = ServiceAccount.LoadFromJsonString(
|
|
@"
|
|
{
|
|
""type"": ""serviceaccount"",
|
|
""keyId"": ""170084658355110145"",
|
|
""key"": ""-----BEGIN RSA PRIVATE KEY-----\nMIIEpAIBAAKCAQEAnQisbU4FuLmjLR9I2Q01Rm9Mx6WySat2mbxgmOzu04oXuESI\nyS+RkiimdN0khjqouBftYqtVes7yngMLq3E8hMCwv/kLE+YeXphZXnn8tps8M2gV\n7S//uCp9LooK9qeh0lSkOqIsh0atj/l7NAHFxnhuNhfmn8XIYJNLVNSj5yzTri5E\nSn92SAsUQLSONgr7IEmIjcuPtYeU0iLvVno52ljZHnPX2WJ0HEZv44nZpkR4qBfv\n3hJzNx7sd4TdPGHHugJD8jdG/X4bAxwL5XGHZu18cUVM5RerSMpFQHSuIGgpKmK4\nWlM1AJGeut6EX/SrCxUDvhyOnXAgqhunTUmi6QIDAQABAoIBAHn7y92Y1y743X3m\nqHMbJIBTY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fPelclJUv/IVI59c0F9UMAA2rD1U\nNW6k9251OGU7mQkztluNvl13qtAW/DveOjkFeDJIMzhFjravpLQXhUK4ETnM44YL\nFbClVGJaHYSHgOkNpcN5lYVLoyEvzv9rEPwBqpZRVnwWj6L+/I2L5Q==\n-----END RSA PRIVATE KEY-----\n"",
|
|
""userId"": ""170079991923474689""
|
|
}");
|
|
client = Clients.AuthService(
|
|
new(
|
|
apiUrl,
|
|
ITokenProvider.ServiceAccount(
|
|
serviceAccount,
|
|
apiUrl,
|
|
apiProject)));
|
|
result = await client.GetMyUserAsync(new());
|
|
Console.WriteLine($"User: {result.User}");
|
|
```
|
|
|
|
### Test application
|
|
|
|
After you have configured everything correctly, you can simply start the example by:
|
|
|
|
```bash
|
|
dotnet run
|
|
```
|
|
|
|
This will output something similar to:
|
|
|
|
```
|
|
User: {"FirstName": "MyName", "LastName": "MyLastName" ... }
|
|
```
|
|
|
|
## Completion
|
|
|
|
You have successfully used the ZITADEL .NET SDK to call the auth API!
|
|
To use the auth API you will not need a specific role, because only an authenticated user is needed.
|
|
|
|
For accessing the admin or management API the user will need some specific roles.
|
|
If you encountered an error (e.g. `code = PermissionDenied desc = No matching permissions found`),
|
|
ensure your service account has the required permissions by assigning the `ORG_OWNER` or `ORG_OWNER_VIEWER` role
|
|
and check the mentioned [guides](#prerequisites) at the beginning.
|
|
|
|
If you've run into any other problem, don't hesitate to contact us or raise an issue on [ZITADEL](https://github.com/zitadel/zitadel/issues) or in the [SDK](https://github.com/zitadel/zitadel-go/issues).
|
|
|
|
### Whats next?
|
|
|
|
Now you can proceed implementing our APIs by adding more calls.
|
|
|
|
Checkout more [examples from the SDK](https://github.com/zitadel/zitadel-go/blob/main/example) or refer to our [API Docs](/apis/introduction).
|
|
|
|
> This guide will be updated soon to show you how to use the SDK for your own API as well.
|