One of the features that sets Stormpath apart from other hosted identity management providers is our Custom Data feature, which allows you to store up to 10 megabytes of unstructured data (read: JSON) alongside any Stormpath resource. Stormpath clients use custom data to store any manner of application-specific user data, from custom profile fields to authorization roles, or even references to external data.
Stormpath’s Custom Data feature indexes every data element you store on an Account so that searching the data is blazingly fast. Our customers value speed, so we deployed a custom-built microservice architecture that handles this complex task with a typical processing time under 50 milliseconds, even under load.
Learn more about Custom Data Search with Stormpath in our upcoming webinar!
Custom Data in .NET
The Stormpath .NET SDK provides rich support for the Stormpath REST API in .NET, and Custom Data is no exception. In this post, I’ll show you how to use the .NET SDK to store and search user profile data with a few simple lines of code!
First, the basics: follow the C# quickstart to get a sample application connected to Stormpath. The examples in this article are in C#, but the SDK works fine with Visual Basic, too!
Storing and Updating Custom Data
When you create (or update) an Account, you can use the CustomData
property to add any data you want to the Account:
1 2 3 4 5 6 7 8 9 |
var picard = client.Instantiate<IAccount>() .SetUsername("jlpicard") .SetGivenName("Jean-Luc") .SetSurname("Picard") .SetPassword("uGhd%a8Kl!"); picard.CustomData["currentAssignment"] = "USS Enterprise (NCC-1701-D)"; await picard.SaveAsync(); |
If you want to remove some previously-stored data, you can use the Remove
method:
1 2 3 |
picard.CustomData.Remove(“currentAssignment”); await picard.SaveAsync(); |
When you use SaveAsync
to persist a new or changed Account, any changes or additions to the Account’s Custom Data are saved as well.
Retrieving Custom Data
Getting the Custom Data for an Account is easy. If you’ve already retrieved the Account, use the GetCustomDataAsync
method to grab the Custom Data:
1 2 3 |
var customData = await picard.GetCustomDataAsync(); Console.WriteLine(customData[“currentAssignment”]); |
Custom Data Search
In order to perform queries, you’ll need to know your Stormpath Directory href. You can find this by logging into the Stormpath Admin Console and finding the Application you created in the Quickstart:
Click on Account Stores on the left side and open up the Directory that was created for the Application. The href is displayed on the page:
In your code, you can retrieve the Directory resource using the .NET SDK:
1 2 |
var directory = await client.GetDirectoryAsync(/*your_href_here*/); |
Now that you have a reference to the Directory, you can create queries that search the Accounts in that Directory. As a refresher, you can query against any of the built-in Account fields with LINQ-to-Stormpath:
1 2 3 4 5 |
var peopleNamedJ = await directory .GetAccounts() .Where(acct => acct.GivenName.StartsWith(“J”)) .ToListAsync(); |
Searching against a value saved in Custom Data is similarly easy:
1 2 3 4 5 |
var enterpriseCrew = await directory .GetAccounts() .Where(acct => (string)acct.CustomData[“currentAssignment”].Contains(“enterprise”)) .ToListAsync(); |
Note that string matching is case-insensitive in the Stormpath API, so the case of the search term doesn’t matter.
There’s a lot more you can do with Custom Data in Stormpath! We have documentation available in our Product Guide.
What about my ASP.NET applications?
While these examples focused on the Stormpath .NET SDK, the same objects and features are available in the official Stormpath ASP.NET integration packages!
If you’re using ASP.NET 4.5+, you can use Request.GetStormpathClient()
or Request.GetStormpathAccount()
to access the relevant Stormpath SDK objects from inside a controller. There are anumber of examples to get you started in the ASP.NET documentation.
If you’re building applications with ASP.NET Core, it’s even easier. Simply use constructor injection to request an IClient
or IAccount
in the controller, and you’re in business! Check out the ASP.NET Core documentation to learn more.
Questions? Leave a comment here, or hit me up on Twitter @nbarbettini.
Viva .NET!