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

屬性值

IPrincipal

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

備註

Internet Information Services (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 服務安全性的詳細資訊,請參閱保護使用 ASP.NET 建立的 XML Web 服務

適用於

另請參閱