The .NET SDK targets .NET 8 or later and has no dependencies. Package: Speedle.Sdk 0.1.0 on NuGet, namespace Speedle.Sdk. Reference: speedle-sdk/dotnet.
The examples check the policy described in SDKs.
Add the dependency
In a new console project:
dotnet new console -o SpeedleExample
cd SpeedleExample
dotnet add package Speedle.Sdk --version 0.1.0Call a local Speedle
Start Speedle and create the example policy as described in SDKs. Replace Program.cs with:
using Speedle.Sdk;
try
{
await using var speedle = new SpeedleClient(new SpeedleClientOptions
{
Backend = Backend.SpeedlePlus,
Endpoint = new Uri("http://localhost:6734"),
Auth = Auth.None(),
ServiceName = "docs",
});
bool allowed = await speedle.IsAllowedAsync("alice", "get", "/docs/public");
Console.WriteLine($"alice get /docs/public: allowed={allowed}");
Decision d = await speedle.CheckAsync("alice", "delete", "/docs/public");
Console.WriteLine($"alice delete /docs/public: allowed={d.Allowed} reason={d.Reason}");
}
catch (SpeedleException e)
{
Console.Error.WriteLine($"Speedle error: {e.Message}");
return 1;
}
return 0;Run it:
dotnet runOutput:
alice get /docs/public: allowed=True
alice delete /docs/public: allowed=False reason=NoApplicablePoliciesCall Speedle Cloud
Speedle Cloud is coming soon.
The program reads the service name from SPEEDLE_SERVICE and the service’s token from SPEEDLE_TOKEN. SPEEDLE_ENDPOINT overrides the default endpoint https://cloud.speedle.io. Replace Program.cs with:
using Speedle.Sdk;
try
{
await using var speedle = new SpeedleClient(new SpeedleClientOptions
{
Backend = Backend.SpeedleCloud,
Endpoint = new Uri(Environment.GetEnvironmentVariable("SPEEDLE_ENDPOINT") ?? "https://cloud.speedle.io"),
Auth = Auth.Bearer(Environment.GetEnvironmentVariable("SPEEDLE_TOKEN") ?? ""),
ServiceName = Environment.GetEnvironmentVariable("SPEEDLE_SERVICE") ?? "",
});
bool allowed = await speedle.IsAllowedAsync("alice", "get", "/docs/public");
Console.WriteLine($"alice get /docs/public: allowed={allowed}");
Decision d = await speedle.CheckAsync("alice", "delete", "/docs/public");
Console.WriteLine($"alice delete /docs/public: allowed={d.Allowed} reason={d.Reason}");
}
catch (SpeedleException e)
{
Console.Error.WriteLine($"Speedle error: {e.Message}");
return 1;
}
return 0;Set the environment and run it. read -rs keeps the token out of your shell history and screen:
export SPEEDLE_SERVICE=docs
read -rs SPEEDLE_TOKEN && export SPEEDLE_TOKEN
dotnet runOutput, when the service has the example policy:
alice get /docs/public: allowed=True
alice delete /docs/public: allowed=False reason=NoApplicablePoliciesThe token is a server-side secret. Never ship it to browsers, mobile apps or desktop clients.