[This is preliminary documentation and subject to change.]
Raises the AuthorizationContextChanged event.
You should call this method whenever you change the authorization context outside of the policy as the following example illustrates:
[Visual Basic]
Imports System
Imports System.Diagnostics
Imports System.Security.Principal
Imports LaMarvin.Constable
Imports LaMarvin.Constable.Model
Imports LaMarvin.Constable.Principal
Public Class RaiseAuthorizationContextChangedEvent
Public Shared Sub Main()
' Simple policy that grants the Author role the Create action.
Dim xmlPolicy As String = "<authorizationPolicy>" & _
"<role id=""Author""/>" & _
"<state id=""Default""/>" & _
"<action id=""Create""/>" & _
"<actionRule action=""Create"" Role=""Author""/>" & _
"</authorizationPolicy>"
' Create the policy and hook the AuthorizationContextChanged event.
Dim policy As AuthorizationPolicy = AuthorizationPolicyLoader.LoadFromXml(xmlPolicy)
AddHandler policy.AuthorizationContextChanged, New EventHandler(AddressOf HandleAuthorizationContextChanged)
' Assign a custom principal in the Author role and make sure the
' Create action is executable.
Console.WriteLine("Assigning CurrentPrincipal")
policy.CurrentPrincipal = New ExtendedPrincipal(New GenericIdentity("John"), New String() {"Author"})
Debug.Assert(policy.IsActionExecutable("Create"))
' Executing an action always raises the AuthorizationContextChanged event even
' if no state change takes place.
Console.WriteLine("Executing Create action")
policy.ExecuteAction("Create")
' Now change the principal and and note that the
' AuthorizationContextChanged event isn't raised automatically.
Console.WriteLine("Changing CurrentPrincipal role membership")
DirectCast(policy.CurrentPrincipal, ExtendedPrincipal).Roles.RemoveAt("Author")
Console.WriteLine("Raising the AuthorizationContextChanged event manually")
policy.RaiseAuthorizationContextChangedEvent()
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 RaiseAuthorizationContextChangedEvent
{
public static void Main()
{
// Simple policy that grants the Author role the Create action.
string xmlPolicy = "<authorizationPolicy>" +
"<role id=\"Author\"/>" +
"<state id=\"Default\"/>" +
"<action id=\"Create\"/>" +
"<actionRule action=\"Create\" Role=\"Author\"/>" +
"</authorizationPolicy>";
// Create the policy and hook the AuthorizationContextChanged event.
AuthorizationPolicy policy = AuthorizationPolicyLoader.LoadFromXml(xmlPolicy);
policy.AuthorizationContextChanged += new EventHandler(HandleAuthorizationContextChanged);
// Assign a custom principal in the Author role and make sure the
// Create action is executable.
Console.WriteLine("Assigning CurrentPrincipal");
policy.CurrentPrincipal = new ExtendedPrincipal(new GenericIdentity("John"), new string[] {"Author"});
Debug.Assert(policy.IsActionExecutable("Create"));
// Executing an action always raises the AuthorizationContextChanged event even
// if no state change takes place.
Console.WriteLine("Executing Create action");
policy.ExecuteAction("Create");
// Now change the principal and and note that the
// AuthorizationContextChanged event isn't raised automatically.
Console.WriteLine("Changing CurrentPrincipal role membership");
((ExtendedPrincipal)policy.CurrentPrincipal).Roles.RemoveAt("Author");
Console.WriteLine("Raising the AuthorizationContextChanged event manually");
policy.RaiseAuthorizationContextChangedEvent();
}
private static void HandleAuthorizationContextChanged(object sender, EventArgs e)
{
Console.WriteLine(" ** Authorization context changed ** ");
}
}
The code above generates the following console output: Assigning CurrentPrincipal ** Authorization context changed ** Executing Create action ** Authorization context changed ** Changing CurrentPrincipal role membership Raising the AuthorizationContextChanged event manually ** Authorization context changed **
AuthorizationPolicy Class | LaMarvin.Constable Namespace | AuthorizationPolicy.RaiseAuthorizationContextChangedEvent Overload List