HttpRequest.LogonUserIdentity 属性

定义

获取当前用户的 WindowsIdentity 类型。

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 Internet Information Services (IIS)身份验证设置的 WindowsIdentity 对象。

例外

Web 应用程序正在 IIS 7 集成模式下运行,尚未引发 PostAuthenticateRequest 事件。

示例

下面的代码示例演示如何检索 LogonUserIdentity 当前用户的 属性,并写出文本文件中每个项的值。 将此代码放在窗体 ACTION 的 属性引用的 ASP.NET 页上。

<%@ 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 Internet Information Services (IIS) 。 由 LogonUserIdentity 公开的 类的WindowsIdentity实例跟踪 IIS 请求令牌,并为 ASP.NET 内正在处理的当前 HTTP 请求提供对此令牌的轻松访问。 类的实例是自动创建的, WindowsIdentity 因此无需将其构造为 即可访问其方法和属性。

适用于