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.
Or we can install using package manager console with the following line:
Install-Package Unirest-API
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.
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.
- {
- "@odata.context": "https://outlook.office365.com/api/v1.0/$metadata#Me",
- "@odata.id": "https://outlook.office365.com/api/v1.0/Users('[email protected]')",
- "Id": "yourId",
- "DisplayName": "Your Display Name",
- "Alias": "aliasName",
- "MailboxGuid": "mailBoxGuid"
- }
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.
- <table class="auto-style1">
- <tr>
- <td>Email:</td>
- <td>
- <asp:TextBox ID="txtEmail" runat="server"></asp:TextBox>
- </td>
- </tr>
- <tr>
- <td>Password:</td>
- <td>
- <asp:TextBox ID="txtPassword" runat="server" TextMode="Password"></asp:TextBox>
- </td>
- </tr>
- <tr>
- <td> </td>
- <td>
- <asp:Button ID="btnLogin" runat="server" Text="Login" OnClick="btnLogin_Click" />
- </td>
- </tr>
- </table>
Below is the code for the login button click.
- protected void btnLogin_Click(object sender, EventArgs e)
- {
- bool result = false;
-
- HttpResponse < string > response = Unirest.get("https://outlook.office365.com/api/v1.0/me/").basicAuth(txtEmail.Text, txtPassword.Text).asJson < string > ();
-
- if (response.Code == 200)
- result = true;
-
- if (result)
- {
- ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('User validated successfully')", true);
- } else
- {
- ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('Email/Password is wrong')", true);
- }
- }
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.
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.
In above image, we can see that
response.Body is empty and r
esponse.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: