Click here to Skip to main content
15,898,374 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi guys,
Does anyone can help me with this?
I tried to make a new connection using DotRas library,but because of some permission matters in win7,the windows does not allow me to create a entry phonebook.
the code is as follows.
Appreciate for help!
public static void CreateVPNConnection(string connectionName, string serverAddress, string username, string password)
{

            // create vpn connection
            RasDevice device = RasDevice.GetDeviceByName("(L2TP)", RasDeviceType.Vpn, false);
            if (device == null) throw new Exception("Cannot get RasDevice");
            RasEntry entry = RasEntry.CreateVpnEntry(connectionName, serverAddress, RasVpnStrategy.PptpOnly, device);

            // set vpn connection options
            entry.Options.RemoteDefaultGateway = false;
            //entry.Options.IPv6RemoteDefaultGateway = false;
            //entry.Options.ReconnectIfDropped = true;
            entry.Options.ShowDialingProgress = false;
            entry.Options.RequireMSChap2 = true;
            entry.Options.PreviewUserPassword = false;

            // add connection
            RasPhoneBook phoneBook = new RasPhoneBook();
            phoneBook.Open();
            if (phoneBook.Entries.Contains(connectionName))
            {// already exists
                // check if connected
                RasConnection rasConnection = RasConnection.GetActiveConnectionByName(connectionName, phoneBook.Path);
                if (rasConnection != null)
                    rasConnection.HangUp();
                phoneBook.Entries.Remove(connectionName);
            }
            phoneBook.Entries.Add(entry);//You don't have permission               
            // set credentials            
            entry.UpdateCredentials(new System.Net.NetworkCredential(username, password));            
            
            
        }
Posted
Updated 2-Dec-12 20:23pm
v2
Comments
CHill60 3-Dec-12 6:01am    
Might help to be more explicit on the error message you get

please run your application as administrator
 
Share this answer
 
>Hi maybe its too late but for other people add app.manifest file in your solution
you can find this in add add new item
open app.manifest file and replace this code line with this
orignal
<requestedExecutionLevel  level="asInvoker" uiAccess="false" />

Replace
<requestedExecutionLevel level="highestAvailable" uiAccess="false" />
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900