Registration and secrets
Register a new free account at Cloudinary.
Go into the Dashboard and look for
- Cloud name
- API Key
- API Secret
Open appsettings.json and add the template for the keys
"CloudinarySettings" : {
"CloudName": "Set the key in secrets. View README for more info.",
"ApiKey": "Set the key in secrets. View README for more info.",
"ApiSecret": "Set the key in secrets. View README for more info."
}
Using the console create the three key in secret storage.
dotnet user-secrets set "CloudinarySettings:CloudName" "******"
dotnet user-secrets set "CloudinarySettings:ApiKey" "******"
dotnet user-secrets set "CloudinarySettings:ApiSecret" "******"
If the ApiSecret starts with -
or --
there is a bug in the command line that will result in a message:
Unrecognized option 'your ApiSecret value'
Workaround: create a file cloudinarysettings.json containing your settings.
{
"CloudinarySettings": {
"CloudName": "******",
"ApiKey": "******",
"ApiSecret": "******"
}
}
Pipe the configuration file in secret storage
cat ./cloudinarysettings.json | dotnet user-secrets set
Remember to put this file in gitignore or in a directory not in source control.
Helper class
Create a new class Helpers/CloudinarySettings.cs with properties for the Cloudinary keys.
public class CloudinarySettings
{
public string CloudName { get; set; }
public string ApiKey { get; set; }
public string ApiSecret { get; set; }
}
Open Startup.cs and add to ConfigureServices the configuration for the settings helper so that it is available for injection.
...
services.AddCors();
services.Configure<CloudinarySettings>(Configuration.GetSection("CloudinarySettings"));
services.AddAutoMapper();
...
Change the Photo class
When we upload a photo to Cloudinary the documentation says that the call returns a JSON object. Inside this object we have a key public_id. We need to store this key inside our photo object.
Open Photo.cs and add a prop for public_id.
public class Photo
{
...
public string PublicId { get; set; }
}
Add a new migration.
dotnet ef migrations add AddedPhotoId
Check the migration code and if everithing is ok apply it to the DB.
dotnet ef database update
Cloudinary package
CTRL+SHIFT+p Nuget Add Package
Search CloudinaryDotNet and add the latest version (at the time of writing it was 1.3.1).
Add a package for Newtonsoft.Json and add the latest version (11.0.2).
Restore to install the new packages.
dotnet restore
Now run the application to check that it builds correctly.