ClaimsAuthorizationManager.CheckAccess(AuthorizationContext) Methode

Definitie

Wanneer deze is geïmplementeerd in een afgeleide klasse, controleert u de autorisatie voor het onderwerp in de opgegeven context om de opgegeven actie uit te voeren op de opgegeven resource.

public:
 virtual bool CheckAccess(System::Security::Claims::AuthorizationContext ^ context);
public virtual bool CheckAccess(System.Security.Claims.AuthorizationContext context);
abstract member CheckAccess : System.Security.Claims.AuthorizationContext -> bool
override this.CheckAccess : System.Security.Claims.AuthorizationContext -> bool
Public Overridable Function CheckAccess (context As AuthorizationContext) As Boolean

Parameters

context
AuthorizationContext

De autorisatiecontext die het onderwerp, de resource en de actie bevat waarvoor autorisatie moet worden gecontroleerd.

Retouren

true als het onderwerp gemachtigd is om de opgegeven actie uit te voeren op de opgegeven resource; anders, false.

Voorbeelden

De codevoorbeelden die in de ClaimsAuthorizationManager onderwerpen worden gebruikt, worden uit het Claims Based Authorization voorbeeld genomen. Dit voorbeeld biedt een aangepaste claimautorisatiebeheerder die onderwerpen kan autoriseren op basis van een beleid dat is opgegeven in de configuratie. De aangepaste claimautorisatiebeheer bestaat uit drie basisonderdelen: een klasse die is afgeleid van ClaimsAuthorizationManager die de manager implementeert, de ResourceAction klasse die een resource en een actie koppelt, en een beleidslezer die beleid leest en compileert dat is opgegeven in het configuratiebestand. Dit gecompileerde beleid kan vervolgens worden gebruikt door de claimautorisatiebeheerder om een principal te evalueren om toegang tot resources te autoriseren. Niet alle elementen worden omwille van de beknoptheid weergegeven. Zie de WIF-codevoorbeeldindex voor informatie over dit voorbeeld en andere voorbeelden die beschikbaar zijn voor WIF en waar u ze kunt downloaden.

De volgende code toont de onderdrukking van de CheckAccess methode. Deze methode verleent of weigert toegang op basis van een beleid dat is gelezen en gecompileerd vanuit het configuratiebestand.

static Dictionary<ResourceAction, Func<ClaimsPrincipal, bool>> _policies = new Dictionary<ResourceAction, Func<ClaimsPrincipal, bool>>();
PolicyReader _policyReader = new PolicyReader();
    /// <summary>
    /// Checks if the principal specified in the authorization context is authorized to perform action specified in the authorization context 
    /// on the specified resoure
    /// </summary>
    /// <param name="pec">Authorization context</param>
    /// <returns>true if authorized, false otherwise</returns>
    public override bool CheckAccess(AuthorizationContext pec)
    {
        //
        // Evaluate the policy against the claims of the 
        // principal to determine access
        //
        bool access = false;
        try
        {
            ResourceAction ra = new ResourceAction(pec.Resource.First<Claim>().Value, pec.Action.First<Claim>().Value);

            access = _policies[ra](pec.Principal);
        }
        catch (Exception)
        {
            access = false;
        }

        return access;
    }
}

De volgende code toont de ResourceAction klasse die wordt gebruikt door de aangepaste claimbeheerder.

using System;

namespace ClaimsAuthorizationLibrary
{
    /// <summary>
    /// Class to encapsulate resource/action pair
    /// </summary>
    public class ResourceAction
    {
        public string Resource;
        public string Action;

        /// <summary>
        /// Checks if the current instance is equal to the given object by comparing the resource and action values
        /// </summary>
        /// <param name="obj">object to compare to</param>
        /// <returns>True if equal, else false.</returns>
        public override bool Equals(object obj)
        {
            ResourceAction ra = obj as ResourceAction;
            if (ra != null)
            {
                return ((string.Compare(ra.Resource, Resource, true) == 0) && (string.Compare(ra.Action, Action, true) == 0));
            }

            return base.Equals(obj);
        }

        /// <summary>
        /// Gets the hash code.
        /// </summary>
        /// <returns>The hash code.</returns>
        public override int GetHashCode()
        {
            return (Resource + Action).ToLower().GetHashCode();
        }

        /// <summary>
        /// Creates an instance of ResourceAction class.
        /// </summary>
        /// <param name="resource">The resource name.</param>
        /// <param name="action">The action.</param>
        /// <exception cref="ArgumentNullException">when <paramref name="resource"/> is null</exception>
        public ResourceAction(string resource, string action)
        {
            if (string.IsNullOrEmpty(resource))
            {
                throw new ArgumentNullException("resource");
            }

            Resource = resource;
            Action = action;
        }
    }
}

Het beleid dat wordt gebruikt door claimautorisatiebeheer, wordt opgegeven door aangepaste <policy> elementen onder het <element claimsAuthorizationManager> . Dit beleid wordt gelezen en gecompileerd door de LoadCustomConfiguration methode. In het eerste beleid moet de principal een van de opgegeven claims bezitten om de opgegeven actie uit te voeren op de opgegeven resource. In het tweede beleid moet de principal beide claims bezitten om de opgegeven actie op de opgegeven resource uit te voeren. In alle andere wordt de principal automatisch toegang verleend, ongeacht de claims die de principal bezit.

<system.identityModel>
  <identityConfiguration>
    <claimsAuthorizationManager type="ClaimsAuthorizationLibrary.MyClaimsAuthorizationManager, ClaimsAuthorizationLibrary">
      <policy resource="http://localhost:28491/Developers.aspx" action="GET">
        <or>
          <claim claimType="http://schemas.microsoft.com/ws/2008/06/identity/claims/role" claimValue="developer" />
          <claim claimType="http://schemas.xmlsoap.org/claims/Group" claimValue="Administrator" />
        </or>
      </policy>
      <policy resource="http://localhost:28491/Administrators.aspx" action="GET">
        <and>
          <claim claimType="http://schemas.xmlsoap.org/claims/Group" claimValue="Administrator" />
          <claim claimType="http://schemas.xmlsoap.org/ws/2005/05/identity/claims/country" claimValue="USA" />
        </and>
      </policy>
      <policy resource="http://localhost:28491/Default.aspx" action="GET">
      </policy>
      <policy resource="http://localhost:28491/" action="GET">
      </policy>
      <policy resource="http://localhost:28491/Claims.aspx" action="GET">
      </policy>
    </claimsAuthorizationManager>

    ...

  </identityConfiguration>
</system.identityModel>

Opmerkingen

De basis-implementatie retourneert truealtijd, waardoor toegang wordt toegestaan. U kunt deze methode in een afgeleide klasse overschrijven om toegang te autoriseren op basis van de vereisten van uw RP-toepassing. Als deze methode false retourneert, retourneert Windows Identity Foundation (WIF) een niet-geautoriseerde fout aan de aanroeper; anders wordt de uitvoering doorgegeven aan de RP-toepassing.

Van toepassing op