Example #1
0
        void WebClient_GetProfile_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            TruelifeEventArgs eventArg = new TruelifeEventArgs();
            eventArg.Code = 0;
            eventArg.Description = "error";
            try
            {
                if (e.Error != null)
                {
                    throw new Exception(e.Error.Message);
                }
                //----------
                XDocument xdoc = XDocument.Parse(e.Result, LoadOptions.None);
                if (xdoc.Root == null)
                {
                    throw new Exception("Root is null");
                }

                if (xdoc.Root.Element("header") == null)
                {
                    throw new Exception("header is null");
                }

                if (xdoc.Root.Element("header").Element("code") == null)
                {
                    throw new Exception("header/code is null");
                }

                if (xdoc.Root.Element("header").Element("description") == null)
                {
                    throw new Exception("header/description is null");
                }
                //----------
                var code = xdoc.Root.Element("header").Element("code");
                eventArg.Code = XmlValueParser.ParseInteger(code);

                var desc = xdoc.Root.Element("header").Element("description");
                eventArg.Description = XmlValueParser.ParseString(desc);

                if (eventArg.Code == 400)
                {
                    eventArg.ErrorMessageForUser = "กรุณาล็อกอินใหม่";
                    eventArg.ErrorMessageForDeveloper = "error code is " + eventArg.Code + " , " + desc;
                    eventArg.IsError = true;
                    ProfileData = null;
                }
                else if (eventArg.Code != 200)
                {
                    eventArg.ErrorMessageForUser = "error code is " + eventArg.Code + " , " + desc;
                    eventArg.ErrorMessageForDeveloper = "error code is " + eventArg.Code + " , " + desc;
                    eventArg.IsError = true;
                    ProfileData = null;
                }
                else
                {
                    if (xdoc.Root.Element("profile") == null)
                    {
                        throw new Exception("profile is null");
                    }

                    //parse
                    ProfileData.UserID = XmlValueParser.ParseInteger(xdoc.Root.Element("profile").Element("uid"));
                    ProfileData.Email = XmlValueParser.ParseString(xdoc.Root.Element("profile").Element("email"));
                    ProfileData.FirstName = XmlValueParser.ParseString(xdoc.Root.Element("profile").Element("first_name"));
                    ProfileData.LastName = XmlValueParser.ParseString(xdoc.Root.Element("profile").Element("last_name"));
                    ProfileData.Birthday = XmlValueParser.ParseString(xdoc.Root.Element("profile").Element("birthday"));
                    ProfileData.Gender = XmlValueParser.ParseString(xdoc.Root.Element("profile").Element("gender"));
                    ProfileData.DisplayName = XmlValueParser.ParseString(xdoc.Root.Element("profile").Element("display_name"));
                    ProfileData.EmailContact = XmlValueParser.ParseString(xdoc.Root.Element("profile").Element("email_contact"));
                    ProfileData.MobileContact = XmlValueParser.ParseString(xdoc.Root.Element("profile").Element("mobile_contact"));
                    ProfileData.Mobile = XmlValueParser.ParseString(xdoc.Root.Element("profile").Element("mobile"));

                    //----------
                    eventArg.IsError = false;
                }
            }
            catch (Exception ex)
            {
                eventArg.ErrorMessageForUser = "get profile error , some error occur please try again later.";
                eventArg.ErrorMessageForDeveloper = "Error : Truelife - GetProfile ; " + ex.Message;
                eventArg.IsError = true;
                ProfileData = null;
            }
            //-----
            GetProfileCompleted(eventArg);
        }
Example #2
0
        //Event
        void WebClient_LoginEmail_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            TruelifeEventArgs eventArg = new TruelifeEventArgs();
            eventArg.Code = 0;
            eventArg.Description = "error";
            try
            {
                if (e.Error != null)
                {
                    throw new Exception(e.Error.Message);
                }
                //----------
                XDocument xdoc = XDocument.Parse(e.Result, LoadOptions.None);
                if (xdoc.Root == null)
                {
                    throw new Exception("Root is null");
                }

                if (xdoc.Root.Element("header") == null)
                {
                    throw new Exception("header is null");
                }

                if (xdoc.Root.Element("header").Element("code") == null)
                {
                    throw new Exception("header/code is null");
                }

                if (xdoc.Root.Element("header").Element("description") == null)
                {
                    throw new Exception("header/description is null");
                }
                //----------
                var code = xdoc.Root.Element("header").Element("code");
                eventArg.Code = XmlValueParser.ParseInteger(code);

                var desc = xdoc.Root.Element("header").Element("description");
                eventArg.Description = XmlValueParser.ParseString(desc);

                if (eventArg.Code != 200)
                {
                    eventArg.ErrorMessageForUser = eventArg.Description;
                    eventArg.ErrorMessageForDeveloper = eventArg.Description;
                        eventArg.IsError = true;
                }
                else
                {
                    if (xdoc.Root.Element("profile") == null)
                    {
                        throw new Exception("profile is null");
                    }
                    
                    //----------
                    LoginItem tmp_item = new LoginItem();
                    
                    //parse
                    //tmp_item.blog_id = XmlValueParser.ParseString(xdoc.Root.Element("profile").Element("blog_id"));
                    tmp_item.sso_access_token = XmlValueParser.ParseString(xdoc.Root.Element("profile").Element("sso_access_token"));
                    tmp_item.sso_expires = XmlValueParser.ParseString(xdoc.Root.Element("profile").Element("sso_expires"));
                    //tmp_item.username = XmlValueParser.ParseString(xdoc.Root.Element("profile").Element("username"));
                    //tmp_item.password = XmlValueParser.ParseString(xdoc.Root.Element("profile").Element("password"));
                    tmp_item.email = XmlValueParser.ParseString(xdoc.Root.Element("profile").Element("email"));
                    tmp_item.name = XmlValueParser.ParseString(xdoc.Root.Element("profile").Element("name"));
                    tmp_item.birthday = XmlValueParser.ParseString(xdoc.Root.Element("profile").Element("birthday"));
                    tmp_item.gender = XmlValueParser.ParseString(xdoc.Root.Element("profile").Element("gender"));
                    tmp_item.role_id = XmlValueParser.ParseString(xdoc.Root.Element("profile").Element("role_id"));
                    tmp_item.employee_id = XmlValueParser.ParseString(xdoc.Root.Element("profile").Element("employee_id"));
                    tmp_item.sso_uid = XmlValueParser.ParseString(xdoc.Root.Element("profile").Element("sso_uid"));
                    tmp_item.verify_status = XmlValueParser.ParseString(xdoc.Root.Element("profile").Element("verify_status"));
                    
                    //----------
                    LoginData = tmp_item;
                    
                    LoginData.username = username;
                    //LoginData.password = password;
                    LoginData.password = "";//not need to save password
                    
                    eventArg.IsError = false;
                    SaveLoginData();
                }
            }
            catch (Exception ex)
            {
                eventArg.ErrorMessageForUser = "login error , some error occur please try again later.";
                eventArg.ErrorMessageForDeveloper = "Error : Truelife - LoginWithEmail ; " + ex.Message;
                eventArg.IsError = true;
            }
            //-----
            //LoginWithEmailCompleted(eventArg);
            LoginAllCompleted(eventArg);
        }