Skip to content
This repository has been archived by the owner on Aug 17, 2020. It is now read-only.

Commit

Permalink
Merge pull request #1794 from robertmclaws/master
Browse files Browse the repository at this point in the history
Temporary fix for cookie problems
  • Loading branch information
Robert McLaws (Microsoft MVP) authored Nov 8, 2016
2 parents 5bc83d9 + f06a826 commit 4366071
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 22 deletions.
6 changes: 3 additions & 3 deletions PokemonGo-UWP/project.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
{
"dependencies": {
"HockeySDK.UWP": "4.1.4",
"HockeySDK.UWP": "4.1.5",
"Microsoft.NETCore.UniversalWindowsPlatform": "5.2.2",
"Newtonsoft.Json": "9.0.1",
"NotificationsExtensions.Win10": "14332.0.2",
"Octokit": "0.22.0",
"Template10": "1.1.12-preview-160712",
"Template10": "1.1.12",
"UniversalMapControl": "0.3.45",
"WinRTXamlToolkit.Controls.Gauge.UWP": "2.0.0",
"Xam.Plugin.DeviceMotion": "1.1.2",
"XamlAnimatedGif": "1.1.5"
"XamlAnimatedGif": "1.1.6"
},
"frameworks": {
"uap10.0": {}
Expand Down
45 changes: 32 additions & 13 deletions PokemonGoAPI/Extensions/CookieContainerExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System.Collections.Generic;
using System.Collections;
using System.Collections.Generic;
using System.Dynamic;
using System.Reflection;

namespace System.Net
Expand All @@ -19,21 +21,31 @@ public static class CookieContainerExtensions
/// <returns></returns>
public static IEnumerable<Cookie> GetCookies(this CookieContainer cookieContainer, string domain)
{
#if WINDOWS_UWP
var domainTable = GetFieldValue<dynamic>(cookieContainer, "_domainTable");
foreach (var entry in domainTable)
#else
var domainTable = GetFieldValue<dynamic>(cookieContainer, "m_domainTable");
#endif
if (domainTable as IEnumerable != null)
{
string key = GetPropertyValue<string>(entry, "Key");

if (key.Contains(domain))
foreach (var entry in domainTable)
{
var value = GetPropertyValue<dynamic>(entry, "Value");
string key = GetPropertyValue<string>(entry, "Key");

var internalList = GetFieldValue<SortedList<string, CookieCollection>>(value, "_list");
foreach (var li in internalList)
if (key.Contains(domain))
{
foreach (Cookie cookie in li.Value)
var value = GetPropertyValue<dynamic>(entry, "Value");
#if WINDOWS_UWP
var internalList = GetFieldValue<SortedList<string, CookieCollection>>(value, "_list");
#else
var internalList = GetFieldValue<SortedList<string, CookieCollection>>(value, "m_list");
#endif
foreach (var li in internalList)
{
yield return cookie;
foreach (Cookie cookie in li.Value)
{
yield return cookie;
}
}
}
}
Expand All @@ -49,9 +61,16 @@ public static IEnumerable<Cookie> GetCookies(this CookieContainer cookieContaine
/// <returns></returns>
internal static T GetFieldValue<T>(object instance, string fieldName)
{
BindingFlags bindFlags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static;
FieldInfo fi = instance.GetType().GetField(fieldName, bindFlags);
return (T)fi.GetValue(instance);
try
{
BindingFlags bindFlags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static;
FieldInfo fi = instance.GetType().GetField(fieldName, bindFlags);
return fi != null ? (T)fi.GetValue(instance) : (T)new object();
}
catch (Exception ex)
{
return (T)new object();
}
}

/// <summary>
Expand Down
11 changes: 5 additions & 6 deletions PokemonGoAPI/Login/PtcLogin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,14 @@ public async Task<AccessToken> GetAccessToken()
{
AccessToken accessToken;
PtcLoginParameters loginData = null;
var cookies = Cookies.GetCookies("sso.pokemon.com").ToList();
Cookies = new CookieContainer();
//var cookies = Cookies.GetCookies("sso.pokemon.com")?.ToList();
// @robertmclaws: "CASTGC" is the name of the login cookie that the service looks for, afaik.
// The second one is listed as a backup in case they change the cookie name.
if (!cookies.Any(c => c.Name == "CASTGC") || cookies.Count == 0)
{
//if (!cookies.Any(c => c.Name == "CASTGC") || cookies.Count == 0)
//{
loginData = await GetLoginParameters().ConfigureAwait(false);
}
//}
var authTicket = await GetAuthenticationTicket(loginData).ConfigureAwait(false);
accessToken = await GetOAuthToken(authTicket).ConfigureAwait(false);

Expand All @@ -99,7 +100,6 @@ public async Task<AccessToken> GetAccessToken()
/// <summary>
/// Responsible for retrieving login parameters for <see cref="GetAuthenticationTicket" />.
/// </summary>
/// <param name="httpClient">An initialized <see cref="HttpClient" /></param>
/// <returns><see cref="PtcLoginParameters" /> for <see cref="GetAuthenticationTicket" />.</returns>
private async Task<PtcLoginParameters> GetLoginParameters()
{
Expand All @@ -112,7 +112,6 @@ private async Task<PtcLoginParameters> GetLoginParameters()
/// <summary>
/// Authenticates against the PTC login service and acquires an Authentication Ticket.
/// </summary>
/// <param name="httpClient">The <see cref="HttpClient"/> instance to use for this request.</param>
/// <param name="loginData">The <see cref="PtcLoginParameters" /> to use from this request. Obtained by calling <see cref="GetLoginParameters(HttpClient)"/>.</param>
/// <returns></returns>
private async Task<string> GetAuthenticationTicket(PtcLoginParameters loginData)
Expand Down

0 comments on commit 4366071

Please sign in to comment.