Пример #1
0
 /// <summary>
 ///  GetUserInfoByUserId ZAM 2014-12-02 //syf 20151014
 /// </summary>
 /// <param name="pclsCache"></param>
 /// <param name="UserId"></param>
 /// <returns></returns>
 public UserInfoByUserId GetUserInfoByUserId(DataConnection pclsCache, string UserId)
 {
     UserInfoByUserId ret = new UserInfoByUserId();
     try
     {
         if (!pclsCache.Connect())
         {
             return null;
         }
         InterSystems.Data.CacheTypes.CacheSysList list = null;
         list = Cm.MstUser.GetUserInfoByUserId(pclsCache.CacheConnectionObject, UserId);
         if (list != null)
         {
             ret.UserId = list[0];
             ret.UserName = list[1];
             ret.Password = list[2];
             ret.Class = list[3];
             ret.ClassName = list[4];
             ret.StartDate = list[5];
             ret.EndDate = list[6];
         }
         return ret;
     }
     catch (Exception ex)
     {
         //MessageBox.Show(ex.ToString(), "获取名称失败!");
         HygeiaComUtility.WriteClientLog(HygeiaEnum.LogType.ErrorLog, "CmMstUser.GetUserInfoByUserId", "数据库操作异常! error information : " + ex.Message + Environment.NewLine + ex.StackTrace);
         return null;
     }
     finally
     {
         pclsCache.DisConnect();
     }
 }
Пример #2
0
        /// <summary>
        /// Checks if a token is valid.
        /// </summary>
        /// <param name="token"></param>
        /// <returns></returns>
        public static bool IsTokenValid(string token)
        {
            bool result = false;

            try
            {
                // Base64 decode the string, obtaining the token:username:timeStamp.
                string key = Encoding.UTF8.GetString(Convert.FromBase64String(token));

                // Split the parts.
                string[] parts = key.Split(new char[] { ':' });
                if (parts.Length == 6)
                {
                    // Get the hash message, username, and timestamp.
                    string hash = parts[0];
                    string UserId = parts[1];
                    string role = parts[2];
                    string tokentime = parts[3] + ":" + parts[4] + ":" + parts[5];
                    //long ticks = long.Parse(tokentime);
                    //DateTime timeStamp = new DateTime(ticks);

                    DateTime timeStamp = Convert.ToDateTime(tokentime);

                    // Ensure the timestamp is valid.
                    bool expired = Math.Abs((DateTime.UtcNow - timeStamp).TotalMinutes) > _expirationMinutes;
                    if (!expired)
                    {
                        DataConnection pclsCache = new DataConnection();
                        Regex dReg = new Regex(@"^1[3578][01379]\d{8}$");        
                        Regex tReg = new Regex(@"^1[34578][01256]\d{8}$");        
                        Regex yReg = new Regex(@"^(134[012345678]\d{7}|1[34578][012356789]\d{8})$");
                        Regex mail = new Regex("^\\s*([A-Za-z0-9_-]+(\\.\\w+)*@(\\w+\\.)+\\w{2,5})\\s*$");
                        string pwType = "";
                        if( dReg.IsMatch(UserId) || tReg.IsMatch(UserId) || yReg.IsMatch(UserId))
                        {
                            pwType = "PhoneNo";
                        }
                        else if(mail.IsMatch(UserId))
                        {
                            pwType = "Email";
                        }
                            string UserIdCheck = new UsersMethod().GetIDByInputPhone(pclsCache, pwType, UserId);//用手机号获取UserId  
                             bool exist = new UsersMethod().CheckUserExist(pclsCache, UserIdCheck);
                             if (exist)
                            {
                                   //string password = "******";
                                   UserInfoByUserId list = new UserInfoByUserId();
                                   list = new UsersMethod().GetUserInfoByUserId(pclsCache, UserIdCheck);
                                   string password = "";
                                   if (list != null)
                                   {
                                       password = list.Password;
                                   }
                                    // Hash the message with the key to generate a token.
                                   string computedToken = GenerateToken(UserId, password, role, tokentime);

                                   // Compare the computed token with the one supplied and ensure they match.
                                   result = (token == computedToken);
                             }
                        }
                       
                    }
                
            }
            catch
            {
            }

            return result;
        }