Skip to content

Instantly share code, notes, and snippets.

public JwtSecurityToken Generate(string userId, IEnumerable<Claim> claims)
{
Requires.NotEmpty(userId, nameof(userId));
// We're using a DateTimeOffset and later using the DateTime property thereof so we don't
// have to mess with extension methods to grab UNIX time, as DateTimeOffset has those built in.
var timeStamp = DateTimeOffset.UtcNow;
var token = new JwtSecurityToken(_options.Issuer, _options.Audience, claims, timeStamp.DateTime,
timeStamp.Add(_options.Expiration).DateTime, _options.SigningCredentials);
byte[] secret = Encoding.UTF8.GetBytes("230aSD123Rqasf!@#wt324EWHgwqeafvASD@#qweTGWesd");
var credentials = new SigningCredentials(new SymmetricSecurityKey(secret), SecurityAlgorithms.HmacSha256);
byte[] secret = Encoding.UTF8.GetBytes("230aSD123Rqasf!@#wt324EWHgwqeafvASD@#qweTGWesd");
var tokenProvider =
new JwtTokenProvider(new TokenProviderOptions("karazhan.org", "api.karazhan.org",
new SigningCredentials(new SymmetricSecurityKey(secret), SecurityAlgorithms.HmacSha256)));
string userId = "aevitas";
var timeStamp = DateTimeOffset.UtcNow;
var claims = new[]
{
var validationParameters = new TokenValidationParameters
{
// Validating the audience, issuer and the key used to sign the token should suffice to verify
// the token is actually issued by our server and not generated elsewhere. If this turns out not to be
// the case in the future, these parameters should be tweaked accordingly.
ValidAudience = _options.Audience,
ValidIssuer = _options.Issuer,
IssuerSigningKey = _options.SigningCredentials.Key,
// We'll skew the clock by the difference in hours between our local time zone and UTC.
// This is because we've timestamped all the tokens with UTC time before, but the token handler
public class JwtValidationResult
{
public JwtValidationResult(bool isValid, SecurityToken securityToken, IReadOnlyList<Claim> claims,
IReadOnlyList<ClaimsIdentity> identities)
{
IsValid = isValid;
SecurityToken = securityToken;
Claims = claims;
Identities = identities;
}
SecurityToken validatedToken;
var claimsPrincipal = _handler.ValidateToken(token, validationParameters, out validatedToken);
var validationResult = new JwtValidationResult(validatedToken != null, validatedToken,
claimsPrincipal.Claims.ToList(), claimsPrincipal.Identities.ToList());
return validationResult;
@aevitas
aevitas / IsValidResults.cs
Created January 14, 2017 15:20
IsValidResults.cs
public bool IsValidResults(IEnumerable<AuthenticationResult> values)
{
if (values == null)
return false;
return values.All(IsValidResult);
bool IsValidResult(AuthenticationResult result)
{
if (result == null)
private bool HasSpecificDiscipline(out CharacterDiscipline discipline)
{
var advancedClass = BuddyTor.Me.AdvancedClass;
if (advancedClass == AdvancedClass.None)
{
discipline = CharacterDiscipline.None;
return false;
}
List<string> strings = new List<string>
{
null,
"hello",
"world"
};
public Maybe<int> TryParse(string value)
{
if (int.TryParse(value, out int num))
return Maybe.OfValueType(num);
return Maybe<int>.NoValue;
}