HttpRequest.LogonUserIdentity HttpRequest.LogonUserIdentity HttpRequest.LogonUserIdentity HttpRequest.LogonUserIdentity Property

定義

取得目前使用者的 WindowsIdentity 型別。Gets the WindowsIdentity type for the current user.

public:
 property System::Security::Principal::WindowsIdentity ^ LogonUserIdentity { System::Security::Principal::WindowsIdentity ^ get(); };
public System.Security.Principal.WindowsIdentity LogonUserIdentity { get; }
member this.LogonUserIdentity : System.Security.Principal.WindowsIdentity
Public ReadOnly Property LogonUserIdentity As WindowsIdentity

屬性值

目前 Microsoft 網際網路資訊服務 (IIS) 驗證設定的 WindowsIdentity 物件。A WindowsIdentity object for the current Microsoft Internet Information Services (IIS) authentication settings.

例外狀況

Web 應用程式是在 IIS 7 整合模式中執行,而且尚未引發 PostAuthenticateRequest 事件。The Web application is running in IIS 7 integrated mode and the PostAuthenticateRequest event has not yet been raised.

範例

下列程式碼範例示範如何擷取LogonUserIdentity屬性寫出的文字檔案中的每個項目值與目前的使用者。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. 此程式碼置於表單的參考的 ASP.NET 頁面ACTION屬性。Place this code on the ASP.NET page referenced by the form's ACTION attribute.

<%@ Page Language="C#" %>
<%@ 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 string INFO_DIR = @"c:\temp\";

    private void Page_Load(object sender, System.EventArgs e)
    {
        // Validate that user is authenticated
        if (!Request.LogonUserIdentity.IsAuthenticated)
            Response.Redirect("LoginPage.aspx");
        
        // Create a string that contains the file path
        string strFilePath = INFO_DIR + "CS_Log.txt";
        
        Response.Write("Writing log file to " + strFilePath + "...");
        
        // Create stream writer object and pass it the file path
        StreamWriter sw = File.CreateText(strFilePath);
        
        // Write user info to log
        sw.WriteLine("Access log from " + DateTime.Now.ToString());
        sw.WriteLine("User: " + Request.LogonUserIdentity.User);
        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);
        sw.WriteLine("Token: " + Request.LogonUserIdentity.Token);

        // Close the stream to the file.
        sw.Close();
    }  
</script>
<%@ 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>

備註

LogonUserIdentity屬性會公開的屬性和方法WindowsIdentity目前連接的使用者以 Microsoft 網際網路資訊服務 (IIS) 的物件。The LogonUserIdentity property exposes the properties and methods of the WindowsIdentity object for the currently connected user to Microsoft Internet Information Services (IIS). 執行個體WindowsIdentity類別所公開LogonUserIdentity追蹤 IIS 要求語彙基元,並可輕鬆存取此語彙基元在 ASP.NET 內正在處理目前的 HTTP 要求。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. 執行個體WindowsIdentity類別會自動建立,因此不需要為建構,以便存取其方法和屬性。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.

適用於