Login page appears to have changed

Aug 3, 2015 at 3:56 PM
Thanks for sharing this solution. Unfortunately it appears to be broken with recent loginpage changes by Microsoft. For me the following changes in Authenticator.cs did the trick:

In the Constants region, add the folowing lines:
        private static readonly Regex REG_CTX = new Regex(@"<input\s.*?name=\""ctx\"".*?value=\""(.*?)\"".*?>", RegexOptions.IgnoreCase);
        private static readonly Regex REG_FLOWTOKEN = new Regex(@"<input\s.*?name=\""flowToken\"".*?value=\""(.*?)\"".*?>", RegexOptions.IgnoreCase);
After the "//Lookup post url" comment add:
            Match mCtx = REG_CTX.Match(strLoginPageContent);
            String strCtx = mCtx.Groups[1].Value;

            Match mFlowToken = REG_FLOWTOKEN.Match(strLoginPageContent);
            String strFlowToken = mFlowToken.Groups[1].Value;
Then below that add to dicPostData:
            dicPostData["ctx"] = strCtx;
            dicPostData["flowToken"] = strFlowToken;
Replace the line directly following "//Post credentials" with:
            HttpWebRequest reqLoginPost = null;
            try
            {
                reqLoginPost = WebRequest.Create(strLoginPostUrl) as HttpWebRequest;
            }
            catch (UriFormatException)
            {
                reqLoginPost = WebRequest.Create(new Uri(reqLoginPage.Address, strLoginPostUrl)) as HttpWebRequest;
            }
I suspect there to be a better solution for this, but I needed a fix and came up with the above.
Aug 21, 2015 at 9:54 AM
Edited Aug 21, 2015 at 9:59 AM
Once again, something changed that broke this code. If you replace the GetAuthenticatedCookies with the following code, it should work again:
        public static CookieContainer GetAuthenticatedCookies(string webUrl, String login, string password)
        {
            var securePassword = new SecureString();
            foreach (char c in password)
            {
                securePassword.AppendChar(c);
            }
            return GetAuthenticatedCookies(new Uri(webUrl), login, securePassword);
        }

        public static CookieContainer GetAuthenticatedCookies(Uri webUri, String login, string password)
        {
            var securePassword = new SecureString();
            foreach (char c in password)
            {
                securePassword.AppendChar(c);
            }
            return GetAuthenticatedCookies(webUri, login, securePassword);
        }

        public static CookieContainer GetAuthenticatedCookies(Uri webUri, String login, SecureString password)
        {
            SharePointOnlineCredentials creds = new SharePointOnlineCredentials(login, password);
            string authCookie = creds.GetAuthenticationCookie(webUri);
            var fedAuthString = authCookie.TrimStart("SPOIDCRL=".ToCharArray());
            var cookies = new CookieContainer();
            cookies.Add(webUri, new Cookie("SPOIDCRL", fedAuthString));
            return cookies;
        }
(thanks to http://stackoverflow.com/questions/25388696/federated-authentication-in-sharepoint-2013-getting-rtfa-and-fedauth-cookies )
Sep 21, 2015 at 11:34 AM
pbvansplunter, thanks for sharing! Your solution works.