public bool SetCredentialData(HttpActionContext actionContext, CredentialData credentialData, CommonParam param)
        {
            bool result = false;

            try
            {
                var headers = actionContext.ControllerContext.Request.Headers;
                if (headers.Contains(HttpHeaderConstant.TOKEN_PARAM) && credentialData != null)
                {
                    string       tokenCode = headers.GetValues(HttpHeaderConstant.TOKEN_PARAM).FirstOrDefault();
                    ExtTokenData tokenData = this.GetTokenDataByCodeAndAddress(tokenCode, this.GetAddress(actionContext));
                    if (tokenData != null && tokenData.ExpireTime > DateTime.Now)
                    {
                        credentialData.TokenCode = tokenCode;
                        this._DeleteCredentialData(credentialData.BackendCode, tokenCode, credentialData.DataKey, param);
                        if (!String.IsNullOrWhiteSpace(credentialData.Data))
                        {
                            result = this._InsertCredentialData(credentialData, param);
                        }
                        else
                        {
                            result = true;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                LogSystem.Error(ex);
            }
            return(result);
        }
        public CredentialData GetCredentialData(HttpActionContext actionContext, CommonParam param)
        {
            CredentialData result = null;

            try
            {
                var headers = actionContext.ControllerContext.Request.Headers;
                if (headers.Contains(HttpHeaderConstant.TOKEN_PARAM) && headers.Contains(HttpHeaderConstant.DATA_KEY_PARAM) && headers.Contains(HttpHeaderConstant.BACKEND_CODE_PARAM))
                {
                    string tokenCode   = headers.GetValues(HttpHeaderConstant.TOKEN_PARAM).FirstOrDefault();
                    string dataKey     = headers.GetValues(HttpHeaderConstant.DATA_KEY_PARAM).FirstOrDefault();
                    string backendCode = headers.GetValues(HttpHeaderConstant.BACKEND_CODE_PARAM).FirstOrDefault();

                    ExtTokenData tokenData = this.GetTokenDataByCodeAndAddress(tokenCode, this.GetAddress(actionContext));
                    if (tokenData != null && tokenData.ExpireTime > DateTime.Now)
                    {
                        result = this._GetCredentialData(backendCode, tokenCode, dataKey, param);
                    }
                }
            }
            catch (Exception ex)
            {
                LogSystem.Error(ex);
            }
            return(result);
        }
        public bool SetTokenDataAlive(string tokenCode)
        {
            bool result = false;

            try
            {
                ExtTokenData tokenData = TokenStore.GetTokenData(tokenCode);
                if (tokenData != null)
                {
                    tokenData.LastAccessTime = DateTime.Now;
                    result = true;
                }
            }
            catch (Exception ex)
            {
                LogSystem.Error(ex);
            }
            return(result);
        }
        public TokenData GetAuthenticatedByAddress(HttpActionContext actionContext, CommonParam param)
        {
            TokenData result = null;

            try
            {
                var headers = actionContext.ControllerContext.Request.Headers;
                if (headers.Contains(HttpHeaderConstant.TOKEN_PARAM) && headers.Contains(HttpHeaderConstant.ADDRESS_PARAM))
                {
                    string tokenCode = headers.GetValues(HttpHeaderConstant.TOKEN_PARAM).FirstOrDefault();
                    string address   = headers.GetValues(HttpHeaderConstant.ADDRESS_PARAM).FirstOrDefault();

                    if (String.IsNullOrWhiteSpace(tokenCode) || String.IsNullOrWhiteSpace(address))
                    {
                        return(null);
                    }

                    ExtTokenData tokenData = this.GetTokenDataByCodeAndAddress(tokenCode, address);
                    if (tokenData != null && tokenData.User != null && !String.IsNullOrWhiteSpace(tokenData.ValidAddress))
                    {
                        List <string> validAddress = Regex.Split(tokenData.ValidAddress, Config.ADDRESS_SEPARATOR).ToList();
                        if (tokenData.ExpireTime < DateTime.Now)
                        {
                            return(null);
                        }
                        string backendAddress = this.GetAddress(actionContext);
                        if (!validAddress.Contains(backendAddress))
                        {
                            validAddress.Add(backendAddress);
                            tokenData.ValidAddress = string.Join(Config.ADDRESS_SEPARATOR, validAddress);
                            TokenStore.AddTokenData(tokenData);
                        }
                        Mapper.CreateMap <ExtTokenData, TokenData>();
                        result = Mapper.Map <TokenData>(tokenData);
                    }
                }
            }
            catch (Exception ex)
            {
                LogSystem.Error(ex);
            }
            return(result);
        }
        private TokenData CreateTokenData(HttpActionContext actionContext, CommonParam param, UserData userData, string machineName)
        {
            ExtTokenData result = new ExtTokenData();

            result.LoginTime      = DateTime.Now;
            result.ExpireTime     = result.LoginTime.AddMinutes(Config.TOKEN_TIMEOUT);
            result.LoginAddress   = this.GetAddress(actionContext);
            result.ValidAddress   = result.LoginAddress;
            result.LastAccessTime = DateTime.Now;
            result.MachineName    = machineName;
            result.TokenCode      = this.GenerateTokenCode(userData.Loginname, result.LoginAddress);
            result.User           = userData;

            if (TokenStore.AddTokenData(result))
            {
                return(result);
            }

            return(null);
        }
        private ExtTokenData GetTokenDataByCodeAndAddress(string tokenCode, string address)
        {
            ExtTokenData result = null;

            if (!String.IsNullOrWhiteSpace(tokenCode) && !String.IsNullOrWhiteSpace(address))
            {
                ExtTokenData tokenData = TokenStore.GetTokenData(tokenCode);
                if (tokenData != null && tokenData.User != null && !String.IsNullOrWhiteSpace(tokenData.ValidAddress))
                {
                    List <string> validAddress = Regex.Split(tokenData.ValidAddress, Config.ADDRESS_SEPARATOR).ToList();
                    bool          valid        = tokenCode.Equals(tokenData.TokenCode);
                    if (Config.CHECK_ADDRESS.HasValue && Config.CHECK_ADDRESS.Value)
                    {
                        valid = valid && validAddress != null && validAddress.Contains(address);
                    }
                    if (valid)
                    {
                        result = tokenData;
                    }
                }
            }
            return(result);
        }
Exemple #7
0
        private static bool UpdateTokenData(TokenEnum tokenEnum, string tokenCode, ExtTokenData data)
        {
            bool result = false;

            try
            {
                if (!String.IsNullOrWhiteSpace(tokenCode))
                {
                    switch (tokenEnum)
                    {
                    case TokenEnum.ADD:
                        if (DIC_TOKEN_DATA.ContainsKey(tokenCode))
                        {
                            DIC_TOKEN_DATA.Remove(tokenCode);
                        }
                        DIC_TOKEN_DATA[tokenCode] = data;
                        break;

                    case TokenEnum.REMOVE:
                        if (DIC_TOKEN_DATA.ContainsKey(tokenCode))
                        {
                            DIC_TOKEN_DATA.Remove(tokenCode);
                        }
                        break;

                    default:
                        break;
                    }
                    result = true;
                }
            }
            catch (Exception ex)
            {
                LogSystem.Error(ex);
            }
            return(result);
        }
Exemple #8
0
 internal static bool AddTokenData(ExtTokenData tokenData)
 {
     return(tokenData != null?UpdateTokenData(TokenEnum.ADD, tokenData.TokenCode, tokenData) : false);
 }