We all know it’s not possible yet to provide credentials when calling WCF Service. I’m telling yet, because I saw some signs that we might get support for credentials.
But what about now? Yes now, because Silverlight 3 isn’t release to the web yet.
Let’s think of all those applications that are running in the Intranet Zone for example. If they are built on the .NET environment, they often make use of Windows Authentication to authenticate the user. And after that make use of the roles assigned to the user to authorize the user.
But when we are working in the Silverlight environment we don’t really have the ability to make use of the User that’s already authentication against the Active Directory. But what about the services that Silverlight is using? Well that’s basically what this article is about.
Windows Authentication on WCF
A few days ago I read an article about Windows Authentication on WCF. This article explains the different steps to get Windows Authentication on WCF very well. But for the sake of this article I will summarize the steps that are required.
- Create your WCF Service
- Ensure authentication mode is Windows by adding <authentication mode="Windows" /> to the web.config
- Create the binding in the system.servicemodel element of the web.config just like this.
<bindings>
<basicHttpBinding>
<binding name="BasicHttpEndpointBinding">
<security mode="TransportCredentialOnly">
<transport clientCredentialType="Windows" />
</security>
</binding>
</basicHttpBinding>
</bindings>
- Make sure the WCF service configuration makes use of the binding created in step 3 by adding the following to the endpoint element for your WCF service.
<endpoint … bindingConfiguration="BasicHttpEndpointBinding" />
- The article mentions to disable anonymous access and enable Windows Authentcation. But to make it work on Windows Server 2008 I had to make sure anonymous access was enabled as well.
- If you want authentication to be automated you can do this easily by adding the url of your service to the local intranet zone.
- Just add a service reference like you would normally do from Silverlight.
- Create a very simple method to know if it’s working as expected. Something like:
[OperationContract]
public string Hello()
{
// Add your operation implementation here
return string.Format("Hello, {0} at {1}", HttpContext.Current.User.Identity.Name, DateTime.Now);
}
And yes that just works. And this enables more things as well like ask if the user is in a specific role. Basically all the things you’re used to have access to in a Windows Authenticated ASP.NET application are available.
I think at least part of the credits for this article should go to Shivprasad koirala who wrote the Code Project article I refer to.
Let’s hope the next version of Silverlight enables credentials so we can make use of different authentication scenario’s as well.
Ps. This article is cross posted on: Mark Monster’s blog and Silverlight Help.