Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
ADFS custom attribute store with multiple values
using Microsoft.IdentityServer.ClaimsPolicy.Engine.AttributeStore;
using System;
using System.Collections.Generic;
using System.IdentityModel;
namespace CAStore
{
public class CAS : IAttributeStore
{
public IAsyncResult BeginExecuteQuery(string query, string[] parameters, AsyncCallback callback, object state)
{
// c:[Type == "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/upn"]
// => issue(store = "CAS", types = ("http://schemas.myorganization.com/identity/uppercaseupn"), query = "toUpper", param = c.Value);
// query = "toUpper"
// parameters [0] = UPN = joe.bloggs@domain.com
if (String.IsNullOrEmpty(query))
{
throw new AttributeStoreQueryFormatException("No query string.");
}
if (parameters == null)
{
throw new AttributeStoreQueryFormatException("No query parameter.");
}
if (parameters.Length != 1)
{
throw new AttributeStoreQueryFormatException("More than one query parameter.");
}
string inputString = parameters[0];
if (inputString == null)
{
throw new AttributeStoreQueryFormatException("Query parameter cannot be null.");
}
if (!query.Equals("House"))
throw new AttributeStoreQueryFormatException("The query string " + query + " is not supported.");
// string[][] outputValues
// C# jagged array.
// There is only one query viz. "HouseID". So each row can only have 1 column. If there were 3 queries e.g. "GivenName, sn, HouseID", then each row would have 3 columns.
// The search has multiple results so we have many rows; each with one column.
try
{
// Dummy value to illustrate the principle.
List<string> claimValues = new List<string>();
claimValues.Add("123456");
claimValues.Add("654321");
claimValues.Add("456123");
List<string[]> claimData = new List<string[]>();
// Each claim value is added to its own string array
foreach (string claimVal in claimValues)
{
claimData.Add(new string[1] { claimVal });
}
// The claim value string arrays are added to the string [][] that is returned by the Custom Attribute Store EndExecuteQuery()
string[][] resultData = claimData.ToArray();
TypedAsyncResult<string[][]> asyncResult = new TypedAsyncResult<string[][]>(callback, state);
asyncResult.Complete(resultData, true);
return asyncResult;
}
catch (Exception ex)
{
String innerMess = "";
if (ex.InnerException != null)
innerMess = ex.InnerException.ToString();
throw new AttributeStoreQueryExecutionException("CAS exception : " + ex.Message + " " + innerMess);
}
}
public string[][] EndExecuteQuery(IAsyncResult result)
{
return TypedAsyncResult<string[][]>.End(result);
}
public void Initialize(Dictionary<string, string> config)
{
// No initialization is required for this store.
}
}
}