Access Office 365 REST APIs Using .NET Libraries


Microsoft’s new Cloud offering of Office 365 APIs is very popular now a days among developers. Developers can access the Office 365 REST APIs by using libraries available for .NET and JavaScript. Along with these libraries, the Visual Studio 2017 release also brings some key updates to developers for the tooling experience, making it easier to interact with Office 365 services. In this post, I will demonstrate how to log in to Office 365 and access REST services from a Web application.

How the Application Works

When users visit the sample Web site I have developed, they will be redirected to the Office 365 Web site for authentication. After successful authentication, the user will be redirected back to my Web site with a code to request a REST API for a token. The user also will get the account information details with the token. Follow the upcoming steps to create your own app.

Step 1

Visit this link to register your Web application.

Please sign in with Microsoft account credentials when prompted.

Register your app
Figure 1: Register your app

Step 2

Add a meaningful name of your application. Click Create.

Name your application
Figure 2: Name your application

Step 3

Select the server site Web application and ASP.NET. You can change the selection if you are not adding Office 365 authentication for a Web application.

Platform and Technology selection
Figure 3: Platform and Technology selection

Step 4

Next, click Setup.

Set up the app
Figure 4: Set up the app

Step 5

You now can download the sample’s Visual Studio project code and copy/paste the authentication code in your Web application. I have mentioned the necessary code snippets and steps to add sample C# authentication code in your Web application.

To add authentication components, go to Visual Studio Editor: Tools > Nuget Package Manager > Package Manager Console > add OWIN middleware NuGet packages and install. Alternatively, you can execute the following install commands in the package manager console manager. Installed OWIN libraries enable single sign-on (SSO) using an OpenID Connect via cookie-based authentication.

Install-Package Microsoft.Owin.Security.OpenIdConnect
Install-Package Microsoft.Owin.Security.Cookies
Install-Package Microsoft.Owin.Host.SystemWeb

Step 6

Next, you have add a new Startup.cs class to your project that will be executed automatically when the IIS process starts. This class is required for OWIN middleware to configure a OpenID connect authentication.

When creating, make sure the class selected is an OWIN startup class and not a standard C# class. Also, add OWIN and Microsoft.IdentityModel references to Startup.cs.

using Microsoft.Owin;
using Owin;
using Microsoft.IdentityModel.Protocols;
using Microsoft.Owin.Security;
using Microsoft.Owin.Security.Cookies;
using Microsoft.Owin.Security.OpenIdConnect;
using Microsoft.Owin.Security.Notifications;

The following is a code snippet of the startup class.

public class Startup
   string clientId = System.Configuration.ConfigurationManager
   string redirectUri = System.Configuration.ConfigurationManager
   static string tenant = System.Configuration
   string authority = String.Format(System.Globalization
      ["Authority"], tenant);
   public void Configuration(IAppBuilder app)

         (new CookieAuthenticationOptions());
         new OpenIdConnectAuthenticationOptions
            ClientId = clientId,
            Authority = authority,
            RedirectUri = redirectUri,
            PostLogoutRedirectUri = redirectUri,
            Scope = OpenIdConnectScopes.OpenIdProfile,
            ResponseType = OpenIdConnectResponseTypes.IdToken,
            TokenValidationParameters = new
                  .TokenValidationParameters() {
               ValidateIssuer = false },

            Notifications = new
               AuthenticationFailed = OnAuthenticationFailed

   private Task OnAuthenticationFailed
      OpenIdConnectAuthenticationOptions> context)
      context.Response.Redirect("/?errormessage=" +
      return Task.FromResult(0);

Step 7

Next, add a controller to your project to handle sign-in and sign-out requests. Also, add OWIN references to the controller class.

using Microsoft.Owin.Security;
using Microsoft.Owin.Security.Cookies;
using Microsoft.Owin.Security.OpenIdConnect;

The following are code snippets of two methods to handle the sign-in and sign-out processes.

public void SignIn()
   if (!Request.IsAuthenticated)
         new AuthenticationProperties { RedirectUri = "/" },

public void SignOut()

Now, create the application home page for sign-in users. Name it Index page. Add the following HTML code to the Index page.

   <meta name = "viewport" content="width=device-width" />
   <title>Sign-In with Microsoft Guide</title>
@if (!Request.IsAuthenticated)
   < !--If the user is not authenticated,
        display the sign-in button-- >
      < a href = "@Url.Action("SignIn", "Home")"
        style = "text-decoration: none;" >
         < svg 
           xml: space = "preserve" width = "300px"
           height = "50px" viewBox = "0 0 3278 522"
            <style type = "text/css" >.fil0:hover
             {fill: #4B4B4B;} .fnt0 {font-size: 260px;
             font-family: 'Segoe UI Semibold', 'Segoe UI';
             text-decoration: none;}</style>
            <rect class="fil0" x="2" y="2" width="3174"
                  height="517" fill="black" />
            <rect x = "150" y="129" width="122"
                  height="122" fill="#F35325" />
            <rect x = "284" y="129" width="122"
                  height="122" fill="#81BC06" />
            <rect x = "150" y="263" width="122"
                  height="122" fill="#05A6F0" />
            <rect x = "284" y="263" width="122"
                  height="122" fill="#FFBA08" />
            <text x = "470" y="357" fill="white"
                  class="fnt0">Sign in with Microsoft</text>
   <span><br/>Hello @System.Security.Claims
   <br /><br />
   @Html.ActionLink("See Your Claims", "Index", "Claims")
   <br /><br />
   @Html.ActionLink("Sign out", "SignOut", "Home")
   <div style = "background-color:red;color:white;
    font-weight: bold;" >
    Error: @Request.QueryString["errormessage"]</div>

Next, add a controller to display the user’s claims. Next is the code of your controller class.

public class ClaimsController : Controller
   /// <summary>
      /// Add user's claims to viewbag
   /// </summary>
   /// <returns></returns>
   public ActionResult Index()
      var claimsPrincipalCurrent = System.Security.Claims
      // You get the user's first and last name below:
      ViewBag.Name =

      // The 'preferred_username' claim can be used for showing
      // the username
      ViewBag.Username = claimsPrincipalCurrent.FindFirst

      // The subject claim can be used to uniquely identify the
      // user across the Web
      ViewBag.Subject = claimsPrincipalCurrent.FindFirst

      // TenantId is the unique Tenant ID - which represents an
      // organization in Azure AD
      ViewBag.TenantId = claimsPrincipalCurrent.FindFirst

      return View();

Add a view to display the user’s claims details. Add the following HTML to it.

   <meta name = "viewport" content="width=device-width" />
   <title>Sign-In with Microsoft Sample</title>
   <link href = "@Url.Content("~/Content/bootstrap.min.css")"
         rel= "stylesheet" type= "text/css" />
</ head >
< body style= "padding:50px" >
   < h3 > Main Claims:</h3>
   <table class="table table-striped table-bordered
   <br />
   <h3>All Claims:</h3>
   <table class="table table-striped table-bordered
                       table-hover table-condensed">
      @foreach(var claim in System.Security.Claims.ClaimsPrincipal
         < tr >< td > @claim.Type </ td >
               < td > @claim.Value </ td ></ tr >
   <br />
   <br />
   @Html.ActionLink("Sign out", "SignOut", "Home", null,
      new { @class = "btn btn-primary" })

Step 8

Now, configure your ASP.NET Web App with the application’s registration information, SSL, and RedirectUri.

Configure for successful redirection
Figure 5: Configure for successful redirection

Add the following entries in the Web application’s web.config file located in the root’s folder. The RedirectUri value will be different for your application.

<add key = "ClientId"
     value="669eabf8-0497-4ee4-a3ef-8b5279532fgt" />
<add key = "RedirectUri"
     value="http://localhost/MyNewWebApplication" />
<add key = "Tenant" value="common" />
<add key = "Authority"
     value="{0}/v2.0" />

Step 9

Execute your code and let the browser open and direct you to http://localhost/MyNewWebApplication. Now, click the Sign in button and complete the sign-in process. After a successful sign-in, the user will be redirected to the home page of the sample Web site. To view the user’s claims, click the hyperlink; you should see a table containing the basic logged-in user properties.


For more information about the Office 365 authentication process and code snippets, refer to this MSDN Web site.

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read