Пример #1
0
        internal static bool CreateToken(string apikey, TokenModelIn modelDataIn, ref TokenModelOut modelDataOut)
        {
            var unixEpoch = FromUnixTime(0);
            var expiry    = Math.Round((DateTime.UtcNow.AddMinutes(modelDataIn.SessionMinutes) - unixEpoch).TotalSeconds);
            var issuedAt  = Math.Round((DateTime.UtcNow - unixEpoch).TotalSeconds);
            var notBefore = Math.Round((DateTime.UtcNow.AddMonths(6) - unixEpoch).TotalSeconds);

            var payload = new Dictionary <string, object>
            {
                { "version", constVersionToken },
                { "userid", modelDataIn.UserId },
                { "surname", modelDataIn.UserFullName },
                { "role", "User" },
                { "nbf", notBefore },
                { "iat", issuedAt },
                { "exp", expiry },
                //{"sub:userid", ModelDataIn.UserId},
                //{"sub:username", ModelDataIn.UserName},
                //{"sub:fullname", ModelDataIn.UserFullName},
            };

            byte[] apikeyObj = GetBytesSecretKey(apikey);
            var    token     = JWT.Encode(payload, apikeyObj, JwsAlgorithm.HS256);

            modelDataOut.Token = token;
            return(true);
        }
Пример #2
0
        public static TokenModelIn GetTokenModel(string token, string clientApiKey, short sessionMinutes)
        {
            var resultModel = new TokenModelIn()
            {
                SessionMinutes = sessionMinutes,
            };

            var payloadData = GetTokenDictionary(token, clientApiKey);

            if (payloadData != null)
            {
                foreach (var pair in payloadData)
                {
                    var claimType = pair.Key;

                    if (pair.Value is ArrayList source)
                    {
                        continue;
                    }

                    switch (pair.Key.ToLower())
                    {
                    case "userid":
                        resultModel.UserId = pair.Value.ToString();
                        break;

                    case "surname":
                        resultModel.UserFullName = pair.Value.ToString();
                        break;
                    }
                }
            }
            return(resultModel);
        }
Пример #3
0
        public static TokenModelOut CreateToken(string clientApiKey, TokenModelIn ModelData)
        {
            var tokenReturn = new TokenModelOut()
            {
                //IdUsuario = ModelData.IdUsuario,
                //Usuario = ModelData.Usuario,
                //NombreUsuario = ModelData.NombreUsuario,
                //Token = clientToken,
            };

            CreateToken(clientApiKey, ModelData, ref tokenReturn);

            return(tokenReturn);
        }