???? ID: 813834 - ????? ???????: 05 ?????? 2010 - ??????: 2.0

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

?????? ??????This article applies to a different operating system than the one you are using. Article content that may not be relevant to you is disabled.
This article references the following .NET Framework Class Library namespace:
System.Net

?? ????? ??

??? ?? ??????? ???? | ??? ?? ??????? ????

??????

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.

TheDefaultCredentials?? ???CredentialCacheclass contains the system credentials of the current security context. ??????? ?????????, ?? ??? ?? ????????????? ????????? ?????????? ???, ???????, ?? ?? ??????? ??? ??? ?? ?????????? ?? ?????? ??????? ??????????? ???????? ??? ?? ??? ???? ???? ??? ??? ???? ?? ??? Windows ??????? ?????? ??????? ?? ???, ???? ?? ??? ?? ??? ???? ??????????????????? ?? ??? ??? ???? ???????? ?? ???CredentialCache.DefaultCredentials.


??? ???? ?????

  1. Microsoft Visual Studio .NET ???? ????.. ??? ??? ?????ASP.NET ??? ????????????? ????? C# .NET ?? Visual Basic .NET ?? ????? ???? ??? ??? ???????? ??? ??, Service1.asmx ?? ???? ???
  2. ????????? MyWebService ??? ???
  3. ?????? Explorer ???, ???? ?????Service1.asmx?? ????-????? ????, ?? ???? ???????? ???.
  4. ?????Service1.asmx.cs????? (??Service1.asmx.VB??????? WebMethod ?? ??????? ????? ??? ?? Visual Basic .NET ?? ?????) ?? ???????HelloWorld().
  5. ????? ????????????? ??,??????? ??????.
  6. ????? ?? ??? ???? ???????? ??? ????? URL ?????Service1??? ???? ?? ?????:
    HTTP://localhost/MyWebService/Service1.asmx
  7. ??????? ???? ?? ???HelloWorldWebMethod, ????? ????HelloWorld????. ????? ??? ?? WebMethod ???????????? ??? ???

??? ???? ?? ??? Windows ?????? ??????? ??? ????

  1. ????? ????,????????? ????? ????,?????????? ????-????? ????, ?? ???? ??????????? ????.
  2. ???????? ???? ??? ??? ????? ???????????????? ?????.
  3. ???-????? ??????????? ????? ??????.
  4. ??????? ??????????? ????? ??????, ?? ???? ??? ?? ?????? ?????MyWebService??????? ???????????
  5. ????-????? ????MyWebService?? ????-????? ????, ?? ???? ??????.
  6. ????? ?????????????? ???????TAB. ?? ?? ???????? ????? ?? ??????? ????????????? ????,??????.
  7. ???????????? ???????????? ????? ???, ?? ??? ??? ????? ?? ??? ???? ?? ??? ????? ?????????? Windows ??????????.

??? ???? ?? ????? ????

  1. ??? ??? ?????ASP.NET ??? ??????????? ?????? ????? C# .NET ?? Visual Basic .NET ?? ????? ????? ????????? WebServiceTest ??? ???
  2. ?????? Explorer ???, ???? ????????????? ????-????? ????, ?? ???? ?????? ?????? ??????.
  3. ??????????? ?????, WebServiceTest ?? ??? ????? URL ???? ????:
    HTTP://localhost/MyWebService/Service1.asmx
  4. ????? ????,?? ?????? ????-????? ????, ?? ???? ????????? ??????.
  5. ?????? Explorer ???, ???? ?????WebForm1.aspx?? ????-????? ????, ?? ???? ???????? ???.
  6. ?????? ????? ?? WebForm1, ??? ??? ????? ????WebForm1????? ?? ???Page_Load????? ???? ????????? ????Page_Load????? ??????????? ???:

    ????? C# .NET ????? ???
    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 ????? ???
    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
  7. ????? ????????????? ??,??????? ??????.
  8. Service1 ????? ?? ??? ??????? ??? ????? URL ????? ???? ?? ????? ???:
    HTTP://localhost/WebServiceTest/WebForm1.aspx
  9. You may receive an Access Denied error message. This occurs because your credentials are not delivered with the Web service request for authentication.

Pass Current Credentials to the Web Service

TheCredentialCacheclass belongs to theSystem.Net??? ????? ???
  1. Add the following namespace declaration to the top of the file:

    ????? C# .NET ????? ???
    using System.Net;
    Visual Basic .NET ????? ???
    Imports System.Net
  2. ????? ????DefaultCredentials???? ?? ??????????????property of the Web service client-side proxy. To do this, change the code of thePage_Loadevent as follows:

    ????? C# .NET ?????:
    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 ????? ???
    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
    
  3. ????? ???????? ???????? ??,???????.Hello ??????appears in the browser.

??????

???????? ??????? ?? ???, Microsoft ?????? ??? ??? ???? ????? ?? ??? ????? ???? ?????? ????? ????::
811318  (http://support.microsoft.com/kb/811318/EN-US/ ) PRB: "????? ????????" ?????? ????? ?? ?? ??? ???? ??? ???? ??? ???? ??????? ?????? ??? ??
???? ??????? ?? ???, Microsoft ?? ????? ??? ???? ?? ????::
HTTP://msdn2.Microsoft.com/en-us/library/aa302415.aspx (http://msdn2.microsoft.com/en-us/library/aa302415.aspx)

???? ???? ???? ??:
  • 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
??????: 
kbwebservices kbwebforms kbsecurity kbauthentication kbhowtomaster kbmt KB813834 KbMthi
???? ?????? ???????????? ?????? ????????
??????????: ?? ???? ?? ???? ??????? ?? ????? ?? Microsoft ????-?????? ?????????? ?????? ?????? ???? ??? ??. Microsoft ???? ??? ????-???????? ?? ????-???????? ????? ?????? ?? ???? ???????? ???? ?? ???? ????? ????? ??? ?? ??? ?????? ?? ???? ???? ???? ??? ????? ??. ???????, ????-???????? ???? ????? ???? ???? ???? ???. ?????, ????????, ?????-???? ?? ??????? ?? ???????? ?? ???? ???, ???? ?? ??? ?????? ???? ???? ??? ????? ??? ?? ???? ??. Microsoft ??????? ??? ???? ?? ?????? ?? ??????????, ????????? ?? ??? ?????? ?? ???? ????? ?? ???? ???????? ?? ??? ???? ????? ?? ??? ????????? ???? ??. Microsoft ????-?????? ?????????? ?? ????? ?????? ?? ?? ??? ??.
?????????? ?? ??????? ????????? ??????? ??:813834  (http://support.microsoft.com/kb/813834/en-us/ )