Constable Authorization Engine 2.0 BETA

AuthorizationPolicy.AuthorizationContextChanged Event

[This is preliminary documentation and subject to change.]

The event is raised whenever the authorization context of the policy is changed.

[VisualBasic]
PublicEventAuthorizationContextChangedAsEventHandler
[C#]
publiceventEventHandlerAuthorizationContextChanged;

Remarks

The following are the reasons that cause the AuthorizationContextChanged event to be raised:

CAUTION If you change the authorization context outside of the policy (for example by adding a role to a custom principal currently associated with the policy), be sure to raise the AuthorizationContextChanged event manually by calling the RaiseAuthorizationContextChangedEvent method.
The AuthorizationContextChanged event handlers are not serialized along with the policy; if you're transferring policies across AppDomain or machine boundaries, the event handlers won't be marshaled and used in the remote domain.

Example

The following example illustrates when the AuthorizationContextChanged event is raised and how to handle it:

[VisualBasic]
Imports System
Imports System.Diagnostics
Imports System.Security.Principal

Imports LaMarvin.Constable
Imports LaMarvin.Constable.Model
Imports LaMarvin.Constable.Principal

Public Class AuthorizationContextChangedEvent
  Public Shared Sub Main()
    ' Create policy and hook the AuthorizationContextChanged event.
    Dim policy As New AuthorizationPolicy
    AddHandler policy.AuthorizationContextChanged, AddressOf HandleAuthorizationContextChanged

    ' Populate the policy with a simple model - the Author can execute the Save
    ' action that moves the policy from the New to the Saved state. (More precisely, the Save action
    ' moves the policy's subject, such as 'document', from New to the Saved state.)
    policy.Roles.AddNew("Author")

    ' The first state added to the policy becomes the current state, so this also
    ' raises the AuthorizationContextChanged just like setting the CurrentState
    ' property directly.
    Console.WriteLine("Adding first state")
    policy.States.AddNew("New")

    policy.States.AddNew("Saved")
    policy.Actions.AddNew("Save")
    policy.ActionRules.AddNew("Save", "Author", "New", "Saved")

    ' Setting the CurrentPrincipal raises the AuthorizationContextChanged event.
    Console.WriteLine("Assigning CurrentPrincipal")
    policy.CurrentPrincipal = New ExtendedPrincipal(New GenericIdentity("John"), New String() {"Author"})
    Debug.Assert(policy.IsActionExecutable("Save"))

    ' Executing an action also raises the AuthorizationContextChanged event (even
    ' if no state change takes place).
    Console.WriteLine("Executing Save action")
    policy.ExecuteAction("Save")

    ' Setting the CurrentState also raises the AuthorizationContextChanged event.
    Console.WriteLine("Assigning CurrentState")
    policy.CurrentState = policy.States("New")
  End Sub


  Private Shared Sub HandleAuthorizationContextChanged(ByVal sender As Object, ByVal e As EventArgs)
    Console.WriteLine(" ** Authorization context changed ** ")
  End Sub

End Class

[C#]
using System;
using System.Diagnostics;
using System.Security.Principal;

using LaMarvin.Constable;
using LaMarvin.Constable.Model;
using LaMarvin.Constable.Principal;

public class AuthorizationContextChangedEvent
{
  public static void Main()
  {
    // Create policy and hook the AuthorizationContextChanged event.
    AuthorizationPolicy policy = new AuthorizationPolicy();
    policy.AuthorizationContextChanged += new EventHandler(HandleAuthorizationContextChanged);

    // Populate the policy with a simple model - the Author can execute the Save
    // action that moves the policy from the New to the Saved state. (More precisely, the Save action
    // moves the policy's subject, such as 'document', from New to the Saved state.)
    policy.Roles.AddNew("Author");

    // The first state added to the policy becomes the current state, so this also
    // raises the AuthorizationContextChanged just like setting the CurrentState
    // property directly.
    Console.WriteLine("Adding first state");
    policy.States.AddNew("New"); 

    policy.States.AddNew("Saved");
    policy.Actions.AddNew("Save");
    policy.ActionRules.AddNew("Save", "Author", "New", "Saved");

    // Setting the CurrentPrincipal raises the AuthorizationContextChanged event.
    Console.WriteLine("Assigning CurrentPrincipal");
    policy.CurrentPrincipal = new ExtendedPrincipal(new GenericIdentity("John"), new string[] {"Author"});
    Debug.Assert(policy.IsActionExecutable("Save"));
    
    // Executing an action also raises the AuthorizationContextChanged event (even
    // if no state change takes place).
    Console.WriteLine("Executing Save action");
    policy.ExecuteAction("Save");

    // Setting the CurrentState also raises the AuthorizationContextChanged event.
    Console.WriteLine("Assigning CurrentState");
    policy.CurrentState = policy.States["New"];
  }


  private static void HandleAuthorizationContextChanged(object sender, EventArgs e)
  {
    Console.WriteLine(" ** Authorization context changed ** ");
  }

}
The code above produces the following console output:
Adding first state
 ** Authorization context changed ** 
Assigning CurrentPrincipal
 ** Authorization context changed ** 
Executing Save action
 ** Authorization context changed ** 
Assigning CurrentState
 ** Authorization context changed **

See Also

AuthorizationPolicy Class | LaMarvin.Constable Namespace |