Show / Hide Table of Contents

SetEnabledUsbCapabilities Method

SetEnabledUsbCapabilities(YubiKeyCapabilities)

Sets which USB features are enabled (and disabled).

C#
public void SetEnabledUsbCapabilities(YubiKeyCapabilities yubiKeyCapabilities)

Parameters

Type Name Description
YubiKeyCapabilities yubiKeyCapabilities

The desired set of USB features to enable on the YubiKey. A set flag means that the related capability is enabled. Otherwise, the capability is disabled. At least one available USB capability must be enabled.

Implements

IYubiKeyDevice.SetEnabledUsbCapabilities(YubiKeyCapabilities)

Exceptions

Type Condition
InvalidOperationException

Either the command failed to complete.

NotSupportedException

An error occurred when attempting to connect to the device.

Remarks

YubiKeys prior to firmware version 5 must use SetLegacyDeviceConfiguration(YubiKeyCapabilities, byte, bool, int).

Modifies the value of EnabledUsbCapabilities. This requires the YubiKey's configuration to be unlocked (see ConfigurationLocked and UnlockConfiguration(ReadOnlySpan<byte>).

The YubiKey will reboot as part of this change. This will cause this IYubiKeyDevice object to become stale, and future connection attempts using this object are likely to fail. To get fresh IYubiKeys, use the YubiKey enumeration functions such as FindAll() and FindByTransport(Transport).

To see which USB features are available on the YubiKey, see AvailableUsbCapabilities. At least one of these capabilities must be enabled.

This example shows how to enable only the Piv capability over USB on all YubiKeys where Piv is available. All other capabilities will be disabled. The new set of enabled USB capabilities will be printed to the console, showing that only Piv is enabled over USB.
IEnumerable<IYubiKeyDevice> yubiKeys =
    YubiKey.FindAll()
    .Where(d => d.AvailableUsbCapabilities.HasFlag(YubiKeyCapabilities.Piv));

foreach (IYubiKeyDevice yubiKey in yubiKeys) { device.SetEnabledUsbCapabilities(YubiKeyCapabilities.Piv); }

// The devices may need a little time to finish rebooting sleep(100);

// Get fresh IYubiKeys IEnumerable<IYubiKeyDevice> freshYubiKeys = YubiKey.FindAll() .Where(d => d.AvailableUsbCapabilities.HasFlag(YubiKeyCapabilities.Piv));

int i = 1; foreach (IYubiKeyDevice yubiKey in freshYubiKeys) { Console.PrintLine($"{i:} {yubiKey.SerialNumber} - {yubiKey.EnabledUsbCapabilities}"); }

To change the state of some capabilities without affecting the others, you can do something like the following:
IYubiKeyDevice yubiKey = YubiKey.FindAll().First();

YubiKeyCapabilities desiredUsbCapabilities = yubiKey.EnabledUsbCapabilities;

// Selectively enable Piv desiredUsbCapabilities |= YubiKeyCapabilities.Piv;

// Selectively disable Otp desiredUsbCapabilities &= ~YubiKeyCapabilities.Otp;

yubiKey.SetEnabledUsbCapabilities(desiredUsbCapabilities);

See Also

SetDeviceInfoCommand
In this article
Back to top Generated by DocFX