Skip to content

Instantly share code, notes, and snippets.

@vmandic
Created May 17, 2019 12:13
Show Gist options
  • Save vmandic/16532399ec6dbc7cd47fb24eb37f10ec to your computer and use it in GitHub Desktop.
Save vmandic/16532399ec6dbc7cd47fb24eb37f10ec to your computer and use it in GitHub Desktop.
meds-processor, part/4, snippet #15
using System;
using System.Text;
using Microsoft.IdentityModel.Tokens;
namespace MedsProcessor.WebAPI.Infrastructure
{
public class AuthTokenOptions
{
public string Secret { get; set; }
public string Issuer { get; set; }
public string Audience { get; set; }
public DateTime NotBefore => DateTime.UtcNow;
DateTime IssuedAt => DateTime.UtcNow;
public long IssuedAtAsUnixEpoch => ToUnixEpochDate(IssuedAt);
public DateTime Expiration => IssuedAt.Add(ValidFor);
public TimeSpan ValidFor => TimeSpan.FromSeconds(ValidForSeconds);
public int ValidForSeconds { get; set; }
public SymmetricSecurityKey Key => new SymmetricSecurityKey(Encoding.ASCII.GetBytes(Secret));
public SigningCredentials SigningCredentials =>
new SigningCredentials(Key, SecurityAlgorithms.HmacSha256);
private static long ToUnixEpochDate(DateTime date) =>
(long) Math.Round(
(date.ToUniversalTime() - new DateTimeOffset(1970, 1, 1, 0, 0, 0, TimeSpan.Zero))
.TotalSeconds);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment