29 July 2012

How to Passively authenticate into SharePoint 2013 Online using the .NET Client OM

In my last post I mentioned that the Passive authentication method for the .NET Client Object Model into SharePoint Online (Office 365) as outlined in this MSDN code sample is not compatible with SharePoint Online Wave 15 preview.

It turns out that there is a slight difference in the HTTP responses returned by the Wave 15 version of SharePoint Online compared with the 2010 version. Some of the URLs returned appear in triplicate, separated by commas. Discarding the multiple copies of the URLs brings the code back into working order.

The MSDN sample code can work in the new version of SharePoint Online, once you edit the ExtraHeadersFromResponse method in the ClaimsWebAuth class like so:

        private bool ExtraHeadersFromResponse(WebResponse response, out string loginUrl, out Uri navigationEndUrl)
        {
            loginUrl = null;
            navigationEndUrl = null;

            try
            {
                // For some reason, SharePoint Online Wave 15 Preview seems to return these responses in triplicate, separated by commas
                // Let's just get the first one

                string returnUrl = response.Headers[Constants.CLAIM_HEADER_RETURN_URL];
                if (returnUrl != null && returnUrl.Contains(","))
                {
                    returnUrl = returnUrl.Substring(0, returnUrl.IndexOf(",", StringComparison.InvariantCultureIgnoreCase));
                }
                navigationEndUrl = new Uri(returnUrl);

                loginUrl = (response.Headers[Constants.CLAIM_HEADER_AUTH_REQUIRED]);
                if (loginUrl != null && loginUrl.Contains(","))
                {
                    loginUrl = loginUrl.Substring(0, loginUrl.IndexOf(",", StringComparison.InvariantCultureIgnoreCase));
                }

                return true;
            }
            catch
            {
                return false;
            }
        }
Once this change is made, the code works in both the current and the next versions of SharePoint Online.

6 comments:

  1. Wow, this was troubling me for a while. Thanks for you post! This works flawlessly.

    --Max

    ReplyDelete
    Replies
    1. Excellent! It's good to know that
      a) this is useful info, and
      b) it hasn't changed over the last couple of months!

      I expect we'll see quite a few tweaks to the 365 Preview as we approach the release.

      Delete
  2. Hi
    Did you know if this code also work for actual SP2013 online version ?

    ReplyDelete
    Replies
    1. Hi Lionel,

      Yes it does work against SP 2013 Online, I used this method to create the SharePoint 2013 Explorer tool (http://sharepointrepairjoint.blogspot.com.au/2012/07/sharepoint-2013-explorer.html) which I just used to connect to a Office 365 tenancy.

      Delete
  3. i used this.
    http://vrdmn.blogspot.com/2013/01/authenticating-net-client-object-model.html

    ReplyDelete
    Replies
    1. Thanks! That blog post you've linked to describes a much better method for SP Online 2013 that will support both attended and unattended scenarios.

      I'm very glad that Microsoft have felt the pain that developers were going through trying to simply connect into Office 365 and have provided a new improved method in the 2013 release!

      Delete