【发布时间】:2026-02-05 08:05:02
【问题描述】:
您好,我正在使用 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))'
堆栈跟踪:
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
【问题讨论】:
-
最好也共享堆栈跟踪。
-
我更新了我的问题并添加了 stackTrace
标签: c# visual-studio vpn