You can also use SOAP headers to transfer user credentials or an authentication token. You can find an article on how to do this on Authentication for Web Services (using SOAP headers) , but to summarize, you create a header class:
using System.Web.Services.Protocols; public class AuthHeader : SoapHeader { public string Username; public string Password; }
You define a public property in the web service
public AuthHeader AuthenticationInfo;
and add some attributes to any web methods that you would like to use only for authenticated users:
[SoapHeader ("AuthenticationInfo", Required=true)] [WebMethod] public string HelloSecretWorld() { if(!(AuthenticationInfo.UserName == "Hello" && AuthenticationInfo.UserName.Password == "World")) throw new AuthenticationException(); return "Hello World"; }
Client code will look like this:
MyWebService ws = new MyWebService(); ws.AuthenticationInfo = new AuthHeader {Username = "Hello", Password = "World"}; Console.Out.WriteLine(ws.HelloSecretWorld());
This way you do not need to change the method signatures to add authentication.
MichaΕ Drozdowicz
source share