Connecting to Office365 via a Proxy server - c#

I need to connect to Office365 portal using powershell via a proxy server. I read an article here which says calling:
New-PSSessionOption -ProxyAccessType IEConfig
with IEConfig should do the trick.
But, is there a way that I can give proxy server without having to rely on IEConfig. i.e is there any other way to generate a -ProxyAccessType ? Something on the lines
$proxyserver = New-Object ProxyAccessType(server ip ...)
and then use the proxyserver in New-PSSessionOption -ProxyAccessType $proxyserver

From http://technet.microsoft.com/en-us/library/hh849703.aspx the supported values are
IEConfig, WinHttpConfig, AutoDetect, NoProxyServer and None. The default value is None.
WinHTTPConfig can be set using netsh http://technet.microsoft.com/en-us/library/cc731131%28v=ws.10%29.aspx
AutoDetect will use the wpad protocol to determine proxy settings http://en.wikipedia.org/wiki/Web_Proxy_Autodiscovery_Protocol which can be kludged with a wpad file
Good Luck!

Related

C# selenium/phantomjs set global proxy

I need to setup phantomjs to use a proxy. I have found many examples for java, javascript/node.js, etc. But none for c# using selenium. I need to set a global proxy and be able to change it without restarting the driver. I believe it would be under driver.ExecutePhantomJS() but I also cant find what phantomjs script I would run to change the global proxy.
When creating the service:
OpenQA.Selnium.Proxy myproxy = new Proxy();
myproxy.httpProxy = ip +":"+port;
driverservice.AddAdditionalCapability(CapabilityType.Proxy, myproxy);
After driver is running to change the proxy:
driver.ExecutePhantomJS("phantom.setProxy('"+ip+"', "+port+", 'http', '', '');")

Unable to access host created by Nancy using external IP

I am creating a Nancy Module that will eventually be hosted inside of a Windows Service. To start the Nancy hosting, I am using Nancy.Hosting.Self. Below is the code to start Nancy host.
string strHostProtocol = Convert.ToString(ConfigurationManager.AppSettings["HostProtocol"]);
string strHostIP = Convert.ToString(ConfigurationManager.AppSettings["HostIP"]);
string strHostPort = Convert.ToString(ConfigurationManager.AppSettings["HostPort"]);
//Here strHostProtocol="https", strHostIP = "192.168.100.88" i.e. System IPv4, strHostPort = "9003"
var url = strHostProtocol + "://" + strHostIP + ":" + strHostPort;
//url ="https://192.168.100.88:9003"
this.host = new NancyHost(new Uri(url));
this.host.Start();
Now once the windows service starts, it will start the above host and I could see this in netstat -a command. When I browse this in browser using https://192.168.100.88:9003 I will get proper response.
The problem arises when the same is browsed using its external IP. Say this system has been assigned with external IP of 208.91.158.66 and when I try browsing this as https://208.91.158.66:9003 I will just get a browser default loading progress continuosly which does not stop and without any error thrown. I have also added the below command and reserved URL successfully.
netsh http add urlacl url=https://192.168.100.88:9003/ user=everyone
But even after this the host cannot be browsed using external IP assigned to that system. Is there any restricting Nancy is putting up? Firewalls are turned off, defenders are turned off. Anyone has any idea on this?
UPDATE
The duplicate linked question talks about LAN but here I am trying through external IP and I've tried answer mentioned over there and also specified the same in question
Alright. This issue was also posted to GitHub Nancy Repo and below is what #Khellang had to say.
When you bind to https://192.168.100.88:9003, the
TcpListener/HttpListener won't listen on other interfaces. You either
have to bind to https://208.91.158.66:9003 or https://localhost:9003
and set RewriteLocalhost = true (default).
Further he also said that
If you also want to listen to requests coming to the external IP, yes.
Or you could use a wildcard, like https://+:9003/, https://*:9003/ or
https://localhost:9003/ (with RewriteLocalhost = true, this will
result in https://+:9003/). You can read more about them in the link I
posted.
and thanks to #TimBourguignon as he suggested the same in his comments. Hope this helps someone in future.
He has also suggested to read this link to know more about the Strong Wildcard and Weak Wildcard

Use of local pac file

I need to use a local pac file when the user is on a vpn.
The problem is that I do not understand how I will get the program to read it. From what I've read .net should be able to pick it up on start from the IE settings. The user can access the web using IE but my program does not work when on vpn, so I guess the pac file is not read. I have set the useDefaultWebProxy to true in that test.
When setting the wcf client proxy address to file:///path/file.pac I get an error that it cannot access it and when searching it can only access http:// so I guess this parameter is not going to work for me.
Any ideas on any workaround?

Get original url without non-standard port (C#)

First question!
Environment
MVC, C#, AppHarbor.
Problem
I am calling an openid provider, and generating an absolute callback url based on the domain.
On my local machine, this works fine if I hit http://localhost:12345/login
Request.Url; //gives me `http://localhost:12345/callback`
However, on AppHarbor where I'm deploying, because they are using non-standard ports, even if I'm hitting it at "http://sub.example.com/login"
Request.Url; //gives me http://sub.example.com:15232/callback
And this screws up my callback, because the port number wasn't in the original source url!
I've tried
Request.Url
Request.Url.OriginalString
Request.RawUrl
All gives me "http://sub.example.com:15232/callback".
Also to clear up that this isn't a Realm issue, the error message I am getting from DotNetOpenAuth is
'http://sub.example.com:14107/accounts/openidcallback' not under realm 'http://*.example.com/'.
I don't think I've stuffed that up?
Now, I'm about to consider some hacky stuff like
preprocessor commands (#IF DEBUG THEN PUT PORT)
string replace (Request.URL.Contains("localhost"))
All of these are not 100% solutions, but I'm sick of mulling over what could be a simple property that I am missing. I have also read this but that doesn't seem to have an accepted answer (and is more about the path rather than the authority). So I'm putting it towards you guys.
Summary
So if I had http://localhost:12345/login, I need to get http://localhost:12345/callback from the Request context.
And if I had "http://sub.example.com/login", I should get "http://sub.example.com/callback", regardless of what port it is on.
Thanks! (Sleep time, will answer any questions in the morning)
This is a common problem in load balanced setups like AppHarbor's - we've provided an example workaround.
Update: A more desirable solution for many ASP.NET applications may be to set the aspnet:UseHostHeaderForRequestUrl appSetting to true. We (AppHarbor) have seen several customers experience issues using it with their WCF apps, which is why we haven't enabled it by default and stil recommend the above solution for those situations. You can configure it using AppHarbor's "Configuration Variables" to inject the appsettings when deployed. More information can be found in this article.
I recently ran into an issue where I compared a URL to the current URL, and then highlighted navigation based on that. It worked locally, but not in production.
I had http://example.com/path/to/file.aspx as my file, but when viewing that file and running Request.Url.ToString() it produced https://example.com:81/path/to/file.aspx in a load balanced production environment.
Now I am using Request.Url.AbsolutePath to just give me /path/to/file.aspx, thus ignoring the schema, hostname, and port numbers.
When I need to compare it to the URL on each navigation item I used:
New Uri(theLink.Href).AbsolutePath
My initial thoughts are get the referrer variable and check if that includes a port, if so use it otherwise don't.
If that’s not an option because a proxy might remove the referrer header variable then you might need to use some client side script to get the location and pass it back to the server.
I'm guessing that AppHarbor use port forwarding to the IIS server so even though publicly the site is on port 80 IIS has it hosted on another port so it can't know what port the client connected on.
Something like
String port = Request.ServerVariables["SERVER_PORT"] == "80" ? "" : ":" + Request.ServerVariables["SERVER_PORT"];
String virtualRoot = Url.Content("~/");
destinationUrl = String.Format("http://{0}{1}{2}", Request.ServerVariables["SERVER_NAME"], port + virtualRoot, "/callback");
If you use the UrlBuilder class in the framework you can easly get around this. On the builder class if you set the port to -1 then the port number will be removed:
new UriBuilder("http://sub.example.com:15232/callback"){ Port = -1}
returns : http://sub.example.com/callback
To keep the port number on a local machine just check Request.IsLocal and don't apply -1 to the port.
I would wrap this into a extension method to keep it clean.
I see that this is an old thread. I had this issue running MVC5, on IIS 7.5, with an Apache proxy in front. Outside of the server, I would get "Empty Response", since the asp.net app gets the Url from apache with the custom port.
In order to have the app redirect to a subpath without including the "custom" port, forget the Response/Request objects, and use the Transfer method. For instance, if I want that users are automatically redirected to the login page in case they are not logged already:
if (!User.Identity.IsAuthenticated)
Server.TransferRequest("Account/Login");

How to connect to TFS via proxy programmatically?

At my company we have recently set up a TeamFoundation proxy to our TeamFoundationServer. I have written a C# app, that connects to the TFS to query various things. Now, I want the app to support connection over the TFS proxy as well. As I am not really familiar with TFS, I am having some difficulties. Ideally, I want the application to only "know" the TFS proxy and have it act just like the normal TFS. Is this even possible?
What I am doing is something like this:
TfsTeamProjectCollection projects =
new TfsTeamProjectCollection(new Uri(serverUriString,
new NetworkCredential(username, password, domain));
This works fine if serverUriString is the TFS (e.g. "http://MyTfs:8080"). When I substitute this with the TFS proxy (e.g. "http://MyTfsProxy:8081") I get some unspecific TeamFoundationServiceUnavailableException, where at the end it states that a http 404 error occurred. The 404 doesn't make much sense to me, I am able to ping the server, I can connect to it from the browser and Visual Studio acceppts it as well. Do I need to set a connection to the TFS AND the proxy? If yes, how do I do that?
The AddProxy() method is used to register a list of proxy servers with the TFS server, so that clients can automatically detect & use a proxy server.
If you just want to configure your client to use a proxy server, there is no property to do this. You have to set a registry key or an undocumented environment variable.
For TFS2008 clients, the registry key is:
[HKEY_CURRENT_USER\Software\Microsoft\VisualStudio\9.0\TeamFoundation\SourceControl\Proxy]
"Enabled"="True"
"Url"="http://someproxy:8081"
For TFS2010 clients, the registry key is:
[HKEY_CURRENT_USER\Software\Microsoft\VisualStudio\10.0\TeamFoundation\SourceControl\Proxy]
"Enabled"="True"
"Url"="http://someproxy:8081"
In either TFS version, you can set the undocumented environment variable:
System.Environment.SetEnvironmentVariable("TFSPROXY",http://someproxy:8081);
You can do this:
TfsTeamProjectCollection server = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(new Uri(serverName));
server.EnsureAuthenticated();
var versionControlServer = server.GetService<VersionControlServer>();
versionControlServer.ConfigureProxy(proxyName);

Categories

Resources