This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
byte[] secret = Encoding.UTF8.GetBytes("230aSD123Rqasf!@#wt324EWHgwqeafvASD@#qweTGWesd"); | |
var credentials = new SigningCredentials(new SymmetricSecurityKey(secret), SecurityAlgorithms.HmacSha256); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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[] | |
{ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class JwtValidationResult | |
{ | |
public JwtValidationResult(bool isValid, SecurityToken securityToken, IReadOnlyList<Claim> claims, | |
IReadOnlyList<ClaimsIdentity> identities) | |
{ | |
IsValid = isValid; | |
SecurityToken = securityToken; | |
Claims = claims; | |
Identities = identities; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public bool IsValidResults(IEnumerable<AuthenticationResult> values) | |
{ | |
if (values == null) | |
return false; | |
return values.All(IsValidResult); | |
bool IsValidResult(AuthenticationResult result) | |
{ | |
if (result == null) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private bool HasSpecificDiscipline(out CharacterDiscipline discipline) | |
{ | |
var advancedClass = BuddyTor.Me.AdvancedClass; | |
if (advancedClass == AdvancedClass.None) | |
{ | |
discipline = CharacterDiscipline.None; | |
return false; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
List<string> strings = new List<string> | |
{ | |
null, | |
"hello", | |
"world" | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public Maybe<int> TryParse(string value) | |
{ | |
if (int.TryParse(value, out int num)) | |
return Maybe.OfValueType(num); | |
return Maybe<int>.NoValue; | |
} |