2 s ylli1 s_ylli1 于 2013.11.13 00:39 提问

C# 代码VPN建立连接时候怎么设置属性:安全:数据加密:可选加密

CSDN移动问答
设置成这样子

我是用网上找的dotras的代码来建立vpn连接的。。英语水平不太好,不会把数据加密选线那里改为可选加密

1.using DotRas;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace IP
{
class VPNHelper
{

    // 系统路径 C:\windows\system32\
    private static string WinDir = Environment.GetFolderPath(Environment.SpecialFolder.System) + @"\";

    // rasdial.exe
    private static string RasDialFileName = "rasdial.exe";

    // VPN路径 C:\windows\system32\rasdial.exe
    private static string VPNPROCESS = WinDir + RasDialFileName;

    // VPN地址
    public string IPToPing { get; set; }

    // VPN名称
    public string VPNName { get; set; }

    // VPN用户名
    public string UserName { get; set; }

    // VPN密码
    public string PassWord { get; set; }


    public VPNHelper()
    {

    }

    /// <summary>
    /// 带参构造函数
    /// </summary>
    /// <param name="_vpnIP"></param>
    /// <param name="_vpnName"></param>
    /// <param name="_userName"></param>
    /// <param name="_passWord"></param>
    public VPNHelper(string _vpnIP, string _vpnName, string _userName, string _passWord)
    {
        this.IPToPing = _vpnIP;
        this.VPNName = _vpnName;
        this.UserName = _userName;
        this.PassWord = _passWord;
    }

    /// <summary>
    /// 尝试连接VPN(默认VPN)
    /// </summary>
    /// <returns></returns>
    public void TryConnectVPN()
    {
        this.TryConnectVPN(this.VPNName,this.UserName,this.PassWord);
    }

    /// <summary>
    /// 尝试断开连接(默认VPN)
    /// </summary>
    /// <returns></returns>
    public void TryDisConnectVPN()
    {
        this.TryDisConnectVPN(this.VPNName);
    }

    /// <summary>
    /// 创建或更新一个默认的VPN连接
    /// </summary>
    public void CreateOrUpdateVPN()
    {
        this.CreateOrUpdateVPN(this.VPNName, this.IPToPing);
    }

    /// <summary>
    /// 尝试删除连接(默认VPN)
    /// </summary>
    /// <returns></returns>
    public void TryDeleteVPN()
    {
        this.TryDeleteVPN(this.VPNName);
    }
    /// <summary>
    /// 尝试连接VPN(指定VPN名称,用户名,密码)
    /// </summary>
    /// <returns></returns>
    public void TryConnectVPN(string connVpnName,string connUserName,string connPassWord)
    {
        try
        {
            string args = string.Format("{0} {1} {2}", connVpnName, connUserName, connPassWord);

            ProcessStartInfo myProcess = new ProcessStartInfo(VPNPROCESS, args);

            myProcess.CreateNoWindow = true;

            myProcess.UseShellExecute = false;

            Process.Start(myProcess);

        }
        catch (Exception Ex)
        {
            Debug.Assert(false, Ex.ToString());
        }
    }

    /// <summary>
    /// 尝试断开VPN(指定VPN名称)
    /// </summary>
    /// <returns></returns>
    public void TryDisConnectVPN(string disConnVpnName)
    {
        try
        {
            string args = string.Format(@"{0} /d", disConnVpnName);

            ProcessStartInfo myProcess = new ProcessStartInfo(VPNPROCESS, args);

            myProcess.CreateNoWindow = true;

            myProcess.UseShellExecute = false;

            Process.Start(myProcess);

        }
        catch (Exception Ex)
        {
            Debug.Assert(false, Ex.ToString());
        }
    }

    /// <summary>
    /// 创建或更新一个VPN连接(指定VPN名称,及IP)
    /// </summary>
    public void CreateOrUpdateVPN(string updateVPNname,string updateVPNip)
    {
        RasDialer dialer = new RasDialer();
        RasPhoneBook allUsersPhoneBook = new RasPhoneBook();

        allUsersPhoneBook.Open();

        // 如果已经该名称的VPN已经存在,则更新这个VPN服务器地址
        if (allUsersPhoneBook.Entries.Contains(updateVPNname))
        {
            allUsersPhoneBook.Entries[updateVPNname].PhoneNumber = updateVPNip;
            // 不管当前VPN是否连接,服务器地址的更新总能成功,如果正在连接,则需要VPN重启后才能起作用
            allUsersPhoneBook.Entries[updateVPNname].Update();                
        }
        // 创建一个新VPN
        else
        {
            RasEntry entry = RasEntry.CreateVpnEntry(updateVPNname, updateVPNip, RasVpnStrategy.L2tpOnly, RasDevice.GetDeviceByName("(L2TP)", RasDeviceType.Vpn));

            allUsersPhoneBook.Entries.Add(entry);

            dialer.EntryName = updateVPNname;

            dialer.PhoneBookPath = RasPhoneBook.GetPhoneBookPath(RasPhoneBookType.AllUsers);

        }
    }

    /// <summary>
    /// 删除指定名称的VPN
    /// 如果VPN正在运行,一样会在电话本里删除,但是不会断开连接,所以,最好是先断开连接,再进行删除操作
    /// </summary>
    /// <param name="delVpnName"></param>
    public void TryDeleteVPN(string delVpnName)
    {
        RasDialer dialer = new RasDialer();
        RasPhoneBook allUsersPhoneBook = new RasPhoneBook();
        allUsersPhoneBook.Open();
        if (allUsersPhoneBook.Entries.Contains(delVpnName))
        {
            allUsersPhoneBook.Entries.Remove(delVpnName);
        }
    }

    /// <summary>
    /// 获取当前正在连接中的VPN名称
    /// </summary>
    public List<string> GetCurrentConnectingVPNNames()
    {
        List<string> ConnectingVPNList = new List<string>();

        Process proIP = new Process();

        proIP.StartInfo.FileName = "cmd.exe ";
        proIP.StartInfo.UseShellExecute = false;
        proIP.StartInfo.RedirectStandardInput = true;
        proIP.StartInfo.RedirectStandardOutput = true;
        proIP.StartInfo.RedirectStandardError = true;
        proIP.StartInfo.CreateNoWindow = true;//不显示cmd窗口 
        proIP.Start();

        proIP.StandardInput.WriteLine(RasDialFileName);
        proIP.StandardInput.WriteLine("exit");

        // 命令行运行结果
        string strResult = proIP.StandardOutput.ReadToEnd();
        proIP.Close();

        Regex regger = new Regex("(?<=已连接\r\n)(.*\n)*(?=命令已完成)", RegexOptions.Multiline);

        // 如果匹配,则说有正在连接的VPN
        if (regger.IsMatch(strResult))
        {
            string[] list = regger.Match(strResult).Value.ToString().Split('\n');
            for(int index = 0 ; index < list.Length;index++)
            {
                if (list[index]!=string.Empty)
                ConnectingVPNList.Add(list[index].Replace("\r",""));
            }
        }
        // 没有正在连接的VPN,则直接返回一个空List<string>
        return ConnectingVPNList;
    }
}

}

1个回答

s_ylli1
s_ylli1   2013.11.25 17:02
已采纳
        VPN建立连接时候怎么设置属性:安全:数据加密:可选加密

##
RasEntry entry = RasEntry.CreateVpnEntry(updateVPNname, updateVPNip,RasVpnStrategy.L2tpOnly,RasDevice.GetDeviceByName("(L2TP)", RasDeviceType.Vpn));

entry.EncryptionType = RasEncryptionType.Optional;//少了这个,这个就是设置属性:安全:数据加密:可选加密
allUsersPhoneBook.Entries.Add(entry);
dialer.EntryName = updateVPNname;
dialer.PhoneBookPath = RasPhoneBook.GetPhoneBookPath(RasPhoneBookType.AllUsers); ##

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!