Esempio n. 1
0
        protected async override void OnElementChanged(VisualElementChangedEventArgs e)
        {
            base.OnElementChanged(e);

            var fileName = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "Cookie.json");

            if (!File.Exists(fileName) && UserInfo.CookieContainer.Count < 1)
            {
                await App.Current.MainPage.Navigation.PushModalAsync(new LoginView());

                return;
            }

            var currentCookies = File.Exists(fileName) ? JsonConvert.DeserializeObject <IList <Cookie> >(File.ReadAllText(fileName))
                : CookieHelper.CastToListCookies(UserInfo.CookieContainer.GetCookies(new System.Uri(Contants.INDEX_URL)));

            if (File.Exists(fileName) && CookieHelper.IsExpiredCookie(currentCookies))
            {
                File.Delete(fileName);
                await App.Current.MainPage.Navigation.PushModalAsync(new LoginView());

                return;
            }

            var cookieUrl = new Uri(Contants.INDEX_URL);
            var cookieJar = NSHttpCookieStorage.SharedStorage;

            cookieJar.AcceptPolicy = NSHttpCookieAcceptPolicy.Always;

            foreach (var aCookie in cookieJar.Cookies)
            {
                cookieJar.DeleteCookie(aCookie);
            }

            var jCookies = UserInfo.CookieContainer.GetCookies(cookieUrl);
            IList <NSHttpCookie> eCookies =
                (from object jCookie in jCookies
                 where jCookie != null
                 select(Cookie) jCookie
                 into netCookie
                 select new NSHttpCookie(netCookie)).ToList();

            cookieJar.SetCookies(eCookies.ToArray(), cookieUrl, cookieUrl);

            if (!File.Exists(fileName))
            {
                var jsonToOutput = JsonConvert.SerializeObject(currentCookies);
                File.WriteAllText(fileName, jsonToOutput);
            }

            this.LoadUrl(Contants.INDEX_URL);
        }
        protected async override void OnElementChanged(ElementChangedEventArgs <Xamarin.Forms.WebView> e)
        {
            base.OnElementChanged(e);

            var fileName = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "Cookie.json");

            if (!File.Exists(fileName) && UserInfo.CookieContainer.Count < 1)
            {
                await App.Current.MainPage.Navigation.PushModalAsync(new LoginView());

                return;
            }

            var currentCookies = File.Exists(fileName) ? JsonConvert.DeserializeObject <IList <Cookie> >(File.ReadAllText(fileName))
                : CookieHelper.CastToListCookies(UserInfo.CookieContainer.GetCookies(new System.Uri(Contants.INDEX_URL)));

            if (File.Exists(fileName) && CookieHelper.IsExpiredCookie(currentCookies))
            {
                File.Delete(fileName);
                await App.Current.MainPage.Navigation.PushModalAsync(new LoginView());

                return;
            }

            var cookieManager = CookieManager.Instance;

            cookieManager.SetAcceptCookie(true);
            cookieManager.RemoveAllCookie();

            for (var i = 0; i < currentCookies.Count; i++)
            {
                string cookieValue = currentCookies[i].Value;
                string cookieName  = currentCookies[i].Name;
                currentCookies[i].Expires = currentCookies[i].TimeStamp.AddMinutes(Contants.EXPIRED_TIME);
                cookieManager.SetCookie(Contants.DOMAIN_URL, $"{cookieName}={cookieValue}");
            }
            if (!File.Exists(fileName))
            {
                var jsonToOutput = JsonConvert.SerializeObject(currentCookies);
                File.WriteAllText(fileName, jsonToOutput);
            }

            this.LoadUrl(Contants.INDEX_URL);
        }