HttpContext.User 属性
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
获取或设置当前 HTTP 请求的安全信息。
public:
property System::Security::Principal::IPrincipal ^ User { System::Security::Principal::IPrincipal ^ get(); void set(System::Security::Principal::IPrincipal ^ value); };
public System.Security.Principal.IPrincipal User { get; set; }
member this.User : System.Security.Principal.IPrincipal with get, set
Public Property User As IPrincipal
属性值
当前 HTTP 请求的安全信息。
示例
以下示例演示如何通过 User 属性访问当前用户的属性。 这些属性用于设置网页的标题。
如果应用程序使用 Windows 身份验证,则用户名包括域。 例如,页面标题为“DOMAIN\username 的主页”。
protected void Page_Load(object sender, EventArgs e)
{
if (User.Identity.IsAuthenticated)
{
Page.Title = "Home page for " + User.Identity.Name;
}
else
{
Page.Title = "Home page for guest user.";
}
}
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If (User.Identity.IsAuthenticated) Then
Page.Title = "Home page for " + User.Identity.Name
Else
Page.Title = "Home page for guest user."
End If
End Sub
注解
该 User 属性提供对接口的属性和方法的 IPrincipal 编程访问。
由于 ASP.NET 页包含对 System.Web 命名空间的默认引用(其中包含 HttpContext 类),因此可以在.aspx页上引用成员 HttpContext ,而无需使用对该类的完全限定的类引用 HttpContext。 例如,可用于 User.Identity.Name 获取代表当前进程运行的用户的名称。 但是,如果要使用来自 ASP.NET 代码隐藏模块的成员 IPrincipal ,则必须在模块中包含对命名空间的引用,以及对 System.Web 当前活动请求/响应上下文和要使用的类 System.Web 的完全限定引用。 例如,在代码隐藏页中,必须指定完全限定的名称 HttpContext.Current.User.Identity.Name。