Created
September 26, 2012 21:04
-
-
Save conradz/3790578 to your computer and use it in GitHub Desktop.
Windows 8 User Credentials
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var CREDENTIAL_NAME = "<Your Credential Resource Name>"; | |
var signin = new WinJS.Promise(function (comp, err) { | |
var vault = new Windows.Security.Credentials.PasswordVault(); | |
var credential; | |
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); | |
credential = { | |
user: stored.userName, | |
password: stored.password | |
}; | |
} | |
} catch (e) { | |
// Could not find credential | |
} | |
if (credential) { | |
// Found saved credential | |
return comp(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