Example #1
0
        public Token GetToken()
        {
            var token = new Token();

            token.AddHeader("typ", "JWT");

            token.IssuedBy = this.TokenIssuerName;
            token.Audience = this.Catalog;
            token.CreatedOn = DateTime.UtcNow;
            token.ExpiresOn = DateTime.UtcNow.AddHours(this.TokenValidHours);
            token.Subject = this.Catalog;
            token.TokenId = this.Catalog + this.LoginId;
            token.LoginId = this.LoginId;
            token.ApplicationId = this.ApplicationId;
            token.ClientToken = this.Encode(token);

            return token;
        }
Example #2
0
        public Token Decode(string clientToken)
        {
            var token = new Token();
            string decoded = JWT.Decode(clientToken, this.Key);
            var dto = JsonConvert.DeserializeObject<List<Claim>>(decoded);
            token.ClientToken = clientToken;

            foreach (var c in dto)
            {
                switch (c.Type)
                {
                    case "aud":
                        token.Audience = c.Value;
                        break;
                    case "iat":
                        token.CreatedOn = new DateTime(c.Value.To<long>(), DateTimeKind.Utc);
                        break;
                    case "exp":
                        token.ExpiresOn = new DateTime(c.Value.To<long>(), DateTimeKind.Utc);
                        break;
                    case "sub":
                        token.Subject = c.Value;
                        break;
                    case "jti":
                        token.TokenId = c.Value;
                        break;
                    case "iss":
                        token.IssuedBy = c.Value;
                        break;
                    case "loginid":
                        token.LoginId = c.Value.To<long>();
                        break;
                    case "userid":
                        token.UserId = c.Value.To<int>();
                        break;
                    case "officeid":
                        token.OfficeId = c.Value.To<int>();
                        break;
                }
            }

            return token;
        }
Example #3
0
 public static void Save(Token token, string ipAddress, string userAgent)
 {
     Factory.Insert(AppUsers.GetCatalog(), new AccessToken
     {
         ApplicationId = token.ApplicationId,
         Audience = token.Audience,
         Claims = JsonConvert.SerializeObject(token.Claims),
         ClientToken = token.ClientToken,
         CreatedOn = token.CreatedOn,
         ExpiresOn = token.ExpiresOn,
         Header = JsonConvert.SerializeObject(token.Header),
         IpAddress = ipAddress,
         IssuedBy = token.IssuedBy,
         LoginId = token.LoginId,
         Subject = token.Subject,
         TokenId = token.TokenId,
         UserAgent = userAgent
     });
 }
Example #4
0
        public Token GetToken()
        {
            var token = new Token();

            token.AddHeader("typ", "JWT");

            token.IssuedBy = this.TokenIssuerName;
            //token.Audience = this.Tenant;
            token.CreatedOn = DateTimeOffset.UtcNow;
            token.ExpiresOn = DateTimeOffset.UtcNow.AddHours(this.TokenValidHours);
            token.Subject = this.Tenant;
            //token.TokenId = this.Tenant + this.LoginId;
            token.LoginId = this.LoginId;
            //token.UserId = this.UserId;
            //token.OfficeId = this.OfficeId;
            token.ApplicationId = this.ApplicationId;
            token.ClientToken = this.Encode(token);

            return token;
        }
Example #5
0
 public static async Task SaveAsync(string tenant, Token token, string ipAddress, string userAgent)
 {
     await Factory.InsertAsync(tenant, new AccessToken
     {
         ApplicationId = token.ApplicationId,
         //Audience = token.Audience,
         Audience = tenant,
         Claims = JsonConvert.SerializeObject(token.Claims),
         ClientToken = token.ClientToken,
         CreatedOn = token.CreatedOn,
         ExpiresOn = token.ExpiresOn,
         Header = JsonConvert.SerializeObject(token.Header),
         IpAddress = ipAddress,
         IssuedBy = token.IssuedBy,
         LoginId = token.LoginId,
         Subject = token.Subject,
         //TokenId = token.TokenId,
         TokenId = tenant + "/" + token.LoginId,
         UserAgent = userAgent
     }).ConfigureAwait(false);
 }
Example #6
0
 private string Encode(Token token)
 {
     return JWT.Encode(token.Claims, this.Key, this.Algorithm, token.Header);
 }