System.UnauthorizedAccessException: 'Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))'

0

Hello I am working on VPN in C# this is my code

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);

i add all this code on button action. now when i start VPN connection this line throw exception

VpnManagementErrorStatus profileStatus = await mgr.AddProfileFromObjectAsync(profile);

Exception is

 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
c#
visual-studio
vpn
asked on Stack Overflow May 28, 2020 by Muhammad Tufail • edited May 28, 2020 by Muhammad Tufail

1 Answer

1

I believe you need to add the App Capability "networkingVpnProvider" as per the below:

VPN App

This link describes the significance and how to apply App Capabilities MSDN App Capability. Ultimately you will need to add the below to your app package manifest source file (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>

Note that this is a Windows 10 requirement as per the documentation for the method: VpnManagementAgent.AddProfileFromObjectAsync(IVpnProfile) Method

answered on Stack Overflow May 28, 2020 by George Kerwood

User contributions licensed under CC BY-SA 3.0