WebService.User 屬性


取得 ASP.NET 伺服器 User 物件。Gets the ASP.NET server User object. 可用來驗證是否授權使用者執行要求。Can be used to authenticate whether a user is authorized to execute the request.

 property System::Security::Principal::IPrincipal ^ User { System::Security::Principal::IPrincipal ^ get(); };
public System.Security.Principal.IPrincipal User { get; }
member this.User : System.Security.Principal.IPrincipal
Public ReadOnly Property User As IPrincipal


IPrincipal,表示 ASP.NET 伺服器 User 物件。A IPrincipal representing the ASP.NET server User object.



下列範例會查閱已驗證的使用者名稱, 並傳回該名稱。The example below looks up the authenticated user name and returns that name.

<%@ WebService Language="C#" Class="Util" %>
 using System.Web.Services;
 public class Util: WebService {
      [ WebMethod(Description="Obtains the User Name",EnableSession=false) ]
      public string GetUserName() {
         return User.Identity.Name;
<%@ WebService Language="VB" Class="Util" %>
Imports System.Web.Services

Public Class Util
    Inherits WebService
    <WebMethod(Description := "Obtains the User Name", _
        EnableSession := False)> _
    Public Function GetUserName() As String
        Return User.Identity.Name
    End Function
End Class


Internet Information Services (IIS) 和 .NET Framework 都必須設定為進行驗證User , 屬性才有意義。Both Internet Information Services (IIS) and the .NET Framework need to be configured for authentication in order for the User property to be meaningful. 驗證是從使用者接受認證, 並根據某個授權單位來驗證那些認證的程式。Authentication is the process of accepting credentials from a user and validating those credentials against some authority. 如果認證有效, 您就會有已驗證的身分識別。If the credentials are valid, you have an authenticated identity. .NET Framework 中的驗證是藉由將專案放在 web.config 檔案中進行設定。Authentication in the .NET Framework is configured by placing entries in the web.config file.

下列範例示範您在 web.config 檔案中放置以啟用 Windows 驗證的專案。The following example demonstrates the entries you place in the web.config file to enable Windows authentication.

 <authentication mode="Windows"> <!-- Mode Options are Windows, Cookie, Passport and None or Empty String -->  

如需設定 XML Web Service 安全性的詳細資訊, 請參閱保護使用 ASP.NET 所建立的 XML Web ServiceFor more information on setting up security for an XML Web service see Securing XML Web Services Created Using ASP.NET.