Microsoft KB Archive/813834

= HOW TO: Pass Current Credentials to an ASP.NET Web Service =

Article ID: 813834

Article Last Modified on 3/14/2007

-

APPLIES TO


 * Microsoft Web Services Enhancements for Microsoft .NET 2.0
 * Microsoft ASP.NET 1.0
 * Microsoft Visual C# .NET 2002 Standard Edition
 * Microsoft Visual Basic .NET 2002 Standard Edition
 * Microsoft ASP.NET 1.1
 * Microsoft Visual C# .NET 2003 Standard Edition
 * Microsoft Visual Basic .NET 2003 Standard Edition

-



This article references the following .NET Framework Class Library namespace:

System.Net



IN THIS TASK

 * SUMMARY
 * Create the Web Service
 * Set Integrated Windows Authentication for the Web Service
 * Use the Web Service
 * Pass Current Credentials to the Web Service
 * REFERENCES



SUMMARY
This step-by-step article describes how to pass the user's current credentials to an XML Web service that was created by using ASP.NET.

The DefaultCredentials property of the CredentialCache class contains the system credentials of the current security context. For client applications, these credentials represent the user name, the password, and the domain of the user who is currently logged on. Client credentials are not passed automatically. To pass the client's Windows security context to a Web service, you must set the Credentials property of the Web service proxy to CredentialCache.DefaultCredentials.

back to the top

Create the Web Service
 Start Microsoft Visual Studio .NET. Create a new ASP.NET Web Service project by using Visual C# .NET or Visual Basic .NET. By default, Service1.asmx is created. Name the project MyWebService. In Solution Explorer, right-click Service1.asmx, and then click View Code. In the Service1.asmx.cs file (or the Service1.asmx.vb file if you used Visual Basic .NET), remove the comment on the default WebMethod HelloWorld. On the Build menu, click Build Solution. Type the following URL in your browser to view the Service1 Web service description:

http://localhost/MyWebService/Service1.asmx

 To test the HelloWorld WebMethod, click the HelloWorld link. Notice that the WebMethod works as expected.</li></ol>

back to the top

Set Integrated Windows Authentication for the Web Service

 * 1) Click Start, point to Settings, and then click Control Panel.
 * 2) In Control Panel, double-click Administrative Tools.
 * 3) Double-click Internet Information Services.
 * 4) Expand Internet Information Services, and then locate the MyWebService virtual directory.
 * 5) Right-click MyWebService, and then click Properties.
 * 6) Click the Directory Security tab. Under Anonymous access and authentication control, click Edit.
 * 7) In the Authentication Methods dialog box, click to select the check box for Integrated Windows authentication.

back to the top

Use the Web Service
<ol> Create a new ASP.NET Web Application by using Visual C# .NET or Visual Basic .NET. Name the project WebServiceTest.</li> In Solution Explorer, right-click References, and then click Add Web Reference.</li> In the Address text box, type the following URL for WebServiceTest:

http://localhost/MyWebService/Service1.asmx

</li> Click Go, and then click Add Reference.</li> In Solution Explorer, right-click WebForm1.aspx, and then click View Code.</li>  In the Design View of WebForm1, double-click WebForm1 to open the Page_Load event code. Change the Page_Load event code as follows:

Visual C# .NET Sample Code private void Page_Load(object sender, System.EventArgs e) { // Start an instance of the Web Service client-side proxy. localhost.Service1 myProxy = new localhost.Service1; Response.Write( myProxy.HelloWorld); } Visual Basic .NET Sample Code Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

'Start an instance of the Web Service client-side proxy. Dim myProxy As localhost.Service1 = New localhost.Service1 Response.Write(myProxy.HelloWorld) End Sub </li> On the Build menu, click Build Solution.</li> Type the following URL in the browser to view the Service1 Web service description:

http://localhost/WebServiceTest/WebForm1.aspx

</li> You may receive an Access Denied error message. This occurs because your credentials are not delivered with the Web service request for authentication.</li></ol>

back to the top

Pass Current Credentials to the Web Service
The CredentialCache class belongs to the System.Net namespace. <ol>  Add the following namespace declaration to the top of the file:

Visual C# .NET Sample Code using System.Net; Visual Basic .NET Sample Code Imports System.Net </li>  Assign DefaultCredentials to the Credentials property of the Web service client-side proxy. To do this, change the code of the Page_Load event as follows:

Visual C# .NET Sample: private void Page_Load(object sender, System.EventArgs e) { // Start an instance of the Web service client-side proxy. localhost.Service1 myProxy = new localhost.Service1; myProxy.Credentials = System.Net.CredentialCache.DefaultCredentials; Response.Write( myProxy.HelloWorld); } Visual Basic .NET Sample Code Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

'Start an instance of the Web service client-side proxy. Dim myProxy As localhost.Service1 = New localhost.Service1 myProxy.Credentials = System.Net.CredentialCache.DefaultCredentials Response.Write(myProxy.HelloWorld) End Sub </li> On the Debug menu, click Start. Hello World appears in the browser.</li></ol>

back to the top

<div class="references_section">