Attempting to set L2tp Only VPN properties in C# - c#

I am attempting to create a console app to create a VPN connection for my company. I am able to create the VPN connection but unable to set a few of the properties. I want Unencrypted password (PAP) to be true and CHAP and CHAP2 to be false. But, the opposite is happening to those settings. I am using DotRas tools. What am i doing wrong or missing?
string VpnName = "Test VPN";
string Destination = "127.0.0.1";
string PresharedKey = "testkey";
RasPhoneBook PhoneBook = new RasPhoneBook();
PhoneBook.Open();
RasEntry VpnEntry = RasEntry.CreateVpnEntry(VpnName, Destination, DotRas.RasVpnStrategy.L2tpOnly, DotRas.RasDevice.Create(VpnName, DotRas.RasDeviceType.Vpn));
VpnEntry.Options.UsePreSharedKey = true;
VpnEntry.Options.UseLogOnCredentials = false;
VpnEntry.Options.RequirePap = true;
VpnEntry.Options.RequireMSChap = false;
VpnEntry.Options.RequireMSChap2 = false;
PhoneBook.Entries.Add(VpnEntry);
VpnEntry.UpdateCredentials(RasPreSharedKey.Client, PresharedKey);
Console.WriteLine("VPN connection created successfully");

You can change the three security checkboxes using a combination of options.
VpnEntry.Options.RequireEncryptedPassword = false;
VpnEntry.Options.RequirePap = true;
VpnEntry.Options.RequireChap = false;
VpnEntry.Options.RequireMSChap = false;
VpnEntry.Options.RequireMSChap2 = false;
Those options will have PAP checked, CHAP unchecked, and MS-CHAP v2 unchecked.

You can connect using the the Windows building dialing command "rasdial.exe", using some code like this :
rasDialFileName = Path.Combine(WinDir, "rasdial.exe");
try
{
string args = $"{connectionName} {userName} {passWord}";
ProcessStartInfo myProcess = new ProcessStartInfo(rasDialFileName, args);
myProcess.CreateNoWindow = true;
myProcess.UseShellExecute = false;
Process.Start(myProcess);
}
catch (Exception Ex)
{
Debug.Assert(false, Ex.ToString());
}

Related

Android webview hangs/crash after continues running (2-3 days)

I have created one Xamarin.forms application that runs URL in webview. The app is a digital signage player, So Android TV/Box will run forever.
I found that after 2-3 days the webview either became unstable/hang/crash. I have tried almost all the types of flags but none of them are working.
Some android TV/Box uses com.google.android.webview and some com.android.webview.
Video Android box (Android 7.1 API 25): https://drive.google.com/file/d/1EU7Vf39wFxStNCGJPHyk519G4ifhSEUw/view?usp=sharing
Log (Check Time stamp: 10-21 14:32:36.278 16584): https://drive.google.com/file/d/16bt1vKYNo02Rm9GQA_Hw-PUV36vDvif3/view?usp=sharing
Anyone have any solution?
MyWebClient client = new MyWebClient();
MyWebChromeClient client2 = new MyWebChromeClient();
webView.SetWebViewClient(client);
webView.SetWebChromeClient(client2);
webView.Settings.JavaScriptEnabled = true;
WebSettings webSettings = webView.Settings;
if (Android.OS.Build.VERSION.SdkInt > BuildVersionCodes.Kitkat)
{
webSettings.BuiltInZoomControls = true;
webSettings.SetSupportZoom(false);
webSettings.DisplayZoomControls = false;
if (Element.AspectRatio)
webView.SetInitialScale(100);
}
webSettings.SetPluginState(WebSettings.PluginState.On);
webSettings.SaveFormData = false;
webSettings.SavePassword = false;
webSettings.AllowFileAccess = true;
webSettings.CacheMode = CacheModes.Default;
webSettings.DatabaseEnabled = true;
webSettings.DomStorageEnabled = true;
webSettings.AllowContentAccess = true;
webSettings.AllowFileAccessFromFileURLs = true;
webSettings.AllowUniversalAccessFromFileURLs = true;
webSettings.BlockNetworkImage = false;
webSettings.BlockNetworkLoads = false;
webSettings.JavaScriptEnabled = true;
webSettings.LoadWithOverviewMode = true;
webSettings.UseWideViewPort = true;
webSettings.SetAppCachePath(this.Context.CacheDir.AbsolutePath);
webSettings.SetAppCacheEnabled(true);
webSettings.LoadsImagesAutomatically = true;
webSettings.MixedContentMode = MixedContentHandling.AlwaysAllow;
webSettings.SetEnableSmoothTransition(true);
if (Build.VERSION.SdkInt >= BuildVersionCodes.Kitkat)
{
//This flag is for the android box as those are rooted. It's the same displayed on the above video link.
if (MainActivity.IsRootedDevice)
webView.SetLayerType(LayerType.Software, null);
else
webView.SetLayerType(LayerType.Hardware, null);
}
else
{
// older android version, disable hardware acceleration
webView.SetLayerType(LayerType.Software, null);
}

RemoteApp session opens in a separate window instead of inside a WPF UserControl

I am writing a WPF UserControl that hosts a RemoteApp session using the AxMSTSCLib library. I'm using the information from this SO question. The code I wrote is very close to the answer of that question.
I placed the RemoteApp ActiveX (axMsRdpClient9NotSafeForScripting1) inside my WPF UserControl, and provide the necessary parameters when connecting.
But the problem is that the WPF Window that contains the UserControl opens but is blank. The RemoteApp actually launches but in a separate window, outside the main WPF Window. How can I ensure the remote app opens inside the WPF UserControl? What am I missing?
Here is my code:
public void Connect()
{
try
{
axMsRdpClient9NotSafeForScripting1.UserName = #"domain\username";
axMsRdpClient9NotSafeForScripting1.Domain = "GTCS";
axMsRdpClient9NotSafeForScripting1.AdvancedSettings7.ClearTextPassword = "password";
axMsRdpClient9NotSafeForScripting1.AdvancedSettings9.AuthenticationLevel = 2;
axMsRdpClient9NotSafeForScripting1.AdvancedSettings9.EnableCredSspSupport = true;
axMsRdpClient9NotSafeForScripting1.AdvancedSettings9.NegotiateSecurityLayer = false;
axMsRdpClient9NotSafeForScripting1.AdvancedSettings9.RDPPort = 3389;
axMsRdpClient9NotSafeForScripting1.Server = "GT-DZ1-ATLS.GTCS.LOCAL"; // 10.24.141.199
axMsRdpClient9NotSafeForScripting1.RemoteProgram2.RemoteProgramMode = true;
axMsRdpClient9NotSafeForScripting1.OnConnected += (o, e) =>
{
m_connectionState = axMsRdpClient9NotSafeForScripting1.Connected;
((ITSRemoteProgram)((IMsRdpClient9)axMsRdpClient9NotSafeForScripting1.GetOcx()).RemoteProgram).ServerStartProgram(#"||startchrome", "", "", true, "", false);
//axMsRdpClient9NotSafeForScripting1.RemoteProgram.ServerStartProgram(#"||startchrome", "", "", true, "", false);
};
axMsRdpClient9NotSafeForScripting1.AdvancedSettings7.PublicMode = false;
axMsRdpClient9NotSafeForScripting1.DesktopWidth = SystemInformation.VirtualScreen.Width;
axMsRdpClient9NotSafeForScripting1.DesktopHeight = SystemInformation.VirtualScreen.Height;
axMsRdpClient9NotSafeForScripting1.AdvancedSettings9.SmartSizing = true;
IMsTscNonScriptable secured = (IMsTscNonScriptable)axMsRdpClient9NotSafeForScripting1.GetOcx();
secured.ClearTextPassword = "password";
axMsRdpClient9NotSafeForScripting1.AdvancedSettings7.ClearTextPassword = "password";
axMsRdpClient9NotSafeForScripting1.AdvancedSettings9.RedirectClipboard = true;
axMsRdpClient9NotSafeForScripting1.AdvancedSettings9.RedirectPrinters = true;
axMsRdpClient9NotSafeForScripting1.AdvancedSettings9.RedirectPorts = false;
axMsRdpClient9NotSafeForScripting1.AdvancedSettings9.RedirectSmartCards = true;
axMsRdpClient9NotSafeForScripting1.AdvancedSettings9.RedirectDrives = true;
axMsRdpClient9NotSafeForScripting1.Visible = true;
axMsRdpClient9NotSafeForScripting1.Enabled = true;
axMsRdpClient9NotSafeForScripting1.Connect();
m_connectionState = axMsRdpClient9NotSafeForScripting1.Connected;
}
catch (Exception ex)
{
var err = ex.Message;
MessageBox.Show(ex.Message);
}

C#: Problems connecting from a PC to an FTP through a gateway

I am making a Windows Form Program where I can be able to connect to an FTP Server to download data to show it to the user. I have to make the app in a way that the user can also connect through a gateway. I made the next function to log on through a gateway:
private Boolean addRoute(string ip, string gw)
{
string arg = String.Format("ADD {0} MASK 255.255.255.255 {1}", ip, gw);
return startProcess("route.exe", arg, 10000, true);
}
private Boolean startProcess(String fileName, String arguments, int timeout, bool admin)
{
try
{
Process process = new Process();
process.StartInfo.FileName = fileName;
if (System.Environment.OSVersion.Version.Major >= 6)
{
//if (admin) { process.StartInfo.UserName = "admin"; }
//process.StartInfo.UserName = "admin";
}
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.UseShellExecute = false;
process.StartInfo.Arguments = arguments;
process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
process.Start();
if (timeout > 0)
{
if (process.WaitForExit(timeout))
{
return true;
}
else
{
return false;
}
}
else
{
process.WaitForExit();
return true;
}
}
catch (Exception e)
{
Global.LogMessageToFile(e.Message);
return false;
}
}
This code works perfect with my PC but when I test it in other PC's with Windows 7 doesn't work anymore.
I thought there was a problem with the UAC permissions so I did the solution adapted to the second answer of the link Selectively disabling UAC for specific programs on Windows Programatically but it doesn't seem to be the problem.
Do you have any other ideas about how can I accomplish this with the code of my program?
EDIT
I have been blocked to post comments, so I will answer that I have no exceptions, and what I need is connect to a lower level, from one network to another, through a gateway. That's why I have adopted this solution.

Hosting a WCF service from within a Windows Service. cannot be reached

So I have a windows service running as Local System.
this windows service then starts off a WCF service.
From my machine there is no problem and works fine.
From a test console application, on the target machine, it works fine
From a windows service, on the target machine, it does not work. Nor does it throw an exception...
I am really stuck on this. :(
Could this be permissions?
m_tknCancelToken = new CancellationTokenSource();
/**************************************************************************************/
/*** Create and start the task ***/
/**************************************************************************************/
m_tskService = Task.Factory.StartNew((object o) =>
{
RunService();
},
m_tknCancelToken);
/**************************************************************************************/
/*** Set the handler when the task is cancelled or faulted ***/
/**************************************************************************************/
m_tskService.ContinueWith(
TaskEndedHandler,
TaskContinuationOptions.OnlyOnFaulted);
m_tskService.ContinueWith(
TaskEndedHandler,
TaskContinuationOptions.OnlyOnCanceled);
and then to catch the errors.
private void TaskEndedHandler(Task tskTask)
{
Log.Log(String.Format("{0} has ended", ServiceName), "WHS010CI");
if (tskTask.Exception != null)
{
Log.LogEx(tskTask.Exception, "WHS0103E");
if (tskTask.Exception.InnerExceptions != null)
{
foreach (Exception ex in tskTask.Exception.InnerExceptions)
{
Log.LogEx(ex, "WHS0104E");
}
}
}
if(tskTask.IsCanceled)
{
Log.Log(String.Format("[{0}] has been cancelled", ServiceName), "WHS0104W");
}
}
As usual it was a stupid mistake.
In my console application I was binding an SSL certificate to a port, this was removed as the powers that be did not want this in production code, which is understandable. So I removed it to then have a seperate batch file or otherwise which has to be manually run... however this is what I forgot to do. :(
for those that are interested, below is the code from my test app.
process = new Process();
process.StartInfo = BindCertToPort(port, certificate);
process.Start();
method:
private static ProcessStartInfo BindCertToPort(int iPort, X509Certificate2 certificate, bool bRemove = false)
{
string strAction = null;
string strExtraArguments = null;
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo.FileName = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.SystemX86), "netsh.exe");
if (bRemove)
{
strAction = "delete";
}
else
{
strAction = "add";
strExtraArguments = string.Format(" certhash={0} appid={{{1}}}", certificate.Thumbprint, Guid.NewGuid());
}
startInfo.Arguments = string.Format("http {0} sslcert ipport=0.0.0.0:{1}{2}", strAction, iPort, strExtraArguments);
startInfo.RedirectStandardOutput = true;
startInfo.UseShellExecute = false;
startInfo.CreateNoWindow = true;
return startInfo;
}

Getting wifi Signal strength

Is there a way to get wifi signal strength in C#? Currently I'm getting the same through
Process proc = new Process();
proc.StartInfo.CreateNoWindow = true;
proc.StartInfo.FileName = "netsh";
proc.StartInfo.Arguments = "wlan show interfaces";
proc.StartInfo.RedirectStandardOutput = true;
proc.StartInfo.UseShellExecute = false;
proc.Start();
and then I get the wifi signal strength by reading the output. Is there a better way? Preferably using API's
Why don't you use a WMI query to get it in a clean way ?
private double RetrieveSignalString()
{
double theSignalStrength = 0;
ConnectionOptions theConnectionOptions = new ConnectionOptions();
ManagementScope theManagementScope = new ManagementScope("root\\wmi");
ObjectQuery theObjectQuery = new ObjectQuery("SELECT * FROM MSNdis_80211_ReceivedSignalStrength WHERE active=true");
ManagementObjectSearcher theQuery = new ManagementObjectSearcher(theManagementScope, theObjectQuery);
try
{
//ManagementObjectCollection theResults = theQuery.Get();
foreach(ManagementObject currentObject in theQuery.Get())
{
theSignalStrength = theSignalStrength + Convert.ToDouble(currentObject["Ndis80211ReceivedSignalStrength"]);
}
}
catch (Exception e)
{
//handle
}
return Convert.ToDouble(theSignalStrength);
}
Please see this for more info.
http://social.msdn.microsoft.com/Forums/en-US/34a66ee5-34f8-473d-b6f2-830a14e2300b/get-signal-strength-in-c

Categories