How to sign in office 365 email programatically using o365 api client librarary c# code

2.2k Views Asked by At

I'm integrating an asp.net web application with office 365 email using o365 api client library.

I want to sign in using the c# code not by showing the Microsoft UI sign in page(https://login.microsoftonline.com/login.srf).

If I provide username/password for an user and if the username/password is correct then it should authenticate the user to see the office 365 mails of the asp.net web application.

Thanks in advance.

1

There are 1 best solutions below

3
On

The purpose of OAUTH is to protect both your and your users. As soon as you get into the business of storing credentials, you own responsibility for securing those credentials.

I assume you are looking to access a user's data via a backend process. This workflow requires the "offline_access" scope. Offline does not mean "disconnected from the internet" but rather that the end-user is "offline" from your app (not able to interact with a UX).

You should be executing an OAUTH Code Grant workflow and adding "openid" and "offline_access" to your scopes. This will give you both an id_token that can be used to hit the Office APIs as well as a refresh_token that you can use to get a new id_token when the existing token expires. Refresh tokens can be used even if the end-user is off line (there is no UX involved).

Once you have an id_token and refresh_token, you can use these values to interact with O365 on the end-user's behalf. A common example would be syncing calendars. The first time a user will need to authenticate and configure (i.e. which calendars do they want to sync). From then on, your sever uses the id_token and refresh_token to operate without the user being online (this the term "offline_access").