Authentication from a .Net client

How to authenticate with a sensenet repository from a .Net client

Unless you are working with publicly available content - like blog posts - it is necessary to make authenticated calls to the sensenet repository to access content items. In this article you'll see how to achieve that in a .Net application.

 

Create a new console application

As a prerequisite, please visit the .Net console application tutorial to see how to create a console app that connects to a sensenet repository.

Add config file and load configuration

Add a new json file to your project called appsettings.json and set the Copy to output directory property to Copy if newer in its properties window. This will ensure that the config file will be present when you start the app.

If you followed the tutorial above, all your services are already registered. Your application will load the necessary services and the configuration from the sensenet:repository segment. The structure of the configuration should be the following:

{
"sensenet": {
"repository": {
"Url": "https://example.sensenet.cloud",
"Authentication": {
"ClientId": "abc",
"ClientSecret": "abcdefghijkl",
}
}
}
}

Or using an API key:

{
"sensenet": {
"repository": {
"Url": "https://example.sensenet.cloud",
"Authentication": {
"ApiKey": "abcdefghijk"
}
}
}
}

To be able to connect to a sensenet service, you will either have to fill the clientid/secret pair, or use an API key. See the links at the beginning of this article to see how can you acquire them.

Get the repository instance

If you configured the values above correctly, you will get an authenticated repository instance when using the API described in the console app getting started article:

var repositoryCollection = host.Services.GetRequiredService<IRepositoryCollection>();
var repository = await repositoryCollection.GetRepositoryAsync(CancellationToken.None);

You do not have to do anything else, the instance is already filled with the necessary tokens, you can start working with content items.

To learn more about the client API we offer for .Net developers and example requests you can make from a .Net Core client application, please visit the following articles: