12.5 Set Authentication Credentials for an XML Web Service


Problem

You want an XML Web service client to submit logon credentials for IIS authentication.

Solution

Use the Credentials property of the proxy class. You can create a new NetworkCredential object by specifying a password and username, or use the CredentialCache to retrieve the credentials for the current user .

Discussion

XML Web services, like Web pages, can be used in conjunction with IIS authentication. All you need to do is place your XML Web services in a virtual directory that restricts anonymous access. Unauthenticated calls to any XML Web services in this directory will fail. However, if the user can submit credentials that map to a valid user account, the user will be authenticated and you'll be able to retrieve the authentication information through the built-in WebService.User object.

Unlike Web pages, XML Web services have no built-in method for retrieving authentication information from the client because XML Web services are executed by other applications, not directly by the user. Thus, the application that's interacting with the XML Web service bears the responsibility for submitting any required authentication information.

The following XML Web service provides a simple user authentication test. GetIISUser returns the user that was authenticated by IIS. If anonymous access is allowed, the result will be an empty string because no authentication will be performed. If anonymous access is denied , the result will be a string in the form [DomainName]\[UserName] or [ComputerName]\[UserName].

 public class AuthenticationTest : System.Web.Services.WebService {     // Retrieves the authenticated IIS user.     [WebMethod()]     public string GetIISUser() {         return User.Identity.Name;     } } 

The final step is to create a client that can submit the authentication information. The credentials are submitted through the Credentials property of the proxy object. You set this Credentials property in the same way that you set the WebRequest.Credentials property when retrieving a Web page. See recipe 11.3 for a full discussion.

The following code snippet shows how a client can access an XML Web service that uses basic authentication by creating a new System.Net.NetworkCredential object that specifies the user name and password information.

 // Create the proxy. localhost.AuthenticationTest proxy = new localhost.AuthenticationTest(); // Create the credentials. proxy.Credentials = new System.Net.NetworkCredential(   "myUserName", "myPassword"); Console.WriteLine(proxy.GetIISUser()); 

Here's how you would use similar code to call an XML Web service that uses Microsoft Windows integrated authentication with the credentials from the currently logged on user:

 // Create the proxy. localhost.AuthenticationTest proxy = new localhost.AuthenticationTest(); // Assign the current user's credentials to the proxy class. proxy.Credentials = System.Net.CredentialCache.DefaultCredentials; Console.WriteLine(proxy.GetIISUser()); 



C# Programmer[ap]s Cookbook
C# Programmer[ap]s Cookbook
ISBN: 735619301
EAN: N/A
Year: 2006
Pages: 266

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net