Example #1
0
        public async Task RefreshCookiesAsync()
        {
            if (NeedLogOn)
            {
                throw new InvalidOperationException("Must log on first.");
            }

            _SetDefaultCookies();
            await Task.WhenAll(_RefreshSettingsAsync(), _RefreshHathPerksAsync(), UserStatus?.RefreshAsync());
        }
Example #2
0
        /// <summary>
        /// Log on with tokens.
        /// View <see cref="LogOnUri"/> to get the tokens.
        /// </summary>
        /// <param name="userID">cookie with name ipb_member_id</param>
        /// <param name="passHash">cookie with name ipb_pass_hash</param>
        public async Task LogOnAsync(long userID, string passHash)
        {
            if (userID <= 0)
            {
                throw new ArgumentOutOfRangeException(nameof(userID));
            }
            if (string.IsNullOrWhiteSpace(passHash))
            {
                throw new ArgumentNullException(nameof(passHash));
            }

            passHash = passHash.Trim().ToLowerInvariant();
            if (passHash.Length != 32 || !passHash.All(c => (c >= '0' && c <= '9') || (c >= 'a' && c <= 'f')))
            {
                throw new ArgumentException("Should be 32 hex chars.", nameof(passHash));
            }

            var cookieBackUp = GetLogOnInfo();

            ClearLogOnInfo();

            CookieManager.SetCookie(new HttpCookie(CookieNames.MemberID, Domains.Eh, "/")
            {
                Value = userID.ToString(), Expires = DateTimeOffset.UtcNow.AddYears(5)
            });
            CookieManager.SetCookie(new HttpCookie(CookieNames.PassHash, Domains.Eh, "/")
            {
                Value = passHash, Expires = DateTimeOffset.UtcNow.AddYears(5)
            });

            try
            {
                await refreshCookieAndSettings();

                try
                {
                    await UserStatus?.RefreshAsync();
                    await refreshHathPerks();
                }
                catch { }

                if (NeedLogOn)
                {
                    throw new ArgumentException("Invalid log on info.");
                }
            }
            catch (Exception)
            {
                RestoreLogOnInfo(cookieBackUp);
                throw;
            }
        }