How to: Specify Client Credentials for a Data Service Request (WCF Data Services)
By default, the client library does not supply credentials when sending a request to an OData service. However, you can specify that credentials be sent to authenticate requests to the data service by supplying a NetworkCredential for the Credentials property of the DataServiceContext. For more information, see Securing WCF Data Services. The example in this topic shows how to explicitly provide credentials that are used by the WCF Data Services client when requesting data from the data service.
The example in this topic uses the Northwind sample data service and autogenerated client data service classes. This service and the client data classes are created when you complete the WCF Data Services quickstart. You can also use the Northwind sample data service that is published on the OData Web site; this sample data service is read-only and attempting to save changes returns an error. The sample data services on the OData Web site allow anonymous authentication.
The following example is from the code-behind page for an Extensible Application Markup Language (XAML) file that is the main page of the Windows Presentation Framework application. This example displays a LoginWindow instance to collect authentication credentials from the user, and then uses these credentials when making a request to the data service.
The following XAML defines the main page of the WPF application.
The following example is from the code-behind page for the window that is used to collect the authentication credentials from the user before making a request to the data service.
The following XAML defines the login of the WPF application.
.NET Framework Security
The following security considerations apply to the example in this topic:
To verify that the credentials supplied in this sample work, the Northwind data service must use an authentication scheme other than anonymous access. Otherwise, the Web site hosting the data service will not request credentials.
User credentials should only be requested during execution and should not be cached. Credentials must always be stored securely.
Data sent with Basic and Digest Authentication is not encrypted, so the data can be seen by an adversary. Additionally, basic authentication credentials (user name and password) are sent in cleartext and can be intercepted.
For more information, see Securing WCF Data Services.