813834 HOW TO: Passing the current credentials to ASP.NET Web Services (from mkba)

xiaoxiao2021-03-06  59

This article quoted the following .NET Framework class library namespace:

SYSTEM.NET

This task content

summary

Create a web service Settings for Web Services Settings Windows Verification Use Web Services to pass the current credentials to web service reference

SUMMARY This step guide describes how to pass the user's current credentials to XML web services created using ASP.NET.

Credentialcache

The defaultcredentials property contains system credentials for current security context. For client applications, these credentials represent the domain where the username, password, and the current login user are located. Client credentials do not pass automatically. To pass the client's Windows security context to the web service, you must use the web service agent

The Credentials property is set to

CredentialCache.defaultcredentials.

Back to top

Create a web service

Start Microsoft Visual Studio .NET. Create a new "ASP.NET Web Services" item using Visual C # .NET or Visual Basic .NET. Service1.asmx will be created by default. Name the project MyWebService. In the Solution Explorer, right-click "Service1.asmx" and click View Code. In the "service1.asmx.cs" file (if you are using Visual Basic .NET, "Service1.asmx.vb" files, delete the default WebMethod HelloWorld () annotation. On the Generate menu, click Generate Solutions. Type the URL below in your browser to view the service1 web service instructions: http://localhost/mywebservice/service1.asmx To test HelloWorld WebMethod, click the HelloWorld link. You will see that the behavior of WebMethod is the same as you expect.

Back to top

Integrate Windows Verification for Web Services

Click Start, point to Setting, and then click Control Panel. In the control panel, double-click Administrative Tools. Double-click "Internet Information Service". Expand "Internet Information Service" and find the "MyWebService" virtual directory. Right-click "MyWebService" and click Properties. Click the Directory Security tab. Under "Anonymous Access and Verification Control", click Edit. In the Verification Method dialog box, click to select the "Integrated Windows Verification" checkbox.

Back to top

Use web services

Create a new "ASP.NET Web Application" using Visual C # .NET or Visual Basic .NET. Name the project WebServiceTestSt. In the Solution Explorer, right-click "Reference", then click Add Web Reference. In the Address text box, type the URL: http://localhost/mywebservice/service1.asmx for WebServiceTestSt, click "GO", and then click Add Reference. In the Solution Explorer, right-click WebForm1.aspx, and then click View Code. Double-click "WebForm1" in WebForm1's Design View to open the Page_Load event code. Change 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 On the Generate menu, click Generate Solution. Type the URL below in your browser to view the Service1 Web Service Description: http://localhost/webservicetest/webform1.aspx You may receive an "access denied" error message. The reason for this error message is that when authentication is performed, no credentials are provided with the Web service request.

Back to top

Passing the current credentials to the Web Services "CredentialCache" class belongs to the "System.Net" namespace.

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 Assign DefaultCredentials to the Credentials property of the Web service client agent. To do this, change the code of the page_load event as shown below: 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 (); 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

On the Debug menu, click Start. "Hello World" will appear in the browser.

Back to top

Refer to additional information, click the article number below to see the article in the Microsoft Knowledge Base:

811318 PRB: "Access Denied" Error Message When You Call A Web Service While Anonymous Authentication IS TURNED OFF For more information, please visit the following Microsoft website:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnnetsec/html/secnetlpmsdn.asp

Back to top

The information in this article applies to:

Microsoft Web Services (including the .NET Framework) 1.0 Microsoft ASP.NET (Included with the .NET Framework) 1.0 Microsoft Visual C # .NET (2002) Microsoft Visual Basic .NET (2002)

Recent Updated: 2003-6-12 (1.0) Keyword kbwebservices kbwebforms Kbsecurity Kbauthentication KbhowTomaster KB813834 KBAUDDEVELOPER

转载请注明原文地址:https://www.9cbs.com/read-108974.html

New Post(0)