8-Week Microsoft Teams AppDev Accelerator Program
Limited seats available! - Start date: Wednesday, April 16, 2025
Join Today & Save $1,000
articles

Understanding O365 and SPO Authentication in a Nutshell

The post is part of a series on O365, SharePoint Online, Windows Azure, and authentication. It explains how authentication works with O365 and SPO.

Understanding O365 and SPO Authentication in a Nutshell
by Andrew Connell

Last updated January 11, 2012
2 minutes read

Share this

Focus Mode

  • Feedback & questions

This post is part of a series on Office365/SharePoint Online, Windows Azure and Authentication. The other posts in this series can be found here: Office365/SharePoint Online, Windows Azure and Authentication

In my previous post in this series I talked about how authentication works with Office365 (O365) & SharePoint Online (SPO). You first authenticate with Microsoft Online (MSO) and then your browser passes along the SAML token with each request to site collections in SPO.

The first thing we need to do when accessing SPO programmatically is to understand how we authenticate with MSO and get the SAML token. Thankfully Microsoft provides a sample to help with the authentication piece. What you need is to make sure you have the Windows Identity Framework Runtime installed first. The main class that Wictor Wilen provides for authenticating to MSO is MsOnlineClaimsHelper. This class takes three parameters in the constructor:

  • MSO Username of the account to authenticate with
  • MSO Password of the account to authenticate with
  • URL of the root site collection for your SPO account - note this is the root site collection which may not necessarily be the site collection you are interested in, but it is needed to authenticate and get the key

The MsOnlineClaimsHelper class has a public property, CookieContainer (which is also of type System.Net.CookieContainer), that you’ll use to get the SAML token from MSO to attach to future requests. When the CookieContainer property is accessed, if null, the MsOnlineClaimsHelper will go authenticate and parse the results to extract the SAML token and store it in the cookie container.

Check out the code sample I mentioned previously in this post. What you’re looking for are two files in the MSDN folder of the ACsCichlids.StoreFront project & the CSOM redistributable. These two files are the aforementioned MsOnlineClaimsHelper.cs & a file it depends on, WcfClientContracts.cs. Copy them into your project and that’s all you need in order to authenticate.

Here’s just a little code from a simple console app that shows me authenticating into my SharePoint Online site collection:

static void Main(string[] args) {
  string msoUsername = "jack.sparrow@aconn.onmicrosoft.com";
  string msoPassword = "[password]";
  string msoRootSpSite = "https://aconn.sharepoint.com/";

  var msoHelper = new MsOnlineClaimsHelper(msoUsername,
                                           msoPassword,
                                           msoRootSpSite);

  CookieContainer cookieJar = msoHelper.CookieContainer;
}
Andrew Connell, Microsoft MVP, Full-Stack Developer & Chief Course Artisan - Voitanos LLC.
author
Andrew Connell

Microsoft MVP, Full-Stack Developer & Chief Course Artisan - Voitanos LLC.

Andrew Connell is a full stack developer who focuses on Microsoft Azure & Microsoft 365. He’s a 20+ year recipient of Microsoft’s MVP award and has helped thousands of developers through the various courses he’s authored & taught. Whether it’s an introduction to the entire ecosystem, or a deep dive into a specific software, his resources, tools, and support help web developers become experts in the Microsoft 365 ecosystem, so they can become irreplaceable in their organization.

Feedback & Questions

newsletter

Join 10,000+ developers for news & insights

No clickbait · 100% free · Unsubscribe anytime.

Subscribe to Andrew's newsletter for insights & stay on top of the latest news in the Microsoft 365 Space!
blurry dot in brand primary color
found this article helpful?

You'll love these!

O365/SPO + Azure + AuthN - Challenges with O365, SPO & MSO

O365/SPO + Azure + AuthN - Challenges with O365, SPO & MSO

January 10, 2012

Read now

Create Managed Metadata Columns in SharePoint with Code

Create Managed Metadata Columns in SharePoint with Code

July 25, 2011

Read now

SharePoint 2010 - Managed Metadata: In Depth Look into the Taxonomy Parts

SharePoint 2010 - Managed Metadata: In Depth Look into the Taxonomy Parts

July 21, 2011

Read now

bi-weekly newsletter

Join 10,000+ Microsoft 365 full-stack web developers for news, insights & resources. 100% free.

Subscribe to Andrew's newsletter for insights & stay on top of the latest news in the Microsoft 365 ecosystem!

No clickbait · 100% free · Unsubscribe anytime.

Subscribe to Andrew's newsletter for insights & stay on top of the latest news in the Microsoft 365 Space!