Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
105 changes: 104 additions & 1 deletion Examples.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# Examples using auth0.net

# Authentication
- [Authentication API](#authentication-api)
- [Management API](#management-api)

# Authentication API
- [1. Client Initialization](#1-client-initialization)
- [2. Login With Client Credentials](#2-login-with-client-credentials)
- [3. Authenticate using Resource Owner Password Grant Flow with MFA](#3-authenticate-using-resource-owner-password-grant-flow-with-mfa)
Expand Down Expand Up @@ -170,3 +173,103 @@ await authClient.DeleteMfaAuthenticatorAsync(
```
⬆️ [Go to Top](#)

# Management API

## 1. Client Initialization

To initialize the Management API client, you also need the Authentication API client to get the access token required by the Management API client constructor.

```csharp
public async Task Initialize()
{
var authClient = new AuthenticationApiClient("my.custom.domain");

// Fetch the access token using the Client Credentials.
var accessTokenResponse = await authClient.GetTokenAsync(new ClientCredentialsTokenRequest()
{
Audience = "audience",
ClientId = "clientId",
ClientSecret = "clientSecret",
});

managementClient = new ManagementApiClient(accessTokenResponse.AccessToken, "my.custom.domain");
}
```

⬆️ [Go to Top](#)

## 2. Client Initialization as a Singleton in ASP.NET Core

To configure the Management API client as a Singleton in the context of an ASP.NET Core application, you should:

1. Define an interface with a client builder method.
2. Define a class implementing that interface.
3. Register the class instance as a singleton service.

Define an interface with a client builder method.

```csharp
using Auth0.ManagementApi;

interface IAuth0Management
{
Task<ManagementApiClient> getClient();
}
```

Define a class implementing that interface.

```csharp
using Auth0.ManagementApi;
using Auth0.AuthenticationApi;
using Auth0.AuthenticationApi.Models;

public class Auth0Management: IAuth0Management
{
private string _domain = "";
private string _clientId = "";
private string _clientSecret = "";
private ManagementApiClient managementClient = null;

public Auth0Management(string domain, string clientId, string clientSecret)
{
_domain = domain;
_clientId = clientId;
_clientSecret = clientSecret;
}

public async Task<ManagementApiClient> getClient()
{
if (managementClient is null)
{
var authClient = new AuthenticationApiClient(_domain);
var accessTokenResponse = await authClient.GetTokenAsync(new ClientCredentialsTokenRequest
{
Audience = $"https://{_domain}/api/v2/",
ClientId = _clientId,
ClientSecret = _clientSecret
});
managementClient = new ManagementApiClient(accessTokenResponse.AccessToken, _domain);
}
return managementClient;
}
}
```

> This class implementation does not take into account refreshing the access token.

Register the class instance as a singleton service in `Program.cs`.

```csharp
...

builder.Services.AddSingleton<IAuth0Management>(sp =>
new Auth0Management(
builder.Configuration["Auth0:Domain"],
builder.Configuration["Auth0:ManagementClientId"],
builder.Configuration["Auth0:ManagementClientSecret"]
)
);

var app = builder.Build();
```
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
## Documentation

- [Docs site](https://www.auth0.com/docs) - explore our docs site and learn more about Auth0.
- [Examples](Examples.md) - code samples for common scenarios.

## Getting started

Expand Down Expand Up @@ -38,6 +39,8 @@ The API calls are divided into groups which correlate to the [Management API doc
await client.Connections.GetAllAsync("auth0");
```

See [more examples](Examples.md#management-api).

### Authentication API

#### Installation
Expand All @@ -60,6 +63,8 @@ This library contains [URL Builders](https://auth0.github.io/auth0.net/#using-ur

**Important note on state validation**: If you choose to use the [AuthorizationUrlBuilder](https://auth0.github.io/auth0.net/api/Auth0.AuthenticationApi.Builders.AuthorizationUrlBuilder.html) to construct the authorization URL and implement a login flow callback yourself, it is important to generate and store a state value (using [WithState](https://auth0.github.io/auth0.net/api/Auth0.AuthenticationApi.Builders.AuthorizationUrlBuilder.html#Auth0_AuthenticationApi_Builders_AuthorizationUrlBuilder_WithState_System_String_)) and validate it in your callback URL before exchanging the authorization code for the tokens.

See [more examples](Examples.md#authentication-api).

## Feedback

### Contributing
Expand Down Expand Up @@ -92,3 +97,4 @@ Please do not report security vulnerabilities on the public GitHub issue tracker
<p align="center">Auth0 is an easy to implement, adaptable authentication and authorization platform. To learn more checkout <a href="https://auth0.com/why-auth0">Why Auth0?</a></p>
<p align="center">
This project is licensed under the MIT license. See the <a href="./LICENSE"> LICENSE</a> file for more info.</p>

Loading