23.01.2018 Views

MICROSOFT_PRESS_EBOOK_PROGRAMMING_WINDOWS_8_APPS_WITH_HTML_CSS_AND_JAVASCRIPT_PDF

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

A credential object also contains an IPropertySet value named properties, through which the same<br />

information can be managed.<br />

In any case, when you collect credentials from a user and want to save them, create a<br />

Password-Credential and pass it to PasswordVault.add:<br />

var vault = new Windows.Security.Credentials.PasswordVault();<br />

vault.add(cred);<br />

Note that if you add a credential to the locker with a resource and userName that already exist, the<br />

new credential will replace the old. And if at any point you want to delete a credential from the locker,<br />

call the PasswordVault.remove method with that credential.<br />

Furthermore, even though a PasswordCredential object sees the world in terms of usernames and<br />

passwords, that password can be anything else you need to store securely, such as an access token. As<br />

we’ll see in the next section, authentication through OAuth providers might return such a token, in<br />

which case you might store something like “Facebook_Token” in the credential’s resource property,<br />

your app name in userName, and the token in password. This is a perfectly legitimate and expected use.<br />

Once a credential is in the locker, it will remain there for subsequent launches of the app until you<br />

call the remove method or the user explicitly deletes it through Control Panel > User Accounts and<br />

Family Safety >Credential Manager. On a trusted PC (which requires sign-in with a Microsoft account),<br />

Windows will also automatically and securely roam the contents of the locker to the user’s other devices<br />

(which can be turned off in PC Settings > Sync Your Settings > Passwords). This help to create a<br />

seamless experience with your app as the user moves between devices. 74<br />

So, when you launch an app—even when launching it for the first time—always check if the locker<br />

contains saved credentials. There are several methods of the PasswordVault class for doing this:<br />

• findAllByResource Returns an array (vector) of credential objects for a given resource<br />

identifier. This is how you can obtain the username and password that’s been roamed from<br />

another device, because the app would have stored those credentials in the locker on the other<br />

machine under the same resource.<br />

• findAllByUserName Returns an array (vector) of credential objects for a given username. This is<br />

useful if you know the username and want to retrieve all the credentials for multiple resources<br />

that the app connects to.<br />

• retrieve Returns a single credential given a resource identifier and a username. Again, there<br />

will only ever be a single credential in the locker for any given resource and username.<br />

• retrieveAll Returns a vector of all credentials in the locker for this app. The vector contains a<br />

snapshot of the locker and will not be updated with later changes to credentials in the locker.<br />

74 Such roaming will not happen, however, if a credential is first stored in the locker on a domain joined machine. This<br />

protects domain credentials from leaking to the cloud.<br />

660

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!