How resolve System.ServiceModel.Security.SecurityNegotiationException - winforms

When I Self hosting the WCF service using console application, It will successfully hosted. But i am trying to use in windows forms client application the below exception should be thrown.
System.ServiceModel.Security.SecurityNegotiationException: 'A call to SSPI failed, see inner exception.'
Win32Exception: The system cannot contact a domain controller to service the authentication request. Please try again later.
WCF service:
<services>
<service behaviorConfiguration="MessageBehavior" name="WcfService.MessageService">
<endpoint address="MessageService" binding="netTcpBinding" contract="WcfService.IMessageService" />
<host>
<baseAddresses>
<add baseAddress="localhost:80" />
<add baseAddress="net.tcp://localhost:8090" />
</baseAddresses>
</host>
</service>
</services>
My Base endpoint is : http://localhost:8080
Any help in this ?

Nettcpbinding secures the communication with message security mode by default, and authenticates the client with window authentication.
NetTcpBinding binding = new NetTcpBinding();
binding.Security.Mode = SecurityMode.Message;
binding.Security.Message.ClientCredentialType = MessageCredentialType.Windows;
Therefore, we need to provide windows credentials to call the service.
ServiceReference1.ServiceClient client = new ServiceReference1.ServiceClient();
//the windows credentials are the window accounts on the server-side which hosts the service.
client.ClientCredentials.Windows.ClientCredential.UserName = "administrator";
client.ClientCredentials.Windows.ClientCredential.Password = "123456";
var result = client.Add(34, 56);
Console.WriteLine(result);
Feel free to let me know if the problem still exists.

Related

Errors in add WCF service references?

My WCF project is OK, no errors, I try browse SVC file, it works well.
But when I add this WCF references, it throw errors :
There was an error downloading 'http://MyIPAddress/BKKService/BService.svc/_vti_bin/ListData.svc/$metadata'.
The request failed with HTTP status 404: Not Found.
Metadata contains a reference that cannot be resolved: 'http://MyIPAddress/BKKService/BService.svc'.
There was no endpoint listening at http://MyIPAddress/BKKService/BService.svc that could accept the message.
Please help me to solve this issue.
This is my web.config file
<services>
<service name="BKKService.BService">
<endpoint address="BKKService" binding="basicHttpBinding"
bindingConfiguration="basicHttp" contract="BKKService.IBService"/>
<host>
<baseAddresses>
<add baseAddress="http://MyIPAddress/BKKService/BService.svc"/>
</baseAddresses>
</host>
</service>
</services>
You can specify an absolute address for each endpoint associated with the service or you can provide a base address for the ServiceHost of a service and then specify an address for each endpoint associated with this service that is defined relative to this base address.
When hosting with IIS, you do not manage the ServiceHost instance yourself. The base address is always the address specified in the .svc file for the service when hosting in IIS. So you must use relative endpoint addresses for IIS-hosted service endpoints.
So "http://MyIPAddress/BKKService/BService.svc" would not work, you need to use the .svc file address. If you use IIS express, the address for adding service reference should be the url when you browse the svc file. If you use IIS, it is the address specified for .svc file in IIS.

Accessing a Claim Aware WCF from Silverlight

I am working on a Silverlight 4 application which connects to a claim ware WCF Service. I am using the following code to retrive the claim token in my WCF to perform authorization.
IClaimsPrincipal principal = ( IClaimsPrincipal )Thread.CurrentPrincipal;
IClaimsIdentity identity = ( IClaimsIdentity )principal.Identity;
return string.Format( "You entered: {0} and you are {1}", value, identity.Name );
When I use wsHttpBinding in WCF and try it out with a console app, it works fine. But since Silverlight only supports basicHttp and customeBinding, i cannot use wsHttp, ws2007Http or anyother binding. Becase of which I am not getting the IClaimIdentity token in my WCF from Silverlight.
Is there any way I can use any of the Silverlight suppported binding and still get the ClaimIdentity in my WCF. Is there any tutorial/help text where I can read more abouth this.
My WCF settings are:
<system.serviceModel>
<services>
<service name="ClainAwareWCF.Service" behaviorConfiguration="ClainAwareWCF.ServiceBehavior">
<endpoint address="" binding="basicHttpBinding" contract="ClainAwareWCF.IService" bindingConfiguration="basicbind">
<identity>
<dns value="localhost"/>
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
</service>
</services>
<bindings>
<basicHttpBinding>
<binding name="basicbind">
<security mode="TransportCredentialOnly"></security>
</binding>
</basicHttpBinding>
</bindings>
<behaviors>
<serviceBehaviors>
<behavior name="ClainAwareWCF.ServiceBehavior" >
<federatedServiceHostConfiguration/>
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
</behaviors>
<extensions>
<behaviorExtensions>
<add name="federatedServiceHostConfiguration" type="Microsoft.IdentityModel.Configuration.ConfigureServiceHostBehaviorExtensionElement, Microsoft.IdentityModel, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
</behaviorExtensions>
</extensions>
</system.serviceModel>
Trying to call this directly from the client will never happen because of the binding issues and also because the client auth awareness security of SL (Windows/Forms/WIF/etc.), but one approach is to use RIA Services Domain Authentication Services to authenticate and call the services from the server-side through a WCF RIA Invoke endpoint. The user's security context is proxied to the client and you can tunnel data over the wire in a straight-forward manner.
This may get you in the right direction:
http://archive.msdn.microsoft.com/RiaServices/Release/ProjectReleases.aspx?ReleaseId=5617
Silverlight doesn´t support Claims Based Authorization and WS-Trust out of the box. Microsoft was going to put this into Silverlight 5 but forgot to do so unfortunately.
There is however a very elegant and usable "Silverlight" version of the WIF IdentityModel stuff available in the Identity Training Kit.
The solution consists of a base AuthenticationService that translates WIF authentication tokens to claims server side, and a Silverlight client library "SL.IdentityModel" containing the building blocks such as a Silverlight version of a ClaimsPrincipal.
Get the Identity Training Kit here. Look for the sample Silverlight implementation.

Silverlight + WCF commnication exception

I have a silverlight application which has a WCF in it. when the wcf is called it fails with this exception:
An error occurred while trying to make a request to URI 'http://localhost:4693/MapService.svc'. This could be due to attempting to access a service in a cross-domain way without a proper cross-domain policy in place, or a policy that is unsuitable for SOAP services. You may need to contact the owner of the service to publish a cross-domain policy file and to ensure it allows SOAP-related HTTP headers to be sent. This error may also be caused by using internal types in the web service proxy without using the InternalsVisibleToAttribute attribute. Please see the inner exception for more details.
Both silverlight and WCF are running from local.
My silverlight is running from:
...\SilverlightApplication1\SilverlightApplication1\Bin\Release\SilverlightApplication1TestPage.html
this is the WCF web.config services tag:
<services>
<service behaviorConfiguration="FileUpAndDownload.Web.MapServiceBehavior" name="FileUpAndDownload.Web.MapService">
<endpoint address="" binding="basicHttpBinding" bindingConfiguration="MapBinding" contract="FileUpAndDownload.Web.IMapService">
<identity>
<dns value="localhost"/>
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
</service>
</services>
and this is the ServiceReference.ClientConfig:
<configuration>
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_IMapService" maxBufferSize="2147483647"
maxReceivedMessageSize="2147483647">
<security mode="None" />
</binding>
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://localhost:4693/MapService.svc" binding="basicHttpBinding"
bindingConfiguration="BasicHttpBinding_IMapService" contract="MapService.IMapService"
name="BasicHttpBinding_IMapService" />
</client>
</system.serviceModel>
I've placed the clientaccesspolicy.xml in bin\release but doesn't help.
HELP PLEASE!
You need some extra configuration to do a cross domain call see:http://www.dotnetcurry.com/ShowArticle.aspx?ID=208&AspxAutoDetectCookieSupport=1
You probably have the clientaccesspolicy.xml in the wrong place, check your IIS log for file not found errors.
The client access policy file must be in your wwwroot folder
There are issues with running Silverlight locally as opposed to from a web server.
The access permissions are different and Silverlight won't be able to access ANY webservice.
I ran into that issue somne time ago with SL2 I think, it might have changed.
You should create a new WebApplication project in Visual Studio and host Silverlight in it.
Then you SL client will have no problem connecting to the service.

Exposing WCF Service to Mobile Clients

I have an existing ASP.NET web application. This ASP.NET web application uses JQuery to provide a rich experience to the users. This user interface interacts with the server through some WCF services. A sample service looks like the following:
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
[ServiceBehavior(IncludeExceptionDetailInFaults = false)]
public class myService : ImyService
{
public bool SomeMethod(string parameter1, string parameter2)
{
try
{
return true;
}
catch (Exception ex)
{
return false;
}
}
}
I now want to expose this service to an iPhone and a Windows Phone 7 application. In an attempt to do this, I have configured the service like the following:
<system.serviceModel>
<behaviors>
<endpointBehaviors>
<behavior name="myServiceBehavior">
<enableWebScript />
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
<services>
<service name="myService">
<endpoint address="" behaviorConfiguration="myServiceBehavior"
binding="webHttpBinding" contract="ImyService" />
</service>
</services>
</system.serviceModel>
The service works with the JQuery calls in my ASP.NET web application. I have not begun working on the iPhone client. But, when I try to expose this service to my WP7 client, I run into problems. As it stands now, when I launch my WP7 application, I receive an error that says:
KeyNotFoundException
If I change the binding in the config file to "basicHttpBinding", I cannot reference the service in Visual Studio. I receive an error that says:
The endpoint at 'http://machine:80/services/myService.svc' does not have a Binding with the None MessageVersion.  'System.ServiceModel.Description.WebScriptEnablingBehavior' is only intended for use with WebHttpBinding or similar bindings.
Ugh. How do I move forward? I thought WCF was designed to make this stuff easier. But I feel like I'm getting stuck doing something relatively basic.
Thank you for your help!
As far as I know (but I have no experience at all with iPhone or WinPhone development) you should be able to expose your WCF service using the regular webHttpBinding and no web script functionality, to get a normal REST style WCF service:
<system.serviceModel>
<services>
<service name="myService">
<endpoint
address=""
binding="webHttpBinding"
contract="ImyService" />
</service>
</services>
</system.serviceModel>
That alone should be sufficient - just browse to the virtual directory where your *.svc file is located and you should see your top-level resources in your browser.
The mobile devices typically don't support the SOAP-style bindings (like basicHttpBinding and so on) - so you need to use webHttpBinding instead (since that really only requires an HTTP client stack on the device - and every device these days definitely has this!)

I set UserName and Password in ClientCredentials, but the login window still pops up. Why?

I have a WCF service which is set up to use basic authentication over https like this:
<basicHttpBinding>
<binding name="secureTransport">
<security mode ="Transport">
<transport clientCredentialType="Basic"/>
</security>
</binding>
</basicHttpBinding>
I have also specified a custom username and password validator. When I call this service from a console application, everything works as expected. However, when I call this service from Silverlight 3, I get a login popup. In both cases, the code is the same and is as follows:
SecureRemoteBox.Service1Client client = new SecureRemoteBox.Service1Client();
client.ClientCredentials.UserName.UserName = "test";
client.ClientCredentials.UserName.Password = "pass";
The client security configuration for the console application is
<security mode="Transport">
<transport clientCredentialType="Basic" proxyCredentialType="None" realm="" />
<message clientCredentialType="UserName" algorithmSuite="Default" />
</security>
The client security configuration for the SL3 application is
<security mode="Transport" />
I have also tried "TransportWithMessageCredential" but with the same issue.
What am I doing wrong? Thanks.
I'm afraid to say Silverlight 3 doesn't support HTTP Basic Authentication (annoying I know). Best option for authentication is to use asp.net membership provider and check for authentication in your WCF services or pass the username and password as arguments into your services.
HTH

Resources