Unable to connect to a web service - c#

I am able to access the web service from the web browser but on the same computer if I try to connect to the web service through code I get a The remote server returned an error: (407) Proxy Authentication Required. error.
I have supplied all the credentials required. Here is my code -
MyWebService objWS = new MyWebService(); // my Web service object
// My credentials
System.Net.NetworkCredential cr = new System.Net.NetworkCredential(MyDomainUserName, UserPassword, DomainController);
System.Net.WebProxy pr = new System.Net.WebProxy(ProxyServer, ProxyPort);
pr.Credentials = cr; //Using my credentials for the local proxy
// Web Service credentials
System.Net.NetworkCredential cr1 = new System.Net.NetworkCredential("My_WebService_UserName", "My_WebService_Pwd");
objWS.Credentials = cr1; // Using WebService credentials to the WS object
//objWS.Credentials = CredentialCache.DefaultCredentials; // I even tried Using default credentials but this didn't help.
objWS.Proxy = pr;
Object result = objWS.WebMethod1(param1,param2,param3);
But, this throws an error - The remote server returned an error: (407) Proxy Authentication Required.
Any idea, what am I doing wrong here? Thanks.

You need to update your config file like this to fix this problem.
<system.net>
<defaultProxy useDefaultCredentials="true"> </defaultProxy>
</system.net>
It isn't clear in this blog post, but this isn't either/or. Both things are required to fix your problem.
http://blogs.msdn.com/b/jpsanders/archive/2009/03/24/httpwebrequest-webexcepton-the-remote-server-returned-an-error-407-proxy-authentication-required.aspx

You get this error because you are trying to access the service from behind a proxy.
Try to use this code (you probably only need the last line):
//Set the system proxy with valid server address or IP and port, for example.
System.Net.WebProxy pry = new System.Net.WebProxy("172.16.0.1",8080);
//The DefaultCredentials should be enough.
pry.Credentials = CredentialCache.DefaultCredentials;
GlobalProxySelection.Select = pry;

This means that your proxy needs some credentials(user name and pasword.) Figure out what they are and set them on pry.

Related

LdapConnection Bind() always fails over SSL

I need to query some information with Active Directory that is only accessible when authenticated over SSL. I can make anonymous connections without issue, but I always get an "LDAP server is unavailable error" when trying to use SSL. There's a lot of forum topics about this, and I've reviewed them but have not found a solution. This code is an ASP.NET MVC app being run on IIS Express.
LdapConnection conn = new LdapConnection("ldap.xxx.com:636/OU=xxx,DC=xxx,DC=xxx,DC=xxx");
//conn.AutoBind = false;
conn.SessionOptions.ProtocolVersion = 3;
conn.AuthType = AuthType.Basic; //Tried with Negotiate as well
conn.Credential = new NetworkCredential(#"domain\user", "userPW", "domain");
conn.SessionOptions.SecureSocketLayer = true;
conn.SessionOptions.VerifyServerCertificate = new VerifyServerCertificateCallback((dev, cer) => true);
//conn.Timeout = new TimeSpan(1, 0, 0);
conn.Bind();
I use that container string with a PrincipalContext to validate the credentials for this account, that validation is successful (that takes place before this code). The container places me into the correct node and the account credentials are a match. Which is why this error puzzles me. Protocol version is correct as well. I've set the verifycert callback to return true regardless of the certificate.
The error is being thrown on the first line, if I remove the container it creates the connection, but then hangs indefinitely when I call .Bind(). If I specify the domain after the connection is made, the "unavailable error" is thrown on .Bind(). I don't understand why it fails with the container because it works when passed in with a PrincipalContext.
Thank you for any help.

Providing domain credentials for SQL Server Reporting Service (SSRS) web service?

I'm trying to supply specific domain credentials to access reports on my reporting server. If I use default credentials (don't supply actual credits) it works just fine as I have access to my report server. As soon as I hardcode my credentials (and disable default credentials) it gives me a 401 exception (The request failed with HTTP status 401: Unauthorized.)
comboBox1 is a combobox (obviously) with each server available
urlText is a text field for the web service url
comboBox1.Items.Clear();
var rs = new ReportingService2005
{
Url = urlText.Text,
//any of those three work
//Credentials = System.Net.CredentialCache.DefaultCredentials,
//Credentials = System.Net.CredentialCache.DefaultNetworkCredentials
UseDefaultCredentials = true
//This doesn't work
//Credentials = new NetworkCredential("user", "pass", "domain")
};
// get catalog items from the report server database
var items = rs.ListChildren("/", true); <------exception on this line---------
foreach (var item in items)
{
if (item.Type == ItemTypeEnum.Report)
comboBox1.Items.Add(item.Path);
}
As far as I can tell, that should be all that's needed. It could be something to do with the rsreportserver.config. As it is now, going through the web interface doesn't prompt for credentials (I don't know if that's important or not, just more information).
Here's the authentication section
<Authentication>
<AuthenticationTypes>
<RSWindowsNegotiate/>
</AuthenticationTypes>
<RSWindowsExtendedProtectionLevel>Off</RSWindowsExtendedProtectionLevel>
<RSWindowsExtendedProtectionScenario>Proxy</RSWindowsExtendedProtectionScenario>
<EnableAuthPersistence>true</EnableAuthPersistence>
</Authentication>

Webservice call returns "401 Authorization Required"

I'm calling a third party webservice which uses credentials to validate the request. below is the code.
SmsWSIService smsService = new SmsWSIService();
smsService.Credentials = new NetworkCredential(smsUserName, smsPassword);
smsService.PreAuthenticate = true;
SendSMS.SendSMS objSendSmsReq = new SendSMS.SendSMS();
objSendSmsReq.From = smsFrmMobNo;
objSendSmsReq.Message = smsMsg
objSendSmsResp = smsService.SendSMS(objSendSmsReq);
but its giving me the error "401 Authorization Required". I'm sure that the credentials which i'm passing are correct. The webservice doesn't give any error when I check it with the SOAP UI 3.5.
Please help where I'm going wrong.
Have you installed Windows Authentication on IIS?

Proxy Authentication Required (Forefront TMG requires authorization to fulfill the request. Access to the Web Proxy filter is denied.)

When I was trying to post messages to Twitter, the above error coming. How to get rid of that error?
The stacktrace is the following:
Exception = {"The remote server returned an error: (407) Proxy Authentication Required."} ExceptionStatus = ProtocolError
Code:
private string GetOAuthUrl()
{
IFluentTwitter twitter;
//Override the callback url if one was entered
if (CallbackUrl != null && CallbackUrl.Trim().Length > 0)
{
twitter = FluentTwitter.CreateRequest().Configuration.UseHttps().Authentication.GetRequestToken(ConsumerKey, ConsumerSecret, CallbackUrl);
}
else
{
twitter = FluentTwitter.CreateRequest().Configuration.UseHttps().Authentication.GetRequestToken(ConsumerKey, ConsumerSecret);
}
var response = twitter.Request();
UnauthorizedToken UnauthorizedToken = response.AsToken();
string AuthorizationUrl = FluentTwitter.CreateRequest().Authentication.GetAuthorizationUrl(UnauthorizedToken.Token);
return AuthorizationUrl;
}
If fluent twitter is using WebRequests under the covers, then you need to specify credentials for the proxy using code like this:
System.Net.WebRequest.DefaultWebProxy.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials;
This will tell all web requests to use the credentials of the user running the application to authenticate with the proxy.
To make this work, you will need to configure the application to run under a service account which has been granted access to the proxy server. You can then tie down this service account so that it has as few permissions as possible to run the service.
If your application needs to run under an account which doesn't have rights to use the proxy server, you can specify the credentials explicitly as follows:
System.Net.NetworkCredential credentials = new System.Net.NetworkCredential("username", "password", "domain");
System.Net.WebRequest.DefaultProxy.Credentials = credentials;
The down side to this is that you have to store those credentials somewhere, and that they could be captured by an attacker if they managed to compromise your application. In some environments, this is not acceptable from a security standpoint.

How to upload to YouTube using the API via a Proxy Server

I am building an application which will allow users to upload videos to a specific account on you tube.
I have followed the examples on http://code.google.com/apis/youtube/2.0/developers_guide_dotnet.html for direct upload however I am now getting a 407 proxy authentication required when request.Upload(newVideo) is called.
I've found an example for the Google Calendar Service using a proxy ( http://code.google.com/p/google-gdata/wiki/WebProxySetup )but can't seem to work out how to refactor it for YouTube.
Any ideas?
It sounds like your proxy requires credentials. Credentials have to be supplied in code; I'm currently trawling the source for the Google API to find it, since they have their own custom request objects.
In the mean time, you might get it to work by simply not using the default proxy. Modify your app.config or web.config to insert this at the correct location:
<configuration>
<system.net>
<defaultProxy>
<proxy usesystemdefault="false"/>
</defaultProxy>
</system.net>
</configuration>
Edit:
Ok, after doing some digging, here's how I think you'd refactor the instructions you linked for your specific request. Assuming you've already created a YouTubeRequest as follows:
YouTubeRequest request = new YouTubeRequest(settings);
Here are the refactored instructions from your link:
YouTubeRequest request = new YouTubeRequest(settings);
GDataRequestFactory f = (GDataRequestFactory) request.Service.RequestFactory;
IWebProxy iProxy = WebRequest.DefaultWebProxy;
WebProxy myProxy = new WebProxy(iProxy.GetProxy(query.Uri));
// potentially, setup credentials on the proxy here
myProxy.Credentials = CredentialsCache.DefaultCredentials;
myProxy.UseDefaultCredentials = true;
f.Proxy = myProxy;
Here are my sources:
http://google-gdata.googlecode.com/svn/docs/folder56/T_Google_YouTube_YouTubeRequest.htm
http://google-gdata.googlecode.com/svn/docs/folder53/P_Google_GData_Client_FeedRequest_1_Service.htm
http://google-gdata.googlecode.com/svn/docs/folder19/P_Google_GData_Client_Service_RequestFactory.htm
Using the code provided by Randolpho I managed to get the code to successfully call YouTube. I managed to simplfy the code more to
YouTubeRequest request = new YouTubeRequest(settings);
GDataRequestFactory f = (GDataRequestFactory)request.Service.RequestFactory;
WebProxy myProxy = new WebProxy("http://proxy-server:port/", true);
myProxy.Credentials = CredentialCache.DefaultNetworkCredentials;
f.Proxy = myProxy;
The code will be running with a service account with access to the internet so I shouldn't need to provide a username and password in the code.

Categories