Пример #1
0
        public string GetEncryptedToken(string audience, string credentials)
        {
            // Ignoring the crdentials and adding a few claims for illustration
            JsonWebEncryptedToken token = new JsonWebEncryptedToken()
            {
                AsymmetricKey = audienceKeys[audience],
                Issuer        = "TokenIssuer",
                Audience      = audience
            };

            token.AddClaim(ClaimTypes.Name, "jqhuman");
            token.AddClaim(ClaimTypes.Role, "Developer");
            token.AddClaim(ClaimTypes.Role, "Admin");

            return(token.ToString());
        }
Пример #2
0
        public void AuthenticateWithEncryptedToken(string token)
        {
            JsonWebEncryptedToken jwt = null;

            try
            {
                jwt = JsonWebEncryptedToken.Parse(token, this.secretKey);

                // Now, swt.Claims will have the list of claims
                jwt.Claims.ToList().ForEach(c => Console.WriteLine("{0} ==> {1}", c.Type, c.Value));

                Thread.CurrentPrincipal = new ClaimsPrincipal(new ClaimsIdentity(jwt.Claims, "JWT"));
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }