UWP:无法创建新的VPN配置文件

3
我正在尝试在通用Windows应用程序中创建一个新的VPN配置文件。我正在使用此API
有两种记录的方法:AddProfileFromXmlAsync和AddProfileFromObjectAsync。不幸的是,这两种方法都无法正常工作。
当我使用AddProfileFromXmlAsync时,我始终会遇到AccessDenied错误。我在此主题上看到,这可能与错误的XML语法有关,但是即使我使用Microsoft示例中的确切XML,我仍然会收到相同的错误。
如果您只提供ProfileName,AddProfileFromObjectAsync可以正常工作。否则,它将显示“其他”错误。这对我来说不够,因为我还需要配置NativeProtocolType属性。在尝试添加新配置文件之前,我会检查配置文件是否已经存在,因此肯定不是因为配置文件已经存在而出现其他错误。
我将这些功能添加到我的应用程序清单中:
    <Capabilities>
        <Capability Name="internetClient" />
        <Capability Name="internetClientServer" />
        <Capability Name="privateNetworkClientServer" />
       <rescap:Capability Name="networkingVpnProvider" />
  </Capabilities>

enter image description here

有人知道可能是什么问题吗?涉及到IT技术。

1个回答

2

这是一个在15063上工作的样例(请注意,周年更新中存在一个导致API无法工作的错误,因此请尝试使用15063或确保您安装了所有周年更新的更新)

// Install key and cert
await 
CertificateEnrollmentManager.UserCertificateEnrollmentManager.ImportPfxDataAsync(PEMKey,
Password,
ExportOption.NotExportable,
KeyProtectionLevel.NoConsent,
InstallOptions.None,
"Test VPN");

VpnNativeProfile profile = new VpnNativeProfile();
profile.AlwaysOn = true;
profile.NativeProtocolType = VpnNativeProtocolType.IpsecIkev2;
profile.ProfileName = "Test VPN";
profile.RememberCredentials = false;
profile.RequireVpnClientAppUI = true;
profile.RoutingPolicyType = VpnRoutingPolicyType.ForceAllTrafficOverVpn;
profile.Servers.Add(Constants.ConcentratorDNSName);
profile.UserAuthenticationMethod = VpnAuthenticationMethod.Eap;
profile.EapConfiguration = File.ReadAllText("profile.xml");
VpnManagementAgent agent = new VpnManagementAgent();
await agent.AddProfileFromObjectAsync(profile);
VpnManagementErrorStatus status = await agent.ConnectProfileAsync(profile);

可以在https://learn.microsoft.com/en-us/windows/client-management/mdm/eap-configuration找到示例EAP XML配置。

另外,遗憾的是VPNv2 CSP中的XML与API中的XML不同。此外,在Microsoft内部检查时,AddProfileFromXMLAsync存在错误导致其无法工作。


谢谢你的回答,Aman! 我尝试了你的示例,成功创建了一个配置文件,但是我无法使用用户名和密码进行连接。vpnAgent.ConnectProfileWithPasswordCredentialAsync(connectionProfile, new PasswordCredential { UserName = "userName", Password = "password" });我收到了“其他”错误。 - Ionut Holbia
现在可以工作了。问题是在创建配置文件后,我正在使用GetProfilesAsync枚举所有配置文件,然后在找到新创建的配置文件后,我使用用户名和密码调用ConnectProfileWithPasswordCredentialAsync。由于某种原因它不起作用。返回的错误是Other,但事件查看器中的真正错误是87(无效参数)。似乎只有在创建新的VpnNativeProfile配置文件时,ConnectProfileWithPasswordCredentialAsync才能正常工作(我认为这是一个错误)。最终,在每次连接时我都会创建配置文件,在断开连接时删除它。 - Ionut Holbia
很好,您能在Windows 10用户界面中看到VPN连接吗? - Cosmin Ioniță
是的,它可以在控制面板\网络和Internet\网络连接中看到,也可以在设置->网络和Internet->VPN(Metro应用程序)中看到。 - Ionut Holbia
我也遇到了同样的错误,请告诉我该怎么办,谢谢。 如何检查配置文件是否已经创建? - Muhammad Tufail
并告知如何输入 VPN 的用户名和密码。 - Muhammad Tufail

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接