My environment : Microsoft Windows Server 2019 Datacenter (Working as an RDS Gateway)
Publish Profile (be careful with the runtime target, only win10-x64 is correctly working) :
Configuration : Release | Any CPU
Framework : .net6.0
Deployment mode : Autonomous
Runtime target : win10-x64
I'm trying to update RDS Gateway certificate using C#. For that, I'm relying on this PowerShell Command which is correctly working :
# The thumbprint value of my certificate in my Local Store
$ Thumbprint = "00000000000000000000000000000"
# Retrieve this certificate and it's hash
$Cert = Get-Item -Path Cert:\LocalMachine\My\$Thumbprint
$CertHash = $Cert.GetCertHash()
# As we have our CertHash we can set the setting for TS/RDGateway
Get-CimInstance -Namespace root/CIMV2/TerminalServices -ClassName Win32_TSGatewayServerSettings | Invoke-CimMethod -MethodName SetCertificate -Arguments @{CertHash = $CertHash}
# To apply new settings we need to restart TS/RDGateway service
Restart-Service -Name TSGateway -Force
For that, I first decided to use the Microsoft.Management.Infrastructure
NuGet package which is correctly working. Here is my actual code that is causing me some trouble :
using Microsoft.Management.Infrastructure;
using Microsoft.Management.Infrastructure.Options;
using System.Security.Cryptography.X509Certificates;
string Namespace = @"root/CIMV2/TerminalServices";
string className = "Win32_TSGatewayServerSettings";
var sessionOptions = new DComSessionOptions
{
Timeout = TimeSpan.FromSeconds(30)
};
var cimSession = CimSession.Create("localhost", sessionOptions);
X509Store store = new X509Store("My", StoreLocation.LocalMachine);
store.Open(OpenFlags.ReadWrite);
X509Certificate2Collection storecollection = (X509Certificate2Collection)store.Certificates;
byte[] certHash = storecollection.First().GetCertHash();
CimMethodParametersCollection methodParameters = new CimMethodParametersCollection
{
CimMethodParameter.Create("CertHash", certHash, CimType.UInt8Array, CimFlags.In),
};
CimInstance ciminstance = new CimInstance(className, Namespace);
cimSession.InvokeMethod(ciminstance, "SetCertificate", methodParameters);
And my error stacktrace is :
Microsoft.Management.Infrastructure.CimException: ParamŠtre(s) de m‚thode non valide
at Microsoft.Management.Infrastructure.Internal.Operations.CimSyncEnumeratorBase`1.MoveNext()
at System.Linq.Enumerable.TryGetSingle[TSource](IEnumerable`1 source, Boolean& found)
at System.Linq.Enumerable.SingleOrDefault[TSource](IEnumerable`1 source)
at Microsoft.Management.Infrastructure.CimSession.InvokeMethod(String namespaceName, CimInstance instance
, String methodName, CimMethodParametersCollection methodParameters, CimOperationOptions options)
at Microsoft.Management.Infrastructure.CimSession.InvokeMethod(String namespaceName, CimInstance instance
, String methodName, CimMethodParametersCollection methodParameters)
at Microsoft.Management.Infrastructure.CimSession.InvokeMethod(CimInstance instance, String methodName, C
imMethodParametersCollection methodParameters)
at Program.<Main>$(String[] args) in C:\Users\username\source\repos\ConsoleApp1\ConsoleApp1\Program.cs:line 36
Sorry main error is in french, the english equivalent is Invalid Method Parameter
.
It's specifying me that the error is at the last line :
cimSession.InvokeMethod(ciminstance, "SetCertificate", methodParameters);
After couple tests, I found that the main error was the CimMethodParameter
object. I had different errors when I tried to change the Method Name
for example, same for the CimType
, so for me the real problem seems to be the parameter value.
However, I tried many types to be sure and also values, following this documentation : SetCertificate Documentation
I'm directly retrieving the hash value from my first certificate found so it should work, I also tried to retrieve the value using the PowerShell command and to set it directly in my code but it's also not working...
Thanks for your help, I'm taking any answer to help me to resolve this !