你好,我在用C#做VPN,这是我的代码
VpnManagementAgent vpnManagementAgent = new VpnManagementAgent();
VpnManagementAgent mgr = vpnManagementAgent;
VpnNativeProfile profile = new VpnNativeProfile()
{
AlwaysOn = false,
NativeProtocolType = VpnNativeProtocolType.IpsecIkev2,
ProfileName = "MyConnection",
RememberCredentials = true,
RequireVpnClientAppUI = true,
RoutingPolicyType = VpnRoutingPolicyType.SplitRouting,
TunnelAuthenticationMethod = VpnAuthenticationMethod.Certificate,
UserAuthenticationMethod = VpnAuthenticationMethod.Mschapv2,
};
profile.Servers.Add("serveAddress");
VpnManagementErrorStatus profileStatus = await mgr.AddProfileFromObjectAsync(profile);
PasswordCredential credentials = new PasswordCredential
{
UserName = "username",
Password = "Abc",
};
VpnManagementErrorStatus connectStatus = await mgr.ConnectProfileWithPasswordCredentialAsync(profile, credentials);
我将所有这些代码添加到按钮操作中。现在,当我启动VPN连接时,这一行将抛出异常
VpnManagementErrorStatus profileStatus = await mgr.AddProfileFromObjectAsync(profile);
例外是
System.UnauthorizedAccessException: 'Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))'
StackTrace:
at Windows.Networking.Vpn.VpnManagementAgent.AddProfileFromObjectAsync(IVpnProfile profile)
at App1.MainPage.<Button_Click>d__2.MoveNext() in C:\Users\HP\source\repos\StarkVPnTesting\App1\MainPage.xaml.cs:line 62
发布于 2020-05-28 11:26:07
我相信您需要添加应用程序功能"networkingVpnProvider“,如下所示:
此链接描述了应用程序功能MSDN应用程序功能的重要性和如何应用。最终,您需要将下面的内容添加到应用程序包清单源文件(Package.appxmanifest)中。
<?xml version="1.0" encoding="utf-8"?>
<Package
...
xmlns:rescap="http://schemas.microsoft.com/appx/manifest/foundation/windows10/restrictedcapabilities"
IgnorableNamespaces="... rescap">
...
<Capabilities>
<rescap:Capability Name="networkingVpnProvider"/>
</Capabilities>
</Package>
注意,根据方法的文档:VpnManagementAgent.AddProfileFromObjectAsync(IVpnProfile)法,这是Windows 10的要求
https://stackoverflow.com/questions/62062788
复制相似问题