Export (0) Print
Expand All

HttpRequest.LogonUserIdentity Property

Gets the WindowsIdentity type for the current user.

Namespace:  System.Web
Assembly:  System.Web (in System.Web.dll)

'Declaration
Public ReadOnly Property LogonUserIdentity As WindowsIdentity
'Usage
Dim instance As HttpRequest 
Dim value As WindowsIdentity 

value = instance.LogonUserIdentity

Property Value

Type: System.Security.Principal.WindowsIdentity
A WindowsIdentity for the current Microsoft Internet Information Services (IIS) authentication settings.

The LogonUserIdentity property exposes the properties and methods of the WindowsIdentity object for the currently connected user to Microsoft Internet Information Services (IIS). The instance of the WindowsIdentity class that is exposed by LogonUserIdentity tracks the IIS request token and provides easy access to this token for the current HTTP request being processed inside of ASP.NET. An instance of the WindowsIdentity class is automatically created so it does not need to be constructed to in order to gain access to its methods and properties.

The following code example shows how to retrieve the LogonUserIdentity property for the current user and write out the values of each item in a text file. Place this code on the ASP.NET page referenced by the form's ACTION attribute.

<%@ Page Language="VB" %>
<%@ import Namespace="System.IO" %>

<script runat="server">

    ' * NOTE: To use this sample, create a c:\temp  folder, 
    ' *  add the ASP.NET account (in IIS 5.x <machinename>\ASPNET, 
    ' *  in IIS 6.x NETWORK SERVICE), and give it write permissions 
    ' *  to the folder. 

    Private Const INFO_DIR As String = "c:\temp\" 

    Private Sub Page_Load(sender As Object, e As System.EventArgs)

        ' Validate that user is authenticated 
        If Not (Request.LogonUserIdentity.IsAuthenticated) Then
            Response.Redirect("LoginPage.aspx")
        End If 

        ' Create a string that contains the file path 
        Dim strFilePath As String = INFO_DIR & "VB_Log.txt"

        Response.Write("Writing log file to " & strFilePath & "...")

        ' Create stream writer object and pass it the file path 
        Dim sw As StreamWriter = File.CreateText(strFilePath)

        ' Write user info to log
        sw.WriteLine("Access log from " & DateTime.Now.ToString())
        sw.WriteLine("User: " & Request.LogonUserIdentity.User.ToString())
        sw.WriteLine("Name: " & Request.LogonUserIdentity.Name)
        sw.WriteLine("AuthenticationType: " & Request.LogonUserIdentity.AuthenticationType)
        sw.WriteLine("ImpersonationLevel: " & Request.LogonUserIdentity.ImpersonationLevel)
        sw.WriteLine("IsAnonymous: " & Request.LogonUserIdentity.IsAnonymous)
        sw.WriteLine("IsGuest: " & Request.LogonUserIdentity.IsGuest)
        sw.WriteLine("IsSystem: " & Request.LogonUserIdentity.IsSystem)
        sw.WriteLine("Owner: " & Request.LogonUserIdentity.Owner.ToString())
        sw.WriteLine("Token: " & Request.LogonUserIdentity.Token.ToString())

        ' Close the stream to the file.
        sw.Close()
    End Sub

</script>

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0

Community Additions

ADD
Show:
© 2014 Microsoft