Show / Hide Table of Contents

TrySetPassword Method

TrySetPassword(ReadOnlyMemory<byte>, ReadOnlyMemory<byte>)

Verify the currentPassword in order to set the OATH application in the YubiKey to be password-protected with the given newPassword.

C#
public bool TrySetPassword(ReadOnlyMemory<byte> currentPassword, ReadOnlyMemory<byte> newPassword)

Parameters

Type Name Description
ReadOnlyMemory<byte> currentPassword

If the OATH application is already password-protected, then this is the current password. If it is not password-protected, you must pass in an Empty value.

ReadOnlyMemory<byte> newPassword

The password to which the OATH application will be set.

Returns

bool

A boolean, true if the OATH application is set to the given password, and false otherwise.

Exceptions

Type Condition
InvalidOperationException

The YubiKey had some error, such as unreliable connection.

Remarks

If the OATH application on the YubiKey is not yet password-protected, you should pass in an Empty currentPassword argument. If you pass in an actual password, this method will try to verify it, which will fail, the method will return false, and the application will not be set with the newPassword.

If the OATH application is already password-protected, the current password must be verified before setting. Hence, this method will verify the currentPassword. Once that password has been verified, this method will be able to set the OATH application on the YubiKey with the newPassword. This is how the password is changed. If the currentPassword does not verify, then this method will return false.

To see if the OATH application is password-protected or not, look at the property IsPasswordProtected.

For example,

bool isSetToNewPassword = false;
if (oathSession.IsPasswordProtected)
{
    // The OATH application is set to password-Protected,
    // this call will change it to a new password.
    isSetToNewPassword = oathSession.TrySetPassword(currentPassword, newPassword);
}
else
{
    // The OATH application is not yet password-protected,
    // this call will set it to be so.
    isSetToNewPassword =
        oathSession.TrySetPassword(ReadOnlyMemory<byte>.Empty, newPassword);
}

Note that if the OATH application is password-protected, and the password has already been verified, it is still necessary to pass in the current password. For example,

if (!oathSession.TryVerifyPassword(currentPassword))
{
    // Some error handling code, maybe exit.
}
  . . . // Some other code, more operations
bool isSetToNewPassword = oathSession.TrySetPassword(currentPassword, newPassword);
If the password has already been verified in the session, and an Empty currentPassword is passed in, this method will return false. If the wrong password is passed in, this method will try to verify it, which will fail, the method will return false, and the application will not be set with the newPassword, even though the current password had been verified in the session previously.

Note also that the only way to get a false return is if the currentPassword does not verify.

In this article
Back to top Generated by DocFX