我需要从代码中删除已保存的wifi配置文件,以便再次启用SoftAP。根据ms文档,没有办法删除配置文件,只有断开连接。这不可能吗?
Ms docs for wifi https://docs.microsoft.com/en-us/uwp/api/windows.devices.wifi.wifiadapter
Device Portal API https://docs.microsoft.com/de-ch/windows/mixed-reality/device-portal-api-reference#wifi-management
这是我使用设备门户API与wifi断开连接的工作代码
// API creds
string username = "Administrator";
string password = "p@ssw0rd
// API request URIs
string apiUri = "http://192.168.1.15:8080/api/wifi/network";
// WiFi details
string wifiInterface = string.Empty;
string wifiProfile = string.Empty;
// WiFi access
WiFiAccessStatus wifiAccess = await WiFiAdapter.RequestAccessAsync();
if (wifiAccess == WiFiAccessStatus.Allowed)
{
// Get WiFi adapter
IReadOnlyList<WiFiAdapter> wifiAdapterResult = await WiFiAdapter.FindAllAdaptersAsync();
WiFiAdapter wifiAdapter = wifiAdapterResult[0];
// Get conn profile / details
ConnectionProfile profile = await wifiAdapter.NetworkAdapter.GetConnectedProfileAsync();
wifiInterface = profile.NetworkAdapter.NetworkAdapterId.ToString();
wifiProfile = profile.ProfileName;
}
// API creds
PasswordCredential credentials = new PasswordCredential("login", username, password);
// HttpClient filter
HttpBaseProtocolFilter filter = new HttpBaseProtocolFilter();
filter.CookieUsageBehavior = HttpCookieUsageBehavior.NoCookies;
filter.CacheControl.ReadBehavior = HttpCacheReadBehavior.MostRecent;
filter.CacheControl.WriteBehavior = HttpCacheWriteBehavior.NoCache;
filter.ServerCredential = credentials;
// HttpClient
HttpClient client = new HttpClient(filter);
apiUri = apiUri + "?interface=" + wifiInterface + "&op=disconnect" + "&createprofile=no";
// Request
HttpRequestMessage request = new HttpRequestMessage();
request.Method = new HttpMethod("POST");
request.RequestUri = new Uri(apiUri);
// Send request
try
{
// Response
HttpResponseMessage response = await client.SendRequestAsync(request);
// Again
if (response.Content.ToString().Contains("Authorization Required"))
{
response = await client.SendRequestAsync(request);
}
}
catch
{
// Dispose
client.Dispose();
filter.Dispose();
}
但是为了删除wifi配置文件,我从API中找不到404。根据上面链接的API文档,请求应该没问题。这是我删除wifi配置文件的代码
// API creds
string username = "Administrator";
string password = "p@ssw0rd
// API request URIs
string apiUri = "http://192.168.1.15:8080/api/wifi/network";
// WiFi details
string wifiInterface = string.Empty;
string wifiProfile = string.Empty;
// WiFi access
WiFiAccessStatus wifiAccess = await WiFiAdapter.RequestAccessAsync();
if (wifiAccess == WiFiAccessStatus.Allowed)
{
// Get WiFi adapter
IReadOnlyList<WiFiAdapter> wifiAdapterResult = await WiFiAdapter.FindAllAdaptersAsync();
WiFiAdapter wifiAdapter = wifiAdapterResult[0];
// Get conn profile / details
ConnectionProfile profile = await wifiAdapter.NetworkAdapter.GetConnectedProfileAsync();
wifiInterface = profile.NetworkAdapter.NetworkAdapterId.ToString();
wifiProfile = profile.ProfileName;
}
// API creds
PasswordCredential credentials = new PasswordCredential("login", username, password);
// HttpClient filter
HttpBaseProtocolFilter filter = new HttpBaseProtocolFilter();
filter.CookieUsageBehavior = HttpCookieUsageBehavior.NoCookies;
filter.CacheControl.ReadBehavior = HttpCacheReadBehavior.MostRecent;
filter.CacheControl.WriteBehavior = HttpCacheWriteBehavior.NoCache;
filter.ServerCredential = credentials;
// HttpClient
HttpClient client = new HttpClient(filter);
apiUri = apiUri + "?interface=" + wifiInterface + "&profile=" + wifiProfile;
// Request
HttpRequestMessage request = new HttpRequestMessage();
request.Method = new HttpMethod("DELETE")
request.RequestUri = new Uri(apiUri);
// Send request
try
{
// Response
HttpResponseMessage response = await client.SendRequestAsync(request);
// Again
if (response.Content.ToString().Contains("Authorization Required"))
{
response = await client.SendRequestAsync(request);
}
}
catch
{
// Dispose
client.Dispose();
filter.Dispose();
}
答案 0 :(得分:0)
您可以从程序中致电netsh
。
netsh wlan delete <profile name>
应该让你到那儿。
答案 1 :(得分:0)
string deleteCommand = "netsh wlan delete profile name=*";
string cmdApi = string.Format("http://192.168.1.15:8080/api/iot/processmanagement/runcommand?command={0}&runasdefaultaccount={1}", GetBase64String(deleteCommand), GetBase64String("no"));
这里要注意的重要一点是你必须将命令编码为base64字符串,否则它不会起作用!
private string GetBase64String(string stringToConvert)
{
return Convert.ToBase64String(Encoding.UTF8.GetBytes(stringToConvert));
}
使用此代码,我终于可以删除某些wifi配置文件,或者在上面的示例中删除每个已保存的配置文件。
答案 2 :(得分:0)
非常感谢Andy找到了这个。我以前是用命令行做的,但这确实有效。我在其周围添加了一些支持代码,以帮助其他我遇到一些问题,例如获取,删除或发布。如果它工作,那么我重新启动IoT以重新进入Onboarding模式。也许有人会觉得这很有帮助。
以管理员身份登录门户网站可能需要也可能不需要,但这就是我的工作方式。 if(interfaceGUID!= null)由先前的Api请求分配,可以删除进行测试。
private string password = "yourpassword";
private string localhost = "127.0.0.1";
private async Task DeleteProfile()
{
try
{
using (HttpClient client = new HttpClient())
{
if (interfaceGUID != null)
{
string deleteCommand = "netsh wlan delete profile name=*";
using (HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, string.Format("http://{0}:8080/api/iot/processmanagement/runcommand?command={1}&runasdefaultaccount={2}", localhost, Convert.ToBase64String(Encoding.UTF8.GetBytes(deleteCommand)), Convert.ToBase64String(Encoding.UTF8.GetBytes("no")))))
{
request.Headers.Authorization = CreateBasicCredentials("Administrator");
using (HttpResponseMessage response = await client.SendAsync(request))
{
if (response.IsSuccessStatusCode == true)
{
ShutdownManager.BeginShutdown(Windows.System.ShutdownKind.Restart, TimeSpan.FromSeconds(1));
}
else
{
Debug.WriteLine("Could not delete the profiles. " + response.ReasonPhrase.ToString());
}
}
}
}
client.Dispose();
}
}
catch (Exception ex)
{
Debug.WriteLine("Could not delete the profiles. " + ex.InnerException.ToString());
}
}
private AuthenticationHeaderValue CreateBasicCredentials(string userName)
{
string toEncode = userName + ":" + password;
Encoding encoding = Encoding.GetEncoding("iso-8859-1");
byte[] toBase64 = encoding.GetBytes(toEncode);
string parameter = Convert.ToBase64String(toBase64);
return new AuthenticationHeaderValue("Basic", parameter);
}
答案 3 :(得分:0)
最近开始研究Windows设备门户API,并发现了这篇文章。您的代码得到404响应的原因是因为在API URI中,&profile=
期望使用Base64值而不是您使用的文本字符串。将配置文件名称编码为Base64后,它应该可以使用。
我相信这在MS的设备门户文档中没有明确说明,因为我只是通过在删除WIFI配置文件时使用网络浏览器调试器检查Windows设备门户网页来发现这一点的。
答案 4 :(得分:0)
要解决此问题,自内部版本17763起,有一种新方法可直接从可用代码中删除WiFi配置文件
bool canDelete = wifiProfile.CanDelete;
if (canDelete)
{
ConnectionProfileDeleteStatus deleteStatus = await wifiProfile.TryDeleteAsync();
}