首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >System.UnauthorizedAccessException:“访问被拒绝。( HRESULT: 0x80070005 (E_ACCESSDENIED)例外)”

System.UnauthorizedAccessException:“访问被拒绝。( HRESULT: 0x80070005 (E_ACCESSDENIED)例外)”
EN

Stack Overflow用户
提问于 2020-05-28 10:44:02
回答 1查看 1.8K关注 0票数 0

你好,我在用C#做VPN,这是我的代码

代码语言:javascript
运行
复制
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连接时,这一行将抛出异常

代码语言:javascript
运行
复制
VpnManagementErrorStatus profileStatus = await mgr.AddProfileFromObjectAsync(profile);

例外是

代码语言:javascript
运行
复制
 System.UnauthorizedAccessException: 'Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))'

StackTrace:

代码语言:javascript
运行
复制
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
EN

Stack Overflow用户

回答已采纳

发布于 2020-05-28 11:26:07

我相信您需要添加应用程序功能"networkingVpnProvider“,如下所示:

此链接描述了应用程序功能MSDN应用程序功能的重要性和如何应用。最终,您需要将下面的内容添加到应用程序包清单源文件(Package.appxmanifest)中。

代码语言:javascript
运行
复制
<?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的要求

票数 1
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62062788

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档