• Reference
  • API
  • .NET

DotNet Client API

💡

Before going through this guide, make sure you follow the Oso Cloud Quickstart to get your Oso Cloud API Key properly set in your environment.

First, install the OsoCloud package:

dotnet add package OsoCloud

Instantiating an Oso Cloud client

The Oso Cloud client provides an Oso class

using OsoCloud;
 
var oso = new Oso("https://cloud.osohq.com", YOUR_API_KEY);
 
// Later:
await oso.Tell("has_role", new List<Value> { user, role, resource});
 
// Wherever authorization needs to be performed:
var allowed = await oso.Authorize(user, action, resource);
if (allowed) {
	// Action is allowed.
}

Passing application entities into the client

Under the hood, Oso Cloud represents an entity in your application as a combination of a type and an ID, which together uniquely identify the entity. The DotNet client represents these entities as oso.Value objects with both Type and Id properties. For example:

var alice = new oso.Value("User", "alice");
anvilsRepository = new oso.Value("Repository", "anvils")

You will pass objects like these into nearly every function call you make to the DotNet client.

Management API

Add fact: oso.Tell(name, args)

Adds a fact named name with the provided arguments. Example:

oso.Tell(
  "has_role",
  new List<Value> {
    new oso.Value("User", "bob"),
    new oso.Value("String", "owner"),
    new oso.Value("Organization", "acme")
  }
)

Add many facts: oso.BulkTell(facts)

ℹ️

For Oso Cloud sandbox accounts, BulkTell, BulkDelete, and Bulk calls are limited to 20 facts. If you attempt to send more than 20 facts, the CLI will return an error.

Adds many facts at once. Example:

oso.BulkTell(new List<Fact> {
  new oso.Fact{
    "has_role",
    new List<Value> {
      new oso.Value("User", "bob"),
      new oso.Value("String", "owner"),
      new oso.Value("Organization", "acme")
    },
  },
  new oso.Fact{
    "has_role",
    new List<Value> {
      new oso.Value("User", "bob"),
      new oso.Value("String", "maintainer"),
      new oso.Value("Repository", "anvil")
    },
  },
})

Delete fact: oso.Delete(name, args)

Deletes a fact. Does not throw an error if the fact is not found. Example:

oso.Delete(
  "has_role",
  new List<Value> {
    new oso.Value("User", "bob"),
    new oso.Value("String", "maintainer"),
    new oso.Value("Repository", "anvil")
  }
)

Delete many facts: oso.BulkDelete(facts)

Deletes many facts at once. Does not throw an error when some of the facts are not found. Example:

oso.BulkDelete(new List<Fact> {
  new oso.Fact{
    "has_role",
    new List<Value> {
      new oso.Value("User", "bob"),
      new oso.Value("String", "owner"),
      new oso.Value("Organization", "acme")
    },
  },
  new oso.Fact{
    "has_role",
    new List<Value> {
      new oso.Value("User", "bob"),
      new oso.Value("String", "maintainer"),
      new oso.Value("Repository", "anvil")
    },
  },
})

Transactionally delete and add facts: oso.Bulk(delete, tell)

Deletes and adds many facts in one atomic transaction. The deletions are performed before the adds. Does not throw an error when the facts to delete are not found. Example:

oso.Bulk(new List<Fact> {
    new oso.Fact{
        "has_role",
        new List<Value> {
            new oso.Value("User", "bob"),
            new oso.Value("String", "viewer"),
            new oso.Value("Repository", "anvil")
        },
    },
  },
  new List<Fact> {
   new oso.Fact{
        "has_role",
        new List<Value> {
            new oso.Value("User", "bob"),
            new oso.Value("String", "maintainer"),
            new oso.Value("Repository", "anvil")
        },
    },
  }
})

List facts: oso.Get(name, args)

ℹ️

For Oso Cloud sandbox accounts, Get calls are limited to 1000 results. If you have more than 1000 facts, the function will return an error.

Lists facts that are stored in Oso Cloud. Can be used to check the existence of a particular fact, or used to fetch all facts that have a particular argument:

// Get one fact:
oso.Get(
  "has_role",
  new List<Value> {
        new oso.Value("User", "bob"),
        new oso.Value("String", "admin"),
        new oso.Value("Repository", "anvil")
  }
)
// => List<oso.Fact> { oso.Fact{
//   "has_role",
//     List<oso.Fact> {
//       oso.Value("User", "bob"),,
//       oso.Value("String", "admin"),
//       oso.Value("Repository", "anvil")
//     }
//   }
//  }
 
// List all roles on the `anvils` repo
oso.Get(
  "has_role",
  new List<Value> {
        new oso.Value("User", null),
        new oso.Value("String", null),
        new oso.Value("Repository", "anvils")
  }
)
// => List<oso.Fact> { oso.Fact{
//   "has_role",
//     List<oso.Fact> {
//       oso.Value("User", "bob"),,
//       oso.Value("String", "admin"),
//       oso.Value("Repository", "anvil")
//     }
//   }
//  }

Note that null behaves like a wildcard: passing null, null, anvils means "find all facts where anvils is the third argument, regardless of other arguments".

Query for anything: oso.Query(rule)

Query Oso Cloud for any predicate and any combination of concrete and wildcard arguments. Unlike oso.Get, which only lists facts you've added, you can use oso.query to list derived information about any rule in your policy. Example:

// Query for all the repos `User:bob` can `read`
oso.Query(
  "allow",
  new List<Value> {
    new oso.Value("User", "bob"),
    new oso.Value("String", "read"),
    new oso.Value("Repository", null)
  }
)
// => List<oso.Fact> {
//    oso.Fact{
//      "allow",
//      List<oso.Fact> {
//        oso.Value("User", "bob"),,
//        oso.Value("String", "read"),
//        oso.Value("Repository", "acme")
//      }
//    },
//    oso.Fact{
//    "allow",
//    List<oso.Fact> {
//      oso.Value("User", "bob"),,
//      oso.Value("String", "read"),
//      oso.Value("Repository", "anvils")
//    }
//   },
//  }
 
// Query for all the objects `User:admin` can `read`
oso.Query(
  "allow",
  new List<Value> {
    new oso.Value("User", "admin"),
    new oso.Value("String", "read"),
    new oso.Value(null, null)
  }
)
// => List<oso.Fact> {
    //   // `User:admin` can `read` anything
//    oso.Fact{
//      "allow",
//      List<oso.Fact> {
//        oso.Value("User", "admin"),,
//        oso.Value("String", "read"),
//        oso.Value(null, null)
//      }
//    }
// }

Note that null behaves like a wildcard. Passing "allow", null, null, anvils means "find anyone who can do anything to anvils". null also behaves like a wildcard in return values from oso.Query. Additionally, if you want to query over all instances of a particular type, pass an oso.Value with a Type but no Id. For example, "allow", bob, "read", oso.Value("Repository", null) will query for all the objects of type "Repository" that bob can read.

Learn more about how to query Oso Cloud.

Check API

ℹ️

For Oso Cloud sandbox accounts,

  • the number of context facts per request is limited to 20; and
  • the number of records returned is limited to 1000.

Context facts

Each Check API method has a variant to which you may provide a list of context facts. When Oso Cloud performs a check, it will consider these context facts in addition to any other facts you've previously added. Context facts are only used in the API call in which they're provided-- they do not persist across requests. Learn more about context facts.

Check a permission: oso.Authorize(actor, action, resource)

Determines whether or not an action is allowed, based on a combination of authorization data and policy logic. Example:

var allowed = oso.Authorize(user, "read", anvilsRepository)
if (allowed) {
	// Action is allowed.
}

You may provide a slice of context facts. Example:

var allowed = oso.Authorize(user, "read", anvilsRepository, new List<Fact> {
    new Fact("has_relation", new List<Value> { 
        issueOnAnvilsRepository,
        new oso.Value("String", "parent"),
        anvilsRepository
    })
})

Check authorized resources: oso.AuthorizeResources(actor, action, resources)

Returns a subset of resources on which an actor can perform a particular action. Ordering and duplicates, if any exist, are preserved.

ℹ️

For Oso Cloud sandbox accounts, the number of input resources is limited to 1000.

Example:

var results = oso.AuthorizeResources(user, "read", new List<Value> {anvilsRepository, acmeRepository})

You may provide a slice of context facts. Example:

var results = oso.AuthorizeResources(user, "read", new List<Value> {anvilsRepository, acmeRepository}, new List<Fact> {
    new Fact("has_relation", new List<Value> {
        issueOnAnvilsRepository, new oso.Value("String", "parent"), anvilsRepository
    }),
    new Fact("has_relation", new List<Value> {
        issueOnAcmeRepository, new oso.Value("String", "parent"), acmeRepository
    })
})

List authorized resources: osoClient.List(actor, action, resourceType)

Fetches a list of resource IDs on which an actor can perform a particular action. Example:

var repositoryIds = oso.List(user, "read", "Repository")

List authorized actions: oso.Actions(actor, resource)

Fetches a list of actions which an actor can perform on a particular resource. Example:

var actions = oso.Actions(user, anvilsRepository)

Both List and Actions can also take Context Facts.

Policy API

Update the active policy: oso.Policy(policy)

Updates the policy in Oso Cloud. The string passed into this method should be written in Polar. Example:

oso.Policy("actor User {}")

Talk to an Oso Engineer

Our team is happy to help you get started with Oso Cloud. If you'd like to learn more about using Oso Cloud in your app or have any questions about this guide, schedule a 1x1 with an Oso engineer.

Get started with Oso Cloud →