Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ConnectNetworkAsync fails when not logged in locally or via RDP #52

Open
prushik opened this issue Jan 15, 2024 · 0 comments
Open

ConnectNetworkAsync fails when not logged in locally or via RDP #52

prushik opened this issue Jan 15, 2024 · 0 comments

Comments

@prushik
Copy link

prushik commented Jan 15, 2024

I have an application that runs in the background. Typically, it is started through an RDP connection, but eventually we will run as a service.
Everything works fine when we are still logged in, but if we start running and then disconnect from RDP, we get an exception on ConnectNetworkAsync:

System.ComponentModel.Win32Exception: 'MethodName: WlanConnect, ErrorCode: 87, ErrorMessage: The parameter is incorrect.
'

The following code should help to reproduce the issue:

// give time to log out to trigger the issue
Thread.Sleep(10000);

// create profile
ManagedNativeWifi.NativeWifi.SetProfile(iface.Id, ProfileType.PerUser, CreateProfileXml(ssid+"_test", ssid, passwd), null, true);

// the following line works correctly while logged in, but throws an exception when logged out
bool connect_success = ManagedNativeWifi.NativeWifi.ConnectNetworkAsync(iface.Id, ssid + "_test", BssType.Infrastructure, new System.Net.NetworkInformation.PhysicalAddress(Array.ConvertAll(bssid.Split(':'), x => Convert.ToByte(x, 16))), new TimeSpan(0, 1, 0), new CancellationToken()).GetAwaiter().GetResult();

// ... snip ...

private static string CreateProfileXml(string profileName, string ssidString, string password) =>
	$@"<?xml version=""1.0""?>
<WLANProfile xmlns=""http://www.microsoft.com/networking/WLAN/profile/v1"">
	<name>{profileName}</name>
	<SSIDConfig>
		<SSID>
			<hex>{toHex(ssidString)}</hex>
			<name>{ssidString}</name>
		</SSID>
	</SSIDConfig>
	<connectionType>ESS</connectionType>
	<connectionMode>manual</connectionMode>
	<MSM>
		<security>
			<authEncryption>
				<authentication>WPA2PSK</authentication>
				<encryption>AES</encryption>
				<useOneX>false</useOneX>
			</authEncryption>
			<sharedKey>
				<keyType>passPhrase</keyType>
				<protected>false</protected>
				<keyMaterial>{password}</keyMaterial>
			</sharedKey>
		</security>
	</MSM>
</WLANProfile>";

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant