public async Task<bool> ConnectToServiceLoginAuth(HttpClient client, Uri plusBaseUrl, System.Net.CookieContainer responseCheckTarget, string email, string password) { const string loginPageUrl = "https://accounts.google.com/ServiceLogin"; const string authPageUrl = "https://accounts.google.com/ServiceLoginAuth"; const string htmlInputElementPattern = "<input[^>]+name=[\"'](?<key>[^\"']*)[\"'][^>]+value=[\"'](?<value>[^\"']*)[\"'][^>]*>"; //必要な値をログインページから取ってくる var loginPageHtm = await GetStringAsync(client, new Uri(string.Format("{0}?service=oz&continue={1}&hl=ja", loginPageUrl, plusBaseUrl))); //想定されたレスポンスを返したかチェック var cookie = responseCheckTarget.GetCookies(new Uri(loginPageUrl)); if (cookie["GALX"] == null || cookie["GAPS"] == null || cookie["GoogleAccountsLocale_session"] == null) throw new Exception("ログインの仕組みが新しいものに変更されているため、このライブラリではログイン処理を進めることができません。"); //認証用クエリ生成 var queryCollection = new Dictionary<string, string>(); queryCollection.Add("Email", email); queryCollection.Add("Passwd", password); var match = System.Text.RegularExpressions.Regex.Match(loginPageHtm, htmlInputElementPattern); while (match.Success) { var key = match.Groups["key"].Value; if (string.IsNullOrEmpty(key) == false && key != "Email" && key != "Passwd") { var val = match.Groups["value"].Value; queryCollection.Add(key, val); } match = match.NextMatch(); } System.Diagnostics.Debug.Assert( queryCollection.Count == 16, "ログインフォームのパラメータに変化があります。ログイン処理の変化を確認してください。"); //認証開始 var isFail = true; using (var res = await client.PostAsync(new Uri(authPageUrl), new FormUrlEncodedContent(queryCollection))) if (res.IsSuccessStatusCode) { //想定されたレスポンスを返したかチェック //accountドメインのcookieをチェック cookie = responseCheckTarget.GetCookies(new Uri(loginPageUrl)); isFail = cookie["SID"] == null || cookie["LSID"] == null || cookie["HSID"] == null || cookie["SSID"] == null || cookie["APISID"] == null || cookie["SAPISID"] == null; //plusドメインのcookieをチェック cookie = responseCheckTarget.GetCookies(plusBaseUrl); isFail |= cookie["SID"] == null; System.Diagnostics.Debug.Assert(isFail == false, "ログインの仕組みが新しいものに変更されているため、このライブラリではログイン処理を進めることができません。"); } return isFail == false; }
void SetCookies (System.Net.CookieContainer cookies, Uri uri) { var store = NSHttpCookieStorage.SharedStorage; var nsurl = NSUrl.FromString(uri.AbsoluteUri); var nscookies = from c in cookies.GetCookies (uri).OfType<System.Net.Cookie> () select new NSHttpCookie (c); store.SetCookies (nscookies.ToArray (), nsurl, nsurl); }
public static bool CheckCanAuth(System.Net.CookieContainer cookies) { return cookies.GetCookies(new Uri("https://plus.google.com"))["SSID"] != null; }