[Windows 8.1] Managing multiples users accounts in your Windows Store application

December 23rd, 2013 | Posted by Tom in .NET | Article | Windows 8 | WinRT | Read: 1,258

Most of the apps available are used by single users but imagine you want to develop an application that will allow users to switch user accounts. This lead us to, at least, 2 majors issues: how to store the credentials of the users ? How to allow them to easily switch the current user account ?

 

Storing user credentials:

Credential Locker, available through the PaswwordVault class, allows you to add, get or delete credentials for user in a secure way (all passwords are encrypted and cannot be accessed by other users). Other good point: the credentials are roamed with the Microsoft account users will be able to retrieved its credentials from another device without no cost!

To use the PasswordVault, it’s pretty simple:

private void AddCredentials(string userName, string password)
{
    var passwordVault = new Windows.Security.Credentials.PasswordVault();
    passwordVault.Add(new PasswordCredential(AppName, userName, password));
}

private bool CheckCredentials(string userName, string password)
{
    var retVal = false;

    var passwordVault = new Windows.Security.Credentials.PasswordVault();

    try
    {
        var passwordCredential = passwordVault.Retrieve(AppName, userName);
        if (passwordCredential != null)
        {
            retVal = passwordCredential.Password == password;
        }
    }
    catch (Exception)
    {
        // If the passwordvault does not contains the credentials
        retVal = false;
    }

    return retVal;
}

Once added, the credentials can be viewed from the Credential Manager and we can confirmed that roaming is activated:

image

Ok so now that the credentials are saved and we know how we can retrieved them, let’s see how to display a

 

Switch user account:

The AccountsSettingsPane is a new class in Windows 8.1 that let you add the Accounts command to the Settings Flyout. This command is available, for example, on the native mail application:

image  image

To get the entry added to the SettingsPane, simply add the command to the SettingsPage:

image

Then, the AccountsSettingsPane pane is used like the SettingsPane. First, get a reference to the pane of the current view then, add an event handler to the CommandRequested event:

image

Finally, all the magic appears at the end where you add a provider account for your app and, for each credentials that to the app, you add a web account command (specifying which actions user will be able to take when he will select an account):

image

image

During my investigations, I found that Mike Taulty has posted a similar blog post with Azure Mobile Services (available here) where he gives more details about how to handle the deconnection/reconnection (so I’ll not give you more details here, just look at his post).

 

PS: Source code is available here.

 

Happy coding!

You can follow any responses to this entry through the RSS 2.0 You can leave a response, or trackback.

One Response

Add Comment Register



Leave a Reply