HttpContext.User Propriedade
Definição
Obtém ou define informações de segurança para a solicitação HTTP atual.Gets or sets security information for the current HTTP request.
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
Valor da propriedade
Informações de segurança da solicitação HTTP atual.Security information for the current HTTP request.
Exemplos
O exemplo a seguir mostra como acessar as propriedades do usuário atual por meio da User propriedade.The following example shows how to access properties of the current user through the User property. Essas propriedades são usadas para definir o título da página da Web.Those properties are used to set the title of the Web page.
Se o aplicativo usar a autenticação do Windows, o nome de usuário incluirá o domínio.If the application uses Windows authentication, the user name includes the domain. Por exemplo, o título da página seria "Home Page do domínio \ nomedeusuário".For example, the page title would be "Home page for 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
Comentários
A User propriedade fornece acesso programático às propriedades e aos métodos da IPrincipal interface.The User property provides programmatic access to the properties and methods of the IPrincipal interface.
Como as páginas ASP.NET contêm uma referência padrão ao System.Web namespace (que contém a HttpContext classe), você pode referenciar os membros de HttpContext em uma página. aspx sem usar a referência de classe totalmente qualificada para o HttpContext .Because ASP.NET pages contain a default reference to the System.Web namespace (which contains the HttpContext class), you can reference the members of HttpContext on an .aspx page without using the fully qualified class reference to HttpContext. Por exemplo, você pode usar User.Identity.Name para obter o nome do usuário em cujo nome o processo atual está sendo executado.For example, you can use User.Identity.Name to get the name of the user on whose behalf the current process is running. No entanto, se você quiser usar os membros de IPrincipal um módulo code-behind ASP.net, deverá incluir uma referência ao System.Web namespace no módulo e uma referência totalmente qualificada para o contexto de solicitação/resposta ativo no momento e a classe no System.Web que você deseja usar.However, if you want to use the members of IPrincipal from an ASP.NET code-behind module, you must include a reference to the System.Web namespace in the module and a fully qualified reference to both the currently active request/response context and the class in System.Web that you want to use. Por exemplo, em uma página code-behind, você deve especificar o nome totalmente qualificado HttpContext.Current.User.Identity.Name .For example, in a code-behind page you must specify the fully qualified name HttpContext.Current.User.Identity.Name.