internal static IAuthenticationMethod DeriveAuthenticationMethod(IAuthenticationMethod currentAuthenticationMethod, AuthenticationResult deviceCredentials) { string deviceId = deviceCredentials.DeviceId; switch (deviceCredentials.Scope) { case AuthenticationScope.None: var policyKeyAuth = currentAuthenticationMethod as DeviceAuthenticationWithSharedAccessPolicyKey; if (policyKeyAuth != null) { return new DeviceAuthenticationWithSharedAccessPolicyKey(deviceId, policyKeyAuth.PolicyName, policyKeyAuth.Key); } var deviceKeyAuth = currentAuthenticationMethod as DeviceAuthenticationWithRegistrySymmetricKey; if (deviceKeyAuth != null) { return new DeviceAuthenticationWithRegistrySymmetricKey(deviceId, deviceKeyAuth.DeviceId); } var deviceTokenAuth = currentAuthenticationMethod as DeviceAuthenticationWithToken; if (deviceTokenAuth != null) { return new DeviceAuthenticationWithToken(deviceId, deviceTokenAuth.Token); } throw new InvalidOperationException(""); case AuthenticationScope.SasToken: return new DeviceAuthenticationWithToken(deviceId, deviceCredentials.Secret); case AuthenticationScope.DeviceKey: return new DeviceAuthenticationWithRegistrySymmetricKey(deviceId, deviceCredentials.Secret); case AuthenticationScope.HubKey: return new DeviceAuthenticationWithSharedAccessPolicyKey(deviceId, deviceCredentials.PolicyName, deviceCredentials.Secret); default: throw new InvalidOperationException("Unexpected AuthenticationScope value: " + deviceCredentials.Scope); } }