WebClientProtocol.Credentials 属性

定义

获取或设置 XML Web services 客户端身份验证的安全凭据。

public:
 property System::Net::ICredentials ^ Credentials { System::Net::ICredentials ^ get(); void set(System::Net::ICredentials ^ value); };
public System.Net.ICredentials Credentials { get; set; }
member this.Credentials : System.Net.ICredentials with get, set
Public Property Credentials As ICredentials

属性值

XML Web services 客户端的 ICredentials

示例

以下示例是一个 ASP.NET Web 窗体,它调用名为 的 MathXML Web 服务。 EnterBtn_Click在 函数中,Web 窗体使用 Credentials 属性显式设置身份验证凭据。 用户名、密码和域将传递到 类的构造函数中 NetworkCredential

重要

此示例具有一个接受用户输入的文本框,这是一个潜在的安全威胁。 默认情况下,ASP.NET 网页验证用户输入是否不包含脚本或 HTML 元素。 有关详细信息,请参阅:

<%@ Page Language="C#" %>
<%@ Import Namespace="System.Net" %>

<html>
    <script language="C#" runat="server">
       void EnterBtn_Click(Object Src, EventArgs E) 
          {
            MyMath.Math math = new MyMath.Math();
            
            // Obtain password from a secure store.
            String SecurelyStoredPassword = String.Empty; 
 
            // Set the client-side credentials using the Credentials property.
            ICredentials credentials = new NetworkCredential("Joe",SecurelyStoredPassword,"mydomain");
            math.Credentials = credentials;
       
            int total = math.Add(Convert.ToInt32(Num1.Text), Convert.ToInt32(Num2.Text));
            Total.Text = "Total: " + total.ToString();
         }
 
    </script>
 
    <body>
       <form action="MathClient.aspx" runat=server>
           
          Enter the two numbers you want to add and then press the Total button.
          <p>
          Number 1: <asp:textbox id="Num1" runat=server/>  +
          Number 2: <asp:textbox id="Num2" runat=server/> =
          <asp:button text="Total" Onclick="EnterBtn_Click" runat=server/>
          <p>
          <asp:label id="Total"  runat=server/>
          
       </form>
    </body>
 </html>
<%@ Page Language="VB" %>
<%@ Import Namespace="System.Net" %>

<html>
    <script language="VB" runat="server">
        Sub EnterBtn_Click(src As Object, e As EventArgs)
            Dim math As New MyMath.Math()

            ' Obtain password from a secure store.
            Dim SecurelyStoredPassword As String = String.Empty
            
            ' Set the client-side credentials using the Credentials property.
            Dim credentials = New NetworkCredential("Joe", SecurelyStoredPassword, "mydomain")
            math.Credentials = credentials
            
            Dim iTotal As Integer = math.Add(Convert.ToInt32(Num1.Text), Convert.ToInt32(Num2.Text))
            Total.Text = "Total: " + iTotal.ToString()
        End Sub
 
    </script>
 
    <body>
       <form action="MathClient.aspx" runat=server>
           
          Enter the two numbers you want to add and then press the Total button.
          <p>
          Number 1: <asp:textbox id="Num1" runat=server/>  +
          Number 2: <asp:textbox id="Num2" runat=server/> =
          <asp:button text="Total" Onclick="EnterBtn_Click" runat=server/>
          <p>
          <asp:label id="Total"  runat=server/>
          
       </form>
    </body>
 </html>

注解

使用 Credentials 属性时,XML Web 服务客户端必须实例化实现 ICredentials的类,例如 NetworkCredential,然后设置特定于身份验证机制的客户端凭据。 类 NetworkCredential 可用于使用基本、摘要、NTLM 和 Kerberos 身份验证机制设置身份验证凭据。

如果将 Credentials 属性设置为 CredentialCache.DefaultCredentials,则客户端将与服务器进行协商,并根据服务器的配置方式执行 Kerberos 和/或 NTLM 身份验证。

适用于

另请参阅