SecurityContext.Run Method (SecurityContext, ContextCallback, Object)

 

Runs the specified method in the specified security context on the current thread.

Namespace:   System.Security
Assembly:  mscorlib (in mscorlib.dll)

[SecurityCriticalAttribute]
public static void Run(
	SecurityContext securityContext,
	ContextCallback callback,
	object state
)

Parameters

securityContext
Type: System.Security.SecurityContext

The security context to set.

callback
Type: System.Threading.ContextCallback

The delegate that represents the method to run in the specified security context.

state
Type: System.Object

The object to pass to the callback method.

Exception Condition
InvalidOperationException

securityContext is null.

-or-

securityContext was not acquired through a capture operation.

-or-

securityContext has already been used as the argument to a Run method call.

The security context for the current thread is returned to its previous state when the method call is complete.

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);
    }
}

SecurityCriticalAttribute

requires full trust for the immediate caller. This member cannot be used by partially trusted or transparent code.

.NET Framework
Available since 2.0
Return to top
Show: