Self hosted WCF with Windows Authentication - c#

I've got a windows service, that hosts WCF service inside.
Below is definition of how endpoint is initialized:
WebHttpBinding binding = new WebHttpBinding(WebHttpSecurityMode.TransportCredentialOnly);
binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Windows;
string url = ConfigurationManager.AppSettings["serviceUrl"];
Uri baseAddress = new Uri(url);
Uri metadataAddress = new Uri(url + "Description");
CallService service = new CallService(controller);
ServiceHost host = new WebServiceHost(service);
host.AddServiceEndpoint(typeof (ICallService), binding, baseAddress);
host.Authorization.PrincipalPermissionMode = PrincipalPermissionMode.UseWindowsGroups;
ServiceDebugBehavior stp = host.Description.Behaviors.Find<ServiceDebugBehavior>();
stp.HttpHelpPageEnabled = true;
ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
smb.HttpGetEnabled = true;
smb.MetadataExporter.PolicyVersion = PolicyVersion.Policy15;
smb.HttpGetUrl = metadataAddress;
host.Description.Behaviors.Add(smb);
host.Open();
The problem is that when accesing this WCF service using computer name (e.g http://mycomputer.mydomain.int:4544/Somthing) first I receive an authentication window, and after it HTTP:400.
Everything works fine if accessing service using http://localhost:4544/Something, or if switch off Windows Authentication.
What is wrong with it?

Related

Getting error while Authenticating online TFS from WCF service

I am trying to connect online TFS using WCF service but it is throwing me exception "TF30063: You are not authorized to access https://abdul-r.visualstudio.com/DefaultCollection/TestTFS.".
Below is my sample code
NetworkCredential netCred = new NetworkCredential(
"*MyEmail*",
"*MyPassword*");
BasicAuthCredential basicCred = new BasicAuthCredential(netCred);
TfsClientCredentials credential = new TfsClientCredentials(basicCred);
credential.AllowInteractive = false;
string TFSServerPath = "https://abdul-r.visualstudio.com/DefaultCollection/TestTFS";
using (TfsTeamProjectCollection tfs1 = new TfsTeamProjectCollection(new Uri(TFSServerPath), credential))
{
tfs1.EnsureAuthenticated();
}
Any help would be appreciated.

Webservice HttpStatusCode 500 but not in browser

When I start a new HttpBasicBinding to a web service which is not initiated yet, because the app pool is not started will result in an Internal Server Error 500. But when I start the Web Service from the browser the service is being started.
How can I start the web service before sending a new request?
Below my code:
BasicHttpBinding binding = new BasicHttpBinding();
binding.Security.Mode = BasicHttpSecurityMode.TransportCredentialOnly;
binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Windows;
binding.Name = "AXDocumentBinding";
EndpointAddress address = new EndpointAddress(serviceUrl);
AA_DynamicsAXDocuHandlingClient cls = new AA_DynamicsAXDocuHandlingClient(binding, address);
cls.ClientCredentials.Windows.ClientCredential = new NetworkCredential(username, password, domain);
string retVal = cls.loadDocumentStr(AreaId, documentFileName, documentIdentification1, documentIdentification2, documentIdentification3, documentIdentification4, documentIdentification5, documentTable);
Thanks in advance

webdriver - Setting a proxy for IE in C#

Is it possible to set a proxy programatically in webdriver for IE?
For Chrome, I do something like this:
ChromeOptions options = new ChromeOptions();
Proxy proxy = new Proxy();
proxy.HttpProxy = "http://proxy.com:8080";
proxy.SslProxy = "http://proxy.com:8080";
options.AddAdditionalCapability("proxy", proxy);
but this doesn't work for IE. I've also tried: options.AddAdditionalCapability(CapabilityType.Proxy, proxy); which doesn't work. Is there a comparable capability for IE?
Have you tried the latest IEDriverServer (version 2.34.0.0)? This is feature is very new to the IEDriverServer. The .NET bindings now expose the Proxy via the InternetExplorerOptions class.
This is the change log: https://code.google.com/p/selenium/source/detail?r=084758c6b515a2699b82c6bf5871e29b552cbc8f
You should be able to do the exact same thing now after updating the IEDriverServer and .NEt bindings:
InternetExplorerOptions options = new InternetExplorerOptions();
Proxy proxy = new Proxy();
proxy.HttpProxy = "http://proxy.com:8080";
proxy.SslProxy = "http://proxy.com:8080";
options.Proxy = proxy;
String PROXY = url://login:pass#proxy:port";
ChromeOptions options = new ChromeOptions();
options.AddArguments("user-data-dir=path/in/your/system");
Proxy proxy = new Proxy();
proxy.HttpProxy = PROXY;
proxy.SslProxy = PROXY;
proxy.FtpProxy = PROXY;
options.Proxy = proxy;
// Initialize the Chrome Driver
using (var driver = new ChromeDriver(options))

(407) Proxy Authentication Required ( The ISA Server requires authorization to fulfill the request. Access to the Web Proxy filter is denied

I know that this error is very common, but I tried to apply the solutions to this problem and could not solve it.
Thats my code:
var endpoint = new EndpointAddress(new Uri("http://www3prz.bancobonsucesso.com.br/Servicos/app.svc"), EndpointIdentity.CreateDnsIdentity("bancobonsucesso.com.br"));
var binding = new WSHttpBinding();
binding.UseDefaultWebProxy = true;
binding.Security.Mode = SecurityMode.Message;
binding.Security.Message.ClientCredentialType = MessageCredentialType.UserName;
binding.Security.Message.EstablishSecurityContext = true;
binding.Security.Message.NegotiateServiceCredential = true;
var customBinding = new CustomBinding(binding);
SymmetricSecurityBindingElement security = customBinding.Elements.Find<SymmetricSecurityBindingElement>();
security.LocalClientSettings.MaxClockSkew = TimeSpan.MaxValue;
security.LocalClientSettings.DetectReplays = false;
SecureConversationSecurityTokenParameters secureTokenParams = (SecureConversationSecurityTokenParameters)security.ProtectionTokenParameters;
SecurityBindingElement bootstrap = secureTokenParams.BootstrapSecurityBindingElement;
bootstrap.LocalClientSettings.MaxClockSkew = TimeSpan.MaxValue;
bootstrap.LocalClientSettings.DetectReplays = false;
ws = new ServicoClient(customBinding, endpoint);
ws.ClientCredentials.ServiceCertificate.Authentication.CertificateValidationMode = X509CertificateValidationMode.None;
ws.ClientCredentials.UserName.UserName = "test";
ws.ClientCredentials.UserName.Password = "test";
var return = ws.EmitirBoleto("test");
IN the WCF Binding use useDefaultWebProxy:
<bindings>
<basicHttpBinding>
<binding name="bindingName" useDefaultWebProxy="true">
WebProxy wproxy = new WebProxy("new proxy",true);
wproxy.Credentials = new NetworkCredential("user", "pass");
WebRequest.DefaultWebProxy = wproxy;
This error may arise problem with proxy settings.
Please check Proxy setting with your web browser. Just changed connection settings Option -> settings -> connection settings to Auto detect proxy settings
Good luck ...

WCF class implementing multiple service contracts

I have a class TestService which implements two service contracts called IService1 and IService2. But I'm facing a difficulty in implementation.
My code looks as follows:
Uri baseAddress = new Uri("http://localhost:8000/ServiceModel/Service");
Uri baseAddress1 = new Uri("http://localhost:8080/ServiceModel/Service1");
ServiceHost selfHost = new ServiceHost(typeof(TestService));
selfHost.AddServiceEndpoint(typeof(IService1), new WSHttpBinding(), baseAddress);
selfHost.AddServiceEndpoint(typeof(IService2), new WSHttpBinding(), baseAddress1);
ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
smb.HttpGetEnabled = true;
selfHost.Description.Behaviors.Add(smb);
selfHost.Open();
Console.WriteLine("The service is ready.");
Console.WriteLine("Press <ENTER> to terminate service.");
Console.WriteLine();
Console.ReadLine();
selfHost.Close();
I'm getting a run time error as:
The HttpGetEnabled property of
ServiceMetadataBehavior is set to true
and the HttpGetUrl property is a
relative address, but there is no http
base address. Either supply an http
base address or set HttpGetUrl to an
absolute address.
What can I do about it? Do I really need two separate endpoints?
you can fix it in two ways
1)
Uri baseAddress = new Uri("http://localhost:8000/ServiceModel");
ServiceHost selfHost = new ServiceHost(typeof(TestService), baseAdress);
selfHost.AddServiceEndpoint(typeof(IService1), new WSHttpBinding(), "Service");
selfHost.AddServiceEndpoint(typeof(IService2), new WSHttpBinding(), "Service1");
ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
smb.HttpGetEnabled = true;
selfHost.Description.Behaviors.Add(smb);
2)
ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
smb.HttpGetEnabled = true;
smb.HttpGetUrl = new Uri("http://localhost:8000/ServiceModel");
selfHost.Description.Behaviors.Add(smb);
All you need to do is to add an base address.
you still have two separated endpoints.
ServiceHost selfHost = new ServiceHost(typeof(TestService), new Uri ("http://localhost:8080/ServiceModel"));

Categories

Resources