Skip to content

Instantly share code, notes, and snippets.

@wislon
Last active February 27, 2022 03:02
Show Gist options
  • Save wislon/260438ee77e8de9e4ffc to your computer and use it in GitHub Desktop.
Save wislon/260438ee77e8de9e4ffc to your computer and use it in GitHub Desktop.
Xamarin Forms xplat WebView Cookie Demo
using System.Collections.Generic;
using System.Net;
public interface IPlatformCookieStore
{
/// <summary>
/// List of cookies pulled from the cookie storage manager
/// on the device/platform you're on. Can be quite an expensive call,
/// so use it sparingly.
/// </summary>
IEnumerable<Cookie> CurrentCookies { get; }
/// <summary>
/// Debug method, just lists all cookies in the <see cref="CurrentCookies"/> list
/// </summary>
void DumpAllCookiesToLog();
/// <summary>
/// Clear cookies for site/url (otherwise auth tokens for your provider
/// will hang around after a logout, which causes problems when you want
/// to log in as someone else)
/// </summary>
void DeleteAllCookiesForSite(string url);
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using Foundation;
public class IOSCookieStore : IPlatformCookieStore
{
private readonly object _refreshLock = new object();
public IEnumerable<Cookie> CurrentCookies
{
get { return RefreshCookies(); }
}
public IOSCookieStore(string url = "")
{
}
private IEnumerable<Cookie> RefreshCookies()
{
lock (_refreshLock)
{
foreach (var cookie in NSHttpCookieStorage.SharedStorage.Cookies)
{
yield return new Cookie
{
Comment = cookie.Comment,
Domain = cookie.Domain,
HttpOnly = cookie.IsHttpOnly,
Name = cookie.Name,
Path = cookie.Path,
Secure = cookie.IsSecure,
Value = cookie.Value,
// TODO expires? / expired?
Version = Convert.ToInt32(cookie.Version)
};
}
}
}
public void DumpAllCookiesToLog()
{
if (!CurrentCookies.Any())
{
LogDebug("No cookies in your iOS cookie store. Srsly? No cookies? At all?!?");
}
CurrentCookies.ToList().ForEach(cookie => LogDebug(string.Format("Cookie dump: {0} = {1}", cookie.Name, cookie.Value)));
}
public void DeleteAllCookiesForSite(string url)
{
var cookieStorage = NSHttpCookieStorage.SharedStorage;
foreach(var cookie in cookieStorage.CookiesForUrl(new NSUrl(url)).ToList())
{
cookieStorage.DeleteCookie(cookie);
}
// you MUST call the .Sync method or those cookies may hang around for a bit
NSUserDefaults.StandardUserDefaults.Synchronize();
}
}
sing System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using Android.Webkit;
public class DroidCookieStore : IPlatformCookieStore
{
private readonly string _url;
private readonly object _refreshLock = new object();
public IEnumerable<Cookie> CurrentCookies
{
get { return RefreshCookies(); }
}
public DroidCookieStore(string url = "")
{
if (string.IsNullOrWhiteSpace(url))
{
throw new ArgumentNullException("url", "On Android, 'url' cannot be empty, please provide a base URL for it to use when loading related cookies");
}
_url = url;
}
private IEnumerable<Cookie> RefreshCookies()
{
lock(_refreshLock)
{
// .GetCookie returns ALL cookies related to the URL as a single, long
// string which we have to split and parse
var allCookiesForUrl = CookieManager.Instance.GetCookie(_url);
if(string.IsNullOrWhiteSpace(allCookiesForUrl))
{
LogDebug(string.Format("No cookies found for '{0}'. Exiting.", _url));
yield return new Cookie("none", "none");
}
else
{
LogDebug(string.Format("\r\n===== CookieHeader : '{0}'\r\n", allCookiesForUrl));
var cookiePairs = allCookiesForUrl.Split(' ');
foreach(var cookiePair in cookiePairs.Where(cp => cp.Contains("=")))
{
// yeah, I know, but this is a quick-and-dirty, remember? ;)
var cookiePieces = cookiePair.Split(new[] {'='}, StringSplitOptions.RemoveEmptyEntries);
if(cookiePieces.Length >= 2)
{
cookiePieces[0] = cookiePieces[0].Contains(":")
? cookiePieces[0].Substring(0, cookiePieces[0].IndexOf(":"))
: cookiePieces[0];
// strip off trailing ';' if it's there (some implementations on droid have it, some do not)
cookiePieces[1] = cookiePieces[1].EndsWith(";")
? cookiePieces[1].Substring(0, cookiePieces[1].Length - 1)
: cookiePieces[1];
yield return new Cookie()
{
Name = cookiePieces[0],
Value = cookiePieces[1],
Path = "/",
Domain = new Uri(_url).DnsSafeHost,
};
}
}
}
}
}
public void DumpAllCookiesToLog()
{
if (CurrentCookies.All(c => c.Name == "none"))
{
LogDebug("No cookies in your Android cookie store. Srsly? No cookies? At all?!?");
}
CurrentCookies.Where(c => c.Name != "none").ToList().ForEach(cookie => LogDebug(string.Format("Cookie dump: {0} = {1}", cookie.Name, cookie.Value)));
}
public void DeleteAllCookiesForSite(string url)
{
// TODO remove cookies only for a specific site, coz this is a bit scorched-earth...
CookieManager.Instance.RemoveAllCookie();
}
}
public override bool FinishedLaunching(UIApplication app, NSDictionary options)
{
Locator.CurrentMutable.RegisterConstant(new IOSCookieStore(), typeof(IPlatformCookieStore));
global::Xamarin.Forms.Forms.Init();
LoadApplication(new App());
return base.FinishedLaunching(app, options);
}
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
Locator.CurrentMutable.Register(() => new DroidCookieStore("http://your.web.url"), typeof(IPlatformCookieStore));
global::Xamarin.Forms.Forms.Init(this, bundle);
LoadApplication(new App());
}
public class LameCookieStoreDemo()
{
private IPlatformCookieStore _cookieStore;
private System.Net.Cookie _oauthCookie;
public LameCookieManagerDemo()
{
_cookieStore = Splat.Locator.Current.GetService<IPlatformCookieStore>();
}
public void RunMeWhenWebViewIsDoneNavigating()
{
_cookieStore.DumpAllCookiesToLog();
_oauthCookie = _cookieStore.CurrentCookies.FirstOrDefault(cc => cc.Name == "oauthCookieName");
if (_oauthCookie != null)
{
// do stuff with it, like hand it off to your Web Api's REST/RPC call, or whatever
}
}
public void LogoutByDeletingOAuthCookies()
{
_cookieStore.DeleteAllCookiesForSite("http://your.web.url");
}
}

The MIT License (MIT)

Copyright (c) 2016, John Wilson

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

@nanaleowan
Copy link

Locator.CurrentMutable.RegisterConstant(new IOSCookieStore(), typeof(IPlatformCookieStore)); gives error
Locator doesnot exit in this context

@wislon
Copy link
Author

wislon commented Feb 27, 2022

For anyone struggling with the Splat/Locator stuff, you should just feel to swap that out with the dependency injection framework of your choice. this was a basic code concept demo of how to put it all together; it wasn't designed to be just copied and pasted into your code and Work Perfectly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment