SqlContext.WindowsIdentity 属性

定义

调用方的 Microsoft Windows 标识。

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

属性值

WindowsIdentity

表示调用方的 Windows 标识的 WindowsIdentity 实例;如果客户端是使用“SQL Server 身份验证”进行身份验证的,则为 null

示例

下面的示例说明如何获取调用客户端的 Windows 标识并模拟该客户端。

WindowsIdentity clientId = null;
WindowsImpersonationContext impersonatedUser = null;

clientId = SqlContext.WindowsIdentity;

// This outer try block is used to thwart exception filter attacks which would prevent
// the inner finally block from executing and resetting the impersonation.
try
{
   try
   {
      impersonatedUser = clientId.Impersonate();
      if (impersonatedUser != null)
      {
        // Perform some action using impersonation.
      }
   }
   finally
   {
      if (impersonatedUser != null)
         impersonatedUser.Undo();
   }
}
catch
{
   throw;
}
Dim clientId As WindowsIdentity
Dim impersonatedUser As WindowsImpersonationContext

clientId = SqlContext.WindowsIdentity

Try 
   Try
   
      impersonatedUser = clientId.Impersonate()

      If impersonatedUser IsNot Nothing Then
         ' Perform some action using impersonation.
      End If

   Finally

     If impersonatedUser IsNot Nothing Then
         impersonatedUser.Undo
     End If

   End Try

Catch e As Exception

   throw e

End Try

注解

始终在进程帐户的上下文中调用SQL Server中的公共语言运行时 (CLR) 代码。 如果代码需要使用调用用户的标识而不是SQL Server进程标识来执行操作,则应通过此属性获取模拟令牌。 获取 WindowsIdentity 对象之后,调用方可以模拟客户端帐户并代表调用方执行操作。

如果从SQL Server外部调用,则会引发 aNotSupportedException

只有用 EXTERNAL_ACCESSUNSAFE 权限标记的程序集才能访问此属性。

此属性为只读。

适用于