protected async Task <string> getTokenedLinkAsync(User targetOrProvider, Func <BinaryWriter, Task> writer) { var token = await generateAsync(); return(S.LinkBuilder.GenerateLink( _targetAction.ControllerName, _targetAction.ActionName, new { token = token })); async Task <string> generateAsync() { var ms = new MemoryStream(); using (var writerStream = ms.CreateWriter()) { var tokenMetadata = new TokenMetadata(); S.Db.TokenMetadatas.Add(tokenMetadata); await S.Db.SaveChangesAsync(); writerStream.Write(tokenMetadata.Id); writerStream.Write(DateTimeOffset.UtcNow); writerStream.Write(Convert.ToString(targetOrProvider.Id, CultureInfo.InvariantCulture)); string stamp = await S.UserManager.GetSecurityStampAsync(targetOrProvider); writerStream.Write(stamp); await writer(writerStream); } var protectedBytes = _protector.Protect(ms.ToArray()); return(Convert.ToBase64String(protectedBytes)); } }
public void MetadataIsYieldedWhenUpdating() { var metadata = new TokenMetadata { { "bar", true } }; var token = new Token("foo", metadata); TokenMetadata?yieldedMetadata = null; token.Update((s, md) => { yieldedMetadata = md; return(s); }); Assert.Equal(metadata, yieldedMetadata); }
public string CreateToken(string userId, string username, UserRole role) { var newToken = _generator.GenerateToken(); var metadata = new TokenMetadata() { LastAccess = DateTime.Now, UserId = userId, Username = username, Role = role }; _storage.Add(newToken, metadata); _cache.Add(newToken, metadata); return newToken; }
public string CreateToken(int userId, string username, UserRole role) { var newToken = _generator.GenerateToken(); var metadata = new TokenMetadata() { LastAccess = DateTime.Now, UserId = userId, Username = username, Role = role }; _storage.Add(newToken, metadata); _cache.Add(newToken, metadata); return(newToken); }
internal Dictionary<string, string> GetTokenMetadata(int tokenIndex, int spanIndex) { if (TokenMetadata is null) { TokenMetadata = new Dictionary<long, Dictionary<string, string>>(); } long ix = GetTokenIndex(spanIndex, tokenIndex); Dictionary<string, string> dict; if (TokenMetadata.TryGetValue(ix, out dict)) { return dict; } else { dict = new Dictionary<string, string>(); TokenMetadata.Add(ix, dict); return dict; } //TODO: REMOVE JsonIgnore from Token and from SingleToken when this is implemented }
public void Cache(TokenMetadata metadata) { RouteCache.Metadata = metadata; }
public void WriteAsJson(IJsonWriter jw) { jw.WriteStartObject(); jw.WritePropertyName(nameof(Language)); jw.WriteStringValue(Languages.EnumToCode(Language)); jw.WritePropertyName(nameof(Length)); jw.WriteNumberValue(Length); jw.WritePropertyName(nameof(Value)); jw.WriteStringValue(Value); if (UID.IsNotNull()) { jw.WritePropertyName(nameof(UID)); jw.WriteStringValue(UID); } if (Metadata is object && Metadata.Any()) { jw.WritePropertyName(nameof(Metadata)); jw.WriteStartObject(); foreach (var kv in Metadata) { jw.WritePropertyName(kv.Key); jw.WriteStringValue(kv.Value); } jw.WriteEndObject(); } if (Labels is object && Labels.Length > 0) { jw.WritePropertyName(nameof(Labels)); jw.WriteStartArray(); foreach (var l in Labels) { jw.WriteStringValue(l); } jw.WriteEndArray(); } jw.WritePropertyName(nameof(TokensData)); jw.WriteStartArray(); for (int i = 0; i < TokensData.Length; i++) { var spanData = TokensData[i]; jw.WriteStartArray(); for (int j = 0; j < spanData.Length; j++) { var tk = spanData[j]; long ix = GetTokenIndex(i, j); jw.WriteStartObject(); jw.WritePropertyName(nameof(TokenData.Bounds)); jw.WriteStartArray(); jw.WriteNumberValue(tk.LowerBound); jw.WriteNumberValue(tk.UpperBound); jw.WriteEndArray(); if (tk.Tag != PartOfSpeech.NONE) { jw.WritePropertyName(nameof(TokenData.Tag)); jw.WriteStringValue(tk.Tag.ToString()); } if (tk.Head >= 0) { jw.WritePropertyName(nameof(TokenData.Head)); jw.WriteNumberValue(tk.Head); } if (tk.Frequency != 0) { jw.WritePropertyName(nameof(TokenData.Frequency)); jw.WriteNumberValue(tk.Frequency); } if (!string.IsNullOrEmpty(tk.Replacement)) { jw.WritePropertyName(nameof(TokenData.Replacement)); jw.WriteStringValue(tk.Replacement); } if (TokenMetadata is object) { if (TokenMetadata.TryGetValue(ix, out var tokenMetadata)) { if (!(tokenMetadata is null) && tokenMetadata.Any()) { jw.WritePropertyName(nameof(Metadata)); jw.WriteStartObject(); foreach (var kv in tokenMetadata) { jw.WritePropertyName(kv.Key); jw.WriteStringValue(kv.Value); } jw.WriteEndObject(); } } } if (EntityData is object) { if (EntityData.TryGetValue(ix, out var entities)) { if (!(entities is null) && entities.Any()) { jw.WritePropertyName(nameof(EntityType)); jw.WriteStartArray(); for (int k = 0; k < entities.Length; k++) { jw.WriteStartObject(); jw.WritePropertyName(nameof(EntityType.Type)); jw.WriteStringValue(entities[k].Type); jw.WritePropertyName(nameof(EntityType.Tag)); jw.WriteStringValue(entities[k].Tag.ToString()); if (entities[k].TargetUID.IsNotNull()) { jw.WritePropertyName(nameof(EntityType.TargetUID)); jw.WriteStringValue(entities[k].TargetUID); } if (!(entities[k].Metadata is null) && entities[k].Metadata.Any()) { jw.WritePropertyName(nameof(EntityType.Metadata)); jw.WriteStartObject(); foreach (var kv in entities[k].Metadata) { jw.WritePropertyName(kv.Key); jw.WriteStringValue(kv.Value); } jw.WriteEndObject(); } jw.WriteEndObject(); } jw.WriteEndArray(); } } } jw.WriteEndObject(); } jw.WriteEndArray(); } jw.WriteEndArray(); jw.WriteEndObject(); }
public bool Add(string token, TokenMetadata tokenMetadata) { return(_dictionary.TryAdd(token, tokenMetadata)); }
public void Cache(TokenMetadata metadata) { }