Skip to content

Instantly share code, notes, and snippets.

@conradz
Created September 26, 2012 21:04

Revisions

  1. conradz revised this gist Oct 5, 2012. No changes.
  2. conradz revised this gist Oct 5, 2012. No changes.
  3. conradz revised this gist Oct 5, 2012. No changes.
  4. conradz revised this gist Oct 5, 2012. No changes.
  5. conradz revised this gist Oct 5, 2012. No changes.
  6. conradz revised this gist Oct 5, 2012. No changes.
  7. conradz revised this gist Oct 5, 2012. No changes.
  8. conradz revised this gist Oct 2, 2012. No changes.
  9. conradz revised this gist Oct 2, 2012. No changes.
  10. conradz revised this gist Sep 26, 2012. 1 changed file with 2 additions and 8 deletions.
    10 changes: 2 additions & 8 deletions credentials.js
    Original file line number Diff line number Diff line change
    @@ -3,28 +3,22 @@ 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 = {
    return comp({
    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;
  11. conradz created this gist Sep 26, 2012.
    54 changes: 54 additions & 0 deletions credentials.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,54 @@
    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);
    })