語言

WebService.User 屬性

定義

取得 ASP.NET 伺服器 User 物件。 可用來驗證使用者是否被授權執行請求。

public:
 property System::Security::Principal::IPrincipal ^ User { System::Security::Principal::IPrincipal ^ get(); };
[System.ComponentModel.Browsable(false)]
public System.Security.Principal.IPrincipal User { get; }
[<System.ComponentModel.Browsable(false)>]
member this.User : System.Security.Principal.IPrincipal
Public ReadOnly Property User As IPrincipal

屬性值

A IPrincipal 代表 ASP.NET 伺服器 User 物件。

屬性

範例

以下範例會查詢已認證的使用者名稱並回傳該名稱。

<%@ 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

備註

網際網路資訊服務(IIS)與 .NET Framework 都需要設定為認證,才能使該 User 屬性具有意義。 認證是接受使用者提供的憑證,並將其與某個權威機構進行驗證的過程。 如果憑證有效,你就擁有一個經過認證的身份。 .NET Framework 中的認證是透過在 web.config 檔案中放置條目來設定的。

以下範例展示了你在 web.config 檔案中放置的條目,以啟用 Windows 認證。

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

欲了解更多關於為 XML Web 服務設置安全性的資訊,請參閱 Secure Secure Using Created 的 XML Web Services ASP.NET。

適用於

另請參閱