/// <inheritdoc /> public virtual async Task <IToken> BuildAsync(ICredentials creds, IUser user, List <RelatedEntityCollection> relatedEntityCollections) { if (user == null) { var userClient = ClientsCache.Generic.GetValueOrNew <EntityClientAdminAsync <User, long> >(typeof(User).Name); var odataUser = await userClient.GetAsync(creds.User) ?? throw new Exception("User not found."); user = odataUser.Object; relatedEntityCollections = relatedEntityCollections ?? new List <RelatedEntityCollection>(); relatedEntityCollections.AddRange(odataUser.RelatedEntityCollection); } var tokenClient = ClientsCache.Generic.GetValueOrNew <EntityClientAdminAsync <Token, long>, bool>(typeof(Token).Name, true); var token = new Token { Text = CryptoRandomString.GetCryptoRandomAlphaNumericString(TokenSize), UserId = user.Id }; var odataToken = await tokenClient.PostAsync(new List <Token> { token }); var claimConfigClient = ClientsCache.Generic.GetValueOrNew <EntityClientAdminAsync <ClaimConfiguration, int> >(typeof(ClaimConfiguration).Name); var claimConfigs = await claimConfigClient.GetAllAsync(); var claims = await ClaimsBuilder.BuildAsync(user, claimConfigs?.Select(c => c.Object)); if (claims != null && claims.Count > 0) { token.ClaimDomains.AddRange(claims); } Task.WaitAll(); return(token); }