共用方式為


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

WindowsIdentity 執行個體,表示呼叫端的 Windows 識別;如果使用 SQL Server 驗證 (SQL Server Authentication) 驗證用戶端,則為 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

備註

Common Language Runtime (CLR) 程式碼,一律會在進程帳戶的內容中叫用SQL Server。 如果程式碼必須使用呼叫使用者的身分識別來執行動作,而不是SQL Server進程識別,則應該透過此屬性取得模擬權杖。 取得 WindowsIdentity 物件之後,呼叫端可以模擬用戶端帳戶,並代表它執行動作。

如果從外部SQL Server叫用 , NotSupportedException 則會擲回 。

只有以 EXTERNAL_ACCESSUNSAFE 使用權限標記的組件才能存取此屬性。

這個屬性是唯讀的。

適用於