How to save user credentials (Windows Store apps using JavaScript and HTML)

This topic shows how to store user credentials in the credential locker store by calling the passwordVault.add method.

Technologies

Instructions

  1. Create a passwordVault object.
  2. Create a PasswordCredential object with the appropriate association (the name of the app), user name, and password.
  3. Call the passwordVault.add method to add the credential to the store.

Saving a credential in the store.

The following snippet from the Credential locker sample shows how to add a credential to the store.

In this example, the credential is associated with "Scenario 1". You should use the name of your app for the credential association.



try { 
 
        if (document.getElementById("InputUserNameValue").value === "" || document.getElementById("InputPasswordValue").value === "") { 
            document.getElementById("Scenario1DebugAreaOutput").value = "Empty User Name and Blank Password is not allowed"; 
            throw new Error("Please enter a username and password."); 
                 
        } 
 
        var vault = new Windows.Security.Credentials.PasswordVault(); 
        var cred = new Windows.Security.Credentials.PasswordCredential("Scenario 1", document.getElementById("InputUserNameValue").value,
         document.getElementById("InputPasswordValue").value); 
 
        if (!document.getElementById("Scenario1AuthToggle").checked) { 
 
            if (document.getElementById("SaveCredCheck").checked) { 
                vault.add(cred); 
                document.getElementById("Scenario1DebugAreaOutput").value = 
                "Credential is saved to vault, You can check your credential in 'Control Panel->User Accounts>Credential Manager'"; 
            } 
            document.getElementById("Scenario1WelcomeMessage").value = "Welcome to Scenario 1, " + cred.userName; 
        } else { // Authentication failed 
            document.getElementById("Scenario1WelcomeMessage").value = "blocked"; 
        } 
 
        cleanInputFieldScenario1(); 
    } 
 
    catch (e) { 
        document.getElementById("Scenario1WelcomeMessage").value = "blocked"; 
        document.getElementById("Scenario1DebugAreaOutput").value = e.message; 
    } 

Related topics

How to enumerate and retrieve user credentials
How to delete user credentials
Credential locker sample

 

 

Build date: 6/11/2013

Show:
© 2014 Microsoft