Monday, October 5, 2015

O365 Dev Challenges - Part 4 - Connecting Office 365 services to the project

At the moment I have an application which lets me sign in and read data from Azure AD. As I also want to send e-mail I need to add capabilities to send e-mail. This is achieved by connecting the application to a connected service.

Adding Office 365 resources to your project

Right click on the web project and add a Connected Service.


I want to change the Azure AD settings and add e-mail capabilities, and thus pick Office 365 APIs, and click Configure.


Pick the domain of the Azure AD which will host the application, typically the Azure instance mentioned in the Prerequisites in Part 1.


On the next screen pick Use settings from an existing Azure AD application to access Office 365 API services. The client id should match the id you have in your web.config file.


Click Next until you get to the Mail settings, where you select Send mail as you. This allows the application to later send e-mails on the user’s behalf using the Office 365 API’s.


Move on further to Users and Groups.

Remember that I originally checked the Read directory data permission when creating the ASP.NET application in order to get the AdalTokenCache code inserted into the project. Now I can remove it as I’m not going to read multiple items from Azure AD.


When clicking Finish VS2015 will start adding the service and pull in needed nuget packages as well as reconfigure your App registration with the chosen delegate permissions.


Taking a look at web.config I see that two new lines have been added; the tentantid and domain where you are hosting the application. This is all good, but not needed as we are doing a multi-tenant application, so you can safely remove them again.

Check the web.config file.


Back in the Azure management portal, the app registration page reflects the changes you made in the wizard. One AAD permission for logging in and reading the users profile, and one Exchange permissions for sending e-mails.


What nuget packages has been added?

Open up the nuget package manager and a reference to OutlookServices has been added. This library contains the OutlookServicesClient which is used to perform e-mail actions.


Now is also a perfect opportunity to upgrade some of the default packages which have been added to the project.

I upgraded Microsoft.Azure.ActiveDirectory.GraphClient from v2.0.2 to 2.1.0 which allows you to replace

IUser user = activeDirectoryClient.Users
.Where(u => u.ObjectId.Equals(userObjectID))


IUser user = activeDirectoryClient.Me.ExecuteAsync().Result

to get the current users object from AAD.

NOTE: The above code replacement is a necessity as I removed the application permission to Read directory data. The original code does a query on all items, while the latter loads your profile only. If you don’t replace it, you will get a permission error when loading the user.

Any other package upgrade should also be ok to make sure you are current.