Darrell Hawley: Home Page

Thursday, October 05, 2006

WSE: Not-So-Minor Details

I know. So many posts in such a short amount of time. It's crazy. But if you want this to work, there are a couple of steps you still have to take. By the end of this post, the client should be able to connect to the webservice using WSE. It won't be secure yet, but we will have the basis for a secure line of communication. Let's get started.

First let's take a look at a simple webservice that I setup in my sample project.


[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[Policy("MyPolicy")]
public class Service : System.Web.Services.WebService
{

public Service()
{
//Uncomment the following line if using designed components
//InitializeComponent();
}

#region Basic
[WebMethod]
public string HelloWorld()
{
return "Hello, World";
}

#endregion

#region WSE

[WebMethod]
public string HelloWSE()
{
SecurityToken token = RequestSoapContext.Current.IdentityToken;
if (token != null)
{
string Name = token.Identity.Name;
return String.Format("Hello, {0}", Name);
}
return null;
}

#endregion
}


If you go simply add a new webservice to your web project, it will give you the skeleton with all of the absolutely necessary attributes - almost all of them, anyways - in the right places. We just need to add the PolicyAttribute located in the Microsoft.Web.Services3 namespace to the webservice class. We are looking for the attribute that takes the string arguement. What do we put here? Do you remember the name of the policy you entered here? Pass that name - type-friendly, of course - to the PolicyAttribute.


Next, open up you web.config file in the web project. If you followed my directions, you should see something that looks like the following:



<securitytokenmanager>
<add type="WebService.TokenManager, Microsoft.Web.Services3, Version=3.0.0.0,
Culture=neutral, PublicKeyToken=31bf3856ad364e35"
namespace="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-
secext-1.0.xsd" localName="UsernameToken" />
</securitytokenmanager>


We need to make this look like this:



<securitytokenmanager>
<add localName="UsernameToken" type="WebService.TokenManager"
namespace="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-
secext-1.0.xsd"/>
</securitytokenmanager>


The localname attribute is the type-friendly name of the token that will be passed to our token manager and the type attribute is the namespace and class of my token manager.



That is the end of the tweaking phase, next we'll setup the client to consume the service using WSE.

0 Comments:

Post a Comment

<< Home