Authentication With Office 365 In C#

In this article we are going to learn how to authenticate with Office 365 credentials in C#. Here, we are going to authenticate with email and password of Office 365 but using our defined textboxes and login button so we have to use Office 365 API service URL and we have to install Unirest dll for Http Request and Http Response of the office 365 API.

We can install unirest dll through manage NuGet packages as below.

NuGet

Or we can install using package manager console with the following line:

Install-Package Unirest-API

Package

We have to use below namespace for Unirest-API DLL in our application.

using unirest_net.http;

Below is the API URL of Office 365. You can directly run below URL in your browser and check the API functionality.

authenticate

The above image is asking for credentials if you directly run the URL in the browser after giving the correct email and password. You will get the data as below. This is sample data. You can check original data by clicking above API link and authenticate with your real credentials.

  1. {  
  2.     "@odata.context""https://outlook.office365.com/api/v1.0/$metadata#Me",  
  3.     "@odata.id""https://outlook.office365.com/api/v1.0/Users('[email protected]')",  
  4.     "Id""yourId",  
  5.     "DisplayName""Your Display Name",  
  6.     "Alias""aliasName",  
  7.     "MailboxGuid""mailBoxGuid"  
  8. }  
I am taking a sample ASP.NET example to show the Authentication with Office 365 in C# using Office 365 API.

Below is the sample code for ASP.NET, In that I added two ASP.NET textboxes and one login button.
  1. <table class="auto-style1">  
  2.     <tr>  
  3.         <td>Email:</td>  
  4.         <td>  
  5.             <asp:TextBox ID="txtEmail" runat="server"></asp:TextBox>  
  6.         </td>  
  7.     </tr>  
  8.     <tr>  
  9.         <td>Password:</td>  
  10.         <td>  
  11.             <asp:TextBox ID="txtPassword" runat="server" TextMode="Password"></asp:TextBox>  
  12.         </td>  
  13.     </tr>  
  14.     <tr>  
  15.         <td> </td>  
  16.         <td>  
  17.             <asp:Button ID="btnLogin" runat="server" Text="Login" OnClick="btnLogin_Click" />  
  18.         </td>  
  19.     </tr>  
  20. </table>  
Below is the code for the login button click.
  1. protected void btnLogin_Click(object sender, EventArgs e)   
  2. {  
  3.     bool result = false;  
  4.   
  5.     HttpResponse < string > response = Unirest.get("https://outlook.office365.com/api/v1.0/me/").basicAuth(txtEmail.Text, txtPassword.Text).asJson < string > ();  
  6.   
  7.     if (response.Code == 200)  
  8.         result = true;  
  9.   
  10.     if (result)   
  11.     {  
  12.         ScriptManager.RegisterClientScriptBlock(thisthis.GetType(), "alertMessage""alert('User validated successfully')"true);  
  13.     } else  
  14.     {  
  15.         ScriptManager.RegisterClientScriptBlock(thisthis.GetType(), "alertMessage""alert('Email/Password is wrong')"true);  
  16.     }  
  17. }  
In the above code, we are using Unirest for Http Request and Http Response and ScriptManager.RegisterClientScriptBlock method for alerting the message in the alert box.

After calling the service, we are getting return values in response variable.

If you provide valid credential then output comes as below image.

credential

In this example, for valid credential I am checking with response.Code = 200. If the condition is true then alerting the message as “User validated successfully”;

And if you provide wrong credential then output comes as in the following image.

credential

In above image, we can see that response.Body is empty and response.Code=401. So based on the condition, its alerting the message as “Email/Password is wrong”.

We can write logic as per our business requirement, in this way we can authenticate the user with office 365 API.

You can go through the following link for more Office 365 API reference.
 
Read more articles on Office 365:

 

 


Similar Articles