1

嗨,我正在尝试创建 IPSEC VPN 连接并自动连接,但即使我定义了共享密钥,它也会给出 No VPN shared secret was provided 错误。我的代码如下所示,它正在保存 VPN 配置但无法连接。

我正在尝试使用相同的参数从我的 iPhone 添加 VPN 配置并且它正在连接。

[[NEVPNManager sharedManager] setEnabled:YES];
[[NEVPNManager sharedManager] loadFromPreferencesWithCompletionHandler: ^(NSError *error) {



    NEVPNProtocolIPSec *p = [[NEVPNProtocolIPSec alloc] init];
    p.serverAddress =@"server";
    p.authenticationMethod = NEVPNIKEAuthenticationMethodSharedSecret;
    p.useExtendedAuthentication = YES;

    NSString *secret = @"secretCode";
    NSData *secretData = [secret dataUsingEncoding:NSUTF8StringEncoding];


    p.sharedSecretReference=secretData;
    p.localIdentifier=@"identifier";
    p.username=@"username";
    p.passwordReference=[[SSKeychain passwordForService:@"service" account:@"passwordReference"] dataUsingEncoding:NSUTF8StringEncoding];
    p.disconnectOnSleep = NO;


    [NEVPNManager sharedManager].protocolConfiguration=p;

    NSMutableArray *rules = [[NSMutableArray alloc] init];
    NEOnDemandRuleConnect *connectRule = [NEOnDemandRuleConnect new];
    [rules addObject:connectRule];
    [[NEVPNManager sharedManager] setOnDemandRules:rules];
    [[NEVPNManager sharedManager] setLocalizedDescription:@"vpn"];

    [[NEVPNManager sharedManager] setEnabled:YES];



    [[NEVPNManager sharedManager] saveToPreferencesWithCompletionHandler: ^(NSError *error) {

        NSLog(@"Save VPN to preference complete");
        if (error) {
            NSLog(@"Save error: %@", error);
        }

    }];

    NSError *startError;
    [[NEVPNManager sharedManager].connection startVPNTunnelAndReturnError:&startError];
    if(startError) {
        NSLog(@"Start error: %@", startError.localizedDescription);
    }
4

1 回答 1

1

我遇到了同样的错误,这个例子对我很有帮助https://github.com/lexrus/VPNOn,我认为你应该在这些点上提供正确和有效的钥匙串引用: p.passwordReference = [VPN user password from keychain];

p.sharedSecretReference = [VPN server shared secret from keychain];

于 2016-03-21T06:12:40.530 回答