Skip to content

Latest commit

 

History

History
72 lines (57 loc) · 1.42 KB

README.md

File metadata and controls

72 lines (57 loc) · 1.42 KB

Web Safe Token Service

Create time sensitive encrypted tokens that are safe to transmit via query string parameters.

appsettings.json

Key is a 32 char string

{
  "Encryption": {
    "Key": "DO_NOT_USE_THIS_DO_NOT_USE_THIS!" 
  }
}

Register Services

public class Startup
{
    private readonly IConfiguration Configuration;

    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddEncryptionServices(Configuration);
    }
}

Example

public class Example
{
    private const int ExpiryTimeInMins = 10;

    private readonly WebTokenService WebTokenService;

    public Example(WebTokenService webTokenService)
    {
        WebTokenService = webTokenService;
    }

    public void Demo()
    {
        var data = "123456789";

        // Token as a url safe string
        var token = WebTokenService.Encrypt(data);

        var url = $"http://example.com?safetoken={token}";

        // Use the url in emails etc
        // ...
        // ...
 
        var decrypted = WebTokenService.Decrypt(token);

        // Is it valid?
        const int ExpiryTimeInMins = 10;
        var valid = decrypted.IsValid(ExpiryTimeInMins);

        if (valid)
        {
            // Do something with the data...
            var userid = int.Parse(decrypted.Data);
        }
    }
}