Extension library for hashids.net.
Auto decode and encode hash ids for your api.
- Install package:
From PM console:
Install-Package Stio.Hashids.AspNetCore
or .net core cli:
dotnet add package Stio.Hashids.AspNetCore
- Register service:
builder.Services.AddHashids("this is my salt");- Decorate hash properties:
In your DTOs, decorate the properties that you want to be hash with HashidsJsonConverterAttribute
Only for System.Text.Json
public class SampleModel
{
[HashidsJsonConverter]
public long HashidsLong { get; set; }
[HashidsJsonConverter]
public long? NullableHashidsLong { get; set; }
public long NonHashidsLong { get; set; }
[HashidsJsonConverter]
public int HashidsInt { get; set; }
[HashidsJsonConverter]
public int? HashidsNullableInt { get; set; }
public int NonHashidsInt { get; set; }
[HashidsJsonConverter]
public long[] HashidsLongArray { get; set; } = null!;
[HashidsJsonConverter]
public long?[] HashidsNullableLongArray { get; set; } = null!;
[HashidsJsonConverter]
public long?[]? HashidsNullableLongNullableArray { get; set; } = null!;
public long[] NonHashidsLongArray { get; set; } = null!;
[HashidsJsonConverter]
public List<long> HashidsLongList { get; set; } = null!;
[HashidsJsonConverter]
public ICollection<long> HashidsLongCollection { get; set; } = null!;
[HashidsJsonConverter]
public ISet<long> HashidsLongSet { get; set; } = null!;
}Use decorated model in your api for request or response
[HttpPost]
public ActionResult<SampleModel> DecodeFromBody(SampleModel request)
{
return this.Ok(request);
}For hash the parameters of a route, query or form-data use the HashidsBinderAttribute
[HttpGet("decode/{id}")]
public IActionResult DecodeFromRoute([HashidsBinder] long id)
{
return this.Ok(id);
}
[HttpGet("decode")]
public IActionResult DecodeFromQuery([HashidsBinder] long id)
{
return this.Ok(id);
}
[HttpPost("decode")]
public IActionResult DecodeFromForm([FromForm, HashidsBinder] long id)
{
return this.Ok(id);
}See full example.