SafeguardDotNet is a .NET library written in C#. It is available from NuGet.org which makes it really easy to use C# to call the SPP API.
This lab will require Visual Studio Code which is a free code editor that every developer or IT professional should use. It is extremely lightweight, and it has broad support for many technologies, not just C# and .NET.
To download and install Visual Studio Code, visit the webpage and follow the instructions.
And you'll need a C# extension:
Once you have Visual Studio Code installed, it is very easy to create your first project that calls the SPP API.
First, create a directory with the name you want to give your project and change directory into it.
Run:
PS> mkdir MyTestProject
PS> cd MyTestProject
Run:
PS> dotnet new console
This will create a console
project. You can see other project types by
running dotnet new
.
Run:
PS> dotnet add package OneIdentity.SafeguardDotNet
This will add the latest OneIdentity.SafeguardDotNet NuGet package into your project.
If you encounter the this error:
Add this file to your project: nuget.config
Run:
PS> dotnet add package OneIdentity.SafeguardDotNet
Run:
PS> dotnet restore
This will restore NuGet packages into your project so you can get code completion in the editor
Finally, run:
PS> code .
This will open the Visual Studio Code editor so you can begin adding code to your project.
Add the using directive at the top of your Program.cs
file to call
SafeguardDotNet:
using OneIdentity.SafeguardDotNet;
Find the line in the file that says Console.WriteLine("Hello World!");
and
replace it with the following:
var password = "<your password>".ToSecureString();
var connection = Safeguard.Connect("<your server>", "local", "<your user>", password, 3, true);
Console.WriteLine(connection.InvokeMethod(Service.Core, Method.Get, "Me"));
You will need to replace the fields in angle brackets with the values for authenticating one of your users.
Your source code editor should look similar to the following:
Go back to the command prompt and run the project by running the following:
PS> dotnet run
In your terminal you will see JSON output describing the user that you authenticated as.
This project is just a simple demonstration, but it could be expanded to do whatever you want it to do.
SafeguardDotNet includes a number of key entry points:
- Use the
Safeguard
static class to contruct aSafeguardConnection
- Use the
Safeguard.Event
static class to construct aPersistentSafeguardListener
- Use the
Safeguard.A2A
static class to construct aSafeguardA2AContext
- Use the
Safeguard.A2A
static class to construct aPersistentSafeguardA2AListener
When using event listeners, you almost always want to use the persistent versions, because they will reconnect if something goes wrong or if the access token expires.
For more information, see the code in the sample projects here.