User authentication for C++ apps

[This article is for Windows 8.x and Windows Phone 8.x developers writing Windows Runtime apps. If you’re developing for Windows 10, see the latest documentation]

Azure Mobile Services enables users to log in by using their credentials not only from a Microsoft account, but also from Facebook, Twitter and Google. This article shows how to support authentication by Google in a Windows store app using C++ and XAML. The steps are essentially the same for all of the supported identity providers.

  1. Complete the walkthrough in Quickstart: Add a mobile service (C++).

  2. Most of the work to set up authentication is done in your Microsoft Azure dashboard and that work is the same regardless of what programming language your client app uses. Using the app you have just created, follow the steps outlined in this article, but stop when you get to the section entitled "Add authentication to the app" and follow these steps instead:

  3. Add the following private members to the MainPage class in MainPage.xaml.h:

    concurrency::task<std::wstring> GetAuthenticatedUser(
        azure::mobile::authentication_provider provider, std::wstring currToken);
    azure::mobile::user azureMobileUser;
    std::wstring userid;
    
  4. Add these using statements to MainPage.xaml.cpp:

    using namespace Windows::UI::Popups;
    using namespace concurrency;
    using namespace azure::mobile;
    

    The concurrency namespace makes task and related types visible. The azure::mobile namespace is part of the C++ Client Library for Mobile Services and contains the client and user objects that we'll use to get authenticated.

  5. Add the two method implementations to MainPage.xaml.cpp. Then replace MY_SERVICE_PLACEHOLDER with the actual name of the Azure service you created in the earlier walkthrough.

    task<wstring> MainPage::GetAuthenticatedUser(authentication_provider provider, wstring currToken)
    {
        azure::mobile::client& client = AzureMobileHelper::MY_SERVICE_PLACEHOLDER::GetClient();
        auto token = json::value::object();
    
        task<azure::mobile::user> t;
        if (!currToken.empty())
        {
            auto token = json::value::object();
            token[L"access_token"] = json::value::string(currToken);
            t = client.login(provider, token);
        }
        else
            t = client.login(provider);
    
        userid = L"";
        return create_task(t)
            .then([=](task<user> curruser)
        {
            user me;
            try 
            {
                me = curruser.get();
                userid = me.id();
            }
            catch (std::exception  e) 
            {
                return task_from_result(userid);
            }
            return task_from_result(userid);
        });
    }
    
  6. In MainPage.xaml.cpp, add this code to the OnNavigatedTo member function.

     GetAuthenticatedUser(authentication_provider::google, L"")
            .then([this](task<std::wstring> result)
        {
            RefreshTodoItems();
        });
    

    Because GetAuthenticatedUser returns asynchronously, we put the call to RefreshTodoItems into a .then clause so that we don't attempt to access the table in the mobile service until after the authentication operation has completed.

  7. Press F5 to build and run your app. You should now see the Google login page appear in the popup window. After you sign in with your Google credentials, you should see the ToDo app main page just like before.