Skip to content

Instantly share code, notes, and snippets.

@conradz
Created September 26, 2012 21:04
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save conradz/3790578 to your computer and use it in GitHub Desktop.
Save conradz/3790578 to your computer and use it in GitHub Desktop.
Windows 8 User Credentials
var CREDENTIAL_NAME = "<Your Credential Resource Name>";
var signin = new WinJS.Promise(function (comp, err) {
var vault = new Windows.Security.Credentials.PasswordVault();
try {
var stored = vault.findAllByResource(CREDENTIAL_NAME);
if (stored.length > 0) {
stored = stored[0];
// Retrieve the full stored credential
stored = vault.retrieve(CREDENTIAL_NAME, stored.userName);
return comp({
user: stored.userName,
password: stored.password
});
}
} catch (e) {
// Could not find credential
}
// Prompt the user for a password
var opts = new Windows.Security.Credentials.UI.CredentialPickerOptions();
opts.authenticationProtocol = Windows.Security.Credentials.UI.AuthenticationProtocol.basic;
opts.caption = "<Caption>";
opts.message = "<Message>";
opts.credentialSaveOption = Windows.Security.Credentials.UI.CredentialSaveOption.unselected;
opts.callerSavesCredential = true;
opts.targetName = ".";
var picker = Windows.Security.Credentials.UI.CredentialPicker.pickAsync(opts);
picker.then(function (result) {
if (result.credential == null) {
// User canceled
return err();
}
if (result.credentialSaveOption === Windows.Security.Credentials.UI.CredentialSaveOption.selected) {
vault.add(new Windows.Security.Credentials.PasswordCredential(
CREDENTIAL_NAME, result.credentialUserName, result.credentialPassword));
}
comp({
user: result.credentialUserName,
password: result.credentialPassword
});
}, err);
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment