Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to make Custom PrincipalPermissionAttribute handle user or role changes
    primarykey
    data
    text
    <p>I have written a custom PrincipalPermissionAttribute that uses an AuthenticationService instead of Thread.CurrentPrincipal as PrincipalPermissionAttribute does. </p> <p>It works as I like, but if the user logs out and back in, or if the user's roles were to change, the attribute code is never called a second time. I suspect I've not informed the attribute it needs to recheck the permission? Breakpoint set on the CreatePermission method only ever hits once. </p> <p>Is an attribute code only ever evaluated once? The attribute is currently decorating an event handler for a button click on my View's codebehind.</p> <p>If I change my method back to use the PrincipalPermissionAttribute then it does work as I expect, logging out and back in as a user without the right role throws the SecurityException I expect. Have I missed overriding a property perhaps?</p> <pre><code>[Serializable] [AttributeUsage(AttributeTargets.Method | AttributeTargets.Class, AllowMultiple = true, Inherited = false)] public sealed class RolePermissionAttribute : CodeAccessSecurityAttribute { private readonly PrincipalPermission _revoke = new PrincipalPermission(PermissionState.None); private readonly PrincipalPermission _allow = new PrincipalPermission(PermissionState.Unrestricted); private IList&lt;string&gt; _roles; private readonly IAuthenticationService _authorisationService; public RolePermissionAttribute(SecurityAction action) : this(action, ServiceLocator.Current.GetInstance&lt;IAuthenticationService&gt;()) { } public RolePermissionAttribute(SecurityAction action, IAuthenticationService authorisationService) : base(action) { _authorisationService = authorisationService; } public string Roles { get; set; } public bool Authenticated { get; set; } public override IPermission CreatePermission() { _roles = (this.Roles ?? string.Empty).Split(',', ';') .Select(s =&gt; s.Trim()) .Where(s =&gt; s.Length &gt; 0) .Distinct() .ToList(); bool result = false; if (_authorisationService != null) { var principal = _authorisationService.ClientSecurityPrincipal; if (principal == null) { throw new SecurityException("Access Denied. You are not logged in"); } // If Authenticated is enforced then revoke if user is not authenticated if (Authenticated &amp;&amp; !_authorisationService.IsAuthenticated) { throw new SecurityException("Access Denied. You are not authenticated"); } // Allow if the principal is in any of the roles result = _roles.Any(principal.IsInRole); if (!result) { throw new SecurityException("Access Denied. You are not in an allowed Role"); } } return result ? _allow : _revoke; } } </code></pre> <p>}</p> <p>Here's the method with the attribute</p> <pre><code>[RolePermission(SecurityAction.Demand, Authenticated = true, Roles = "Admin")] private void barButtonItemConfig_ItemClick(object sender, ItemClickEventArgs e) { // Do stuff } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload