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); }