Microsoft.PowerBI.Security.PowerBIToken.Generate C# (CSharp) Method

Generate() public method

Generates an app token with the specified claims and signs it the the configured signing key.
public Generate ( string accessKey = null ) : string
accessKey string The access key used to sign the app token
return string
        public string Generate(string accessKey = null)
        {
            if (string.IsNullOrWhiteSpace(this.AccessKey) && accessKey == null)
            {
                throw new ArgumentNullException(nameof(accessKey));
            }

            this.ValidateToken();

            accessKey = accessKey ?? this.AccessKey;

            var key = Encoding.UTF8.GetBytes(accessKey);
            var signingCredentials = new SigningCredentials(new InMemorySymmetricSecurityKey(key), SecurityAlgorithms.HmacSha256Signature, SecurityAlgorithms.Sha256Digest);
            var token = new JwtSecurityToken(this.Issuer, this.Audience, this.Claims, DateTime.UtcNow, this.Expiration, signingCredentials);

            return new JwtSecurityTokenHandler().WriteToken(token);
        }

Usage Example

        public void CreateGenericTokenWithMissingParamsFails()
        {
            var token = new PowerBIToken
            {
                Issuer = null,
                Audience = null
            };

            token.Generate(this.accessKey);
        }
All Usage Examples Of Microsoft.PowerBI.Security.PowerBIToken::Generate