SecurityContext.Run(SecurityContext, ContextCallback, Object) 方法

定义

在当前线程上指定的安全上下文中运行指定的方法。Runs the specified method in the specified security context on the current thread.

public:
 static void Run(System::Security::SecurityContext ^ securityContext, System::Threading::ContextCallback ^ callback, System::Object ^ state);
public static void Run (System.Security.SecurityContext securityContext, System.Threading.ContextCallback callback, object state);
[System.Security.SecurityCritical]
public static void Run (System.Security.SecurityContext securityContext, System.Threading.ContextCallback callback, object state);
static member Run : System.Security.SecurityContext * System.Threading.ContextCallback * obj -> unit
[<System.Security.SecurityCritical>]
static member Run : System.Security.SecurityContext * System.Threading.ContextCallback * obj -> unit
Public Shared Sub Run (securityContext As SecurityContext, callback As ContextCallback, state As Object)

参数

securityContext
SecurityContext

要设置的安全上下文。The security context to set.

callback
ContextCallback

表示要在指定的安全上下文中运行的方法的委托。The delegate that represents the method to run in the specified security context.

state
Object

要传递给回调方法的对象。The object to pass to the callback method.

属性

例外

securityContextnullsecurityContext is null.

- 或 --or- securityContext 不是通过捕获操作获取的。securityContext was not acquired through a capture operation.

- 或 --or- 已经将 securityContext 用作 Run(SecurityContext, ContextCallback, Object) 方法调用的参数。securityContext has already been used as the argument to a Run(SecurityContext, ContextCallback, Object) method call.

示例

下面的代码示例演示如何使用 Run 方法在指定的安全上下文中执行方法。The following code example shows how to use the Run method to execute a method in a specified security context.

using System;
using System.Security;
using System.Threading;

class Test
{
    static void Main()
    {
        SecurityContext.Run(SecurityContext.Capture(), 
            new ContextCallback(Callback), "Hello world.");
    }
    static void Callback(object o)
    {
        Console.WriteLine(o);
    }
}
Imports System.Security
Imports System.Threading



Class Test
    
    Shared Sub Main() 
        Dim cCallBack As New ContextCallback(AddressOf Callback)
        SecurityContext.Run(SecurityContext.Capture(), cCallBack, "Hello world.")
    End Sub
    
    Shared Sub Callback(ByVal o As Object) 
        Console.WriteLine(o)
    
    End Sub
End Class

注解

当方法调用完成时,当前线程的安全上下文将恢复为其以前的状态。The security context for the current thread is returned to its previous state when the method call is complete.

适用于