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

Commit

Permalink
Release build cookie problem fix
Browse files Browse the repository at this point in the history
I swear my code worked in Debug mode. The
System.Reflection.TypeExtensions package doesn't work in UWP projects
compiled under .NET Native.
  • Loading branch information
Robert McLaws (Microsoft MVP) committed Nov 8, 2016
1 parent 132dc8d commit f06a826
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 19 deletions.
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 f06a826

Please sign in to comment.