Exemple #1
0
        public async Task Connect(VPNConnection conn)
        {
            VpnManagementAgent mgr = new VpnManagementAgent();

            VpnNativeProfile profile = new VpnNativeProfile()
            {
                AlwaysOn                   = true,
                NativeProtocolType         = VpnNativeProtocolType.Pptp,
                ProfileName                = conn.ConnectionName,
                RememberCredentials        = true,
                RequireVpnClientAppUI      = false,
                RoutingPolicyType          = VpnRoutingPolicyType.ForceAllTrafficOverVpn,
                TunnelAuthenticationMethod = VpnAuthenticationMethod.PresharedKey,
                UserAuthenticationMethod   = VpnAuthenticationMethod.PresharedKey
            };

            profile.Servers.Add(conn.ServerName);

            VpnManagementErrorStatus profileStatus = await mgr.AddProfileFromObjectAsync(profile);

            PasswordCredential credentials = new PasswordCredential
            {
                UserName = conn.UserName,
                Password = conn.Password,
            };

            VpnManagementErrorStatus connectStatus = await mgr.ConnectProfileWithPasswordCredentialAsync(profile, credentials);

            activeProfile = profile;
        }
Exemple #2
0
        public async Task <VpnManagementConnectionStatus> Connect(Server server)
        {
            VpnNativeProfile profile = new VpnNativeProfile()
            {
                AlwaysOn                   = true,
                NativeProtocolType         = VpnNativeProtocolType.IpsecIkev2,
                ProfileName                = Constants.connectionProfileName,
                RememberCredentials        = false,
                RequireVpnClientAppUI      = false,
                RoutingPolicyType          = VpnRoutingPolicyType.ForceAllTrafficOverVpn,
                TunnelAuthenticationMethod = VpnAuthenticationMethod.Eap,
                UserAuthenticationMethod   = VpnAuthenticationMethod.Eap,
                //load eap from xml placed at assembly folder
                EapConfiguration = File.ReadAllText(Path.Combine(Windows.ApplicationModel.Package.Current.Installed­Location.Path, @"profile.xml"))
            };

            profile.Servers.Add(server.serverAddress);

            PasswordCredential credentials = new PasswordCredential
            {
                UserName = server.eap_name,
                Password = server.eap_secret
            };

            VpnManagementErrorStatus profileStatus = await ManagementAgent.AddProfileFromObjectAsync(profile);

            if (profileStatus == VpnManagementErrorStatus.Ok)
            {
                VpnManagementErrorStatus connectStatus = await ManagementAgent.ConnectProfileWithPasswordCredentialAsync(profile, credentials);

                if (connectStatus == VpnManagementErrorStatus.Ok)
                {
                    ActiveProfile = profile;
                    return(profile.ConnectionStatus);
                }
                else
                {
                    throw new Exception("Connetion failed");
                }
            }
            else
            {
                throw new Exception("VPN profile add failed");
            }
        }
Exemple #3
0
 private void SetupNativeProfile(VpnNativeProfile profile)
 {
     if (profile == null)
     {
         return;
     }
     uiType.Text = "Native";
     try
     {
         var connectionStatus = profile.ConnectionStatus;
         uiStatus.Text = StatusIcons[(int)connectionStatus];
         Log($"{profile.ConnectionStatus}");
     }
     catch (Exception ex)
     {
         Log($"ERROR: can't get connection status {ex.Message} :-(");
     }
 }
Exemple #4
0
        public async Task <VpnManagementConnectionStatus> GetStatusAsync()
        {
            var list = await ManagementAgent.GetProfilesAsync();

            foreach (var profile in list)
            {
                if (profile is VpnNativeProfile)
                {
                    var servers = await ConfigurationManager.GetServers();

                    foreach (var server in servers)
                    {
                        if (profile.ProfileName == Constants.connectionProfileName)
                        {
                            VpnNativeProfile nativeProfile = (VpnNativeProfile)profile;
                            try
                            {
                                var status = nativeProfile.ConnectionStatus;
                                if (status == VpnManagementConnectionStatus.Connected)
                                {
                                    ActiveProfile = nativeProfile;
                                    return(status);
                                }
                                return(status);
                            }
                            catch (Exception)
                            {
                                await ManagementAgent.DeleteProfileAsync(nativeProfile);

                                return(VpnManagementConnectionStatus.Disconnected);
                            }
                        }
                    }
                }
            }
            return(VpnManagementConnectionStatus.Disconnected);
        }