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

代表 ASP.NET 服务器 IPrincipal 对象的 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有意义。 身份验证是接受用户凭据并针对某些机构验证这些凭据的过程。 如果凭据有效,则具有经过身份验证的标识。 通过在web.config文件中放置条目来配置.NET Framework中的身份验证。

以下示例演示在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 服务

适用于

另请参阅