Thoughts from a solution architect, technologist and Christian citizen.
Implementing Office 365 into our WinRT application with Visual C++ (Part I)
The following post is about how we can leverage Office 365 (SharePoint in our case) from our WinRT application. WWSAPI will provide the infrastructure required to interact with the Web Services. There’s a blog post I wrote a couple of years ago on WWSAPI that can be found here. The very first thing we have to be aware of is that SharePoint Online relies on Claims-Based authentication which uses FedAuth cookies that are written with an HTTPOnly flag, said that now you must be wondering, how can we retrieve these cookies then?
Before we start delving into the code, I’d like to mention that most of this functionality is encapsulated in a Win32 DLL that’s consumed from our WinRT application. The main reason for this is that WinRT supports only a subset of Win32 and I’d like this functionality to be available to different clients.
Ok… Back to the task of retrieving the FedAuth cookies, we can accomplish this by calling the InternetGetCookieEx function as shown in the ReadSharePointCookies method below, please note two things:
We’re not responsible for the authentication process, but how can we get the cookies after signing in to the site? Well, if you’ve ever used the WebBrowser control (whether it’s WinForms or WPF) then fear not because WinRT provides us with the WebView control, so it’s a pretty straightforward thing to do as shown in the XAML/Code below
14: retval->Insert(ref new String(cookie.first.c_str()), ref new String(cookie.second.c_str()));
Our Win32 DLL must’ve been added to the solution and since every Metro Style application is “packaged” then the application is able to find it without any issues. Another interesting point is the function LoadPackagedLibrary that’s responsible for loading the specified module and its dependencies into the address space of the calling process, then similarly to how it’s done natively, once we have a HINSTANCE to the loaded module, we call GetProcAddress and its return value is cast into a callback to call the desired function (in this case, ReadSharePointCookies).