Lire en anglais

Partager via


IAuthorizationPolicy Interface

Définition

Définit un ensemble de règles pour autoriser un utilisateur en fonction d'un ensemble de revendications donné.

C#
public interface IAuthorizationPolicy
C#
public interface IAuthorizationPolicy : System.IdentityModel.Policy.IAuthorizationComponent
Implémente

Exemples

C#
public class MyAuthorizationPolicy : IAuthorizationPolicy
{
    string id;

    public MyAuthorizationPolicy()
    {
        id =  Guid.NewGuid().ToString();
    }

    public bool Evaluate(EvaluationContext evaluationContext, ref object state)
    {
        bool bRet = false;
        CustomAuthState customstate = null;

        // If state is null, then this method has not been called before, so
        // set up a custom state.
        if (state == null)
        {
            customstate = new CustomAuthState();
            state = customstate;
        }
        else
        {
            customstate = (CustomAuthState)state;
        }

        Console.WriteLine("Inside MyAuthorizationPolicy::Evaluate");

        // If claims have not been added yet...
        if (!customstate.ClaimsAdded)
        {
            // Create an empty list of Claims.
            IList<Claim> claims = new List<Claim>();

            // Iterate through each of the claim sets in the evaluation context.
            foreach (ClaimSet cs in evaluationContext.ClaimSets)
                // Look for Name claims in the current claim set.
                foreach (Claim c in cs.FindClaims(ClaimTypes.Name, Rights.PossessProperty))
                    // Get the list of operations the given username is allowed to call.
                    foreach (string s in GetAllowedOpList(c.Resource.ToString()))
                    {
                        // Add claims to the list.
                        claims.Add(new Claim("http://example.org/claims/allowedoperation", s, Rights.PossessProperty));
                        Console.WriteLine("Claim added {0}", s);
                    }

            // Add claims to the evaluation context.
            evaluationContext.AddClaimSet(this, new DefaultClaimSet(this.Issuer,claims));

            // Record that claims have been added.
            customstate.ClaimsAdded = true;

            // Return true, which indicates this need not be called again.
            bRet = true;
        }
        else
        {
            // This point should not be reached, but just in case...
            bRet = true;
        }

        return bRet;
    }
    public ClaimSet Issuer
    {
        get { return ClaimSet.System; }
    }

    public string Id
    {
        get { return id; }
    }

    // This method returns a collection of action strings that indicate the
    // operations that the specified username is allowed to call.
    private IEnumerable<string> GetAllowedOpList(string username)
    {
        IList<string> ret = new List<string>();

        if (username == "test1")
        {
            ret.Add ( "http://Microsoft.ServiceModel.Samples/ICalculator/Add");
            ret.Add ("http://Microsoft.ServiceModel.Samples/ICalculator/Multiply");
            ret.Add("http://Microsoft.ServiceModel.Samples/ICalculator/Subtract");
        }
        else if (username == "test2")
        {
            ret.Add ( "http://Microsoft.ServiceModel.Samples/ICalculator/Add");
            ret.Add ("http://Microsoft.ServiceModel.Samples/ICalculator/Subtract");
        }
        return ret;
    }

    // internal class for state
    class CustomAuthState
    {
        bool bClaimsAdded;

        public CustomAuthState()
        {
            bClaimsAdded = false;
        }

        public bool ClaimsAdded { get { return bClaimsAdded; }
                                  set {  bClaimsAdded = value; } }
    }
}

Remarques

Implémentez l'interface IAuthorizationPolicy pour ajouter ou mapper un ensemble de revendications à un autre. Une stratégie d'autorisation examine un ensemble de revendications et ajoute des revendications supplémentaires en fonction de l'ensemble actuel. Par exemple, une stratégie d'autorisation peut évaluer une revendication qui contient la date de naissance, ajouter une revendication qui déclare que l'utilisateur a plus de 21 ans et ajouter une revendication Over21 (PlusDe21Ans) au EvaluationContext.

Les classes qui implémentent l'interface IAuthorizationPolicy n'accordent pas d'autorisation aux utilisateurs, mais elles permettent à la classe ServiceAuthorizationManager de le faire. Le ServiceAuthorizationManager appelle la méthode Evaluate pour chaque stratégie d'autorisation activée. La méthode Evaluate détermine si les revendications supplémentaires doivent être ajoutées pour l'utilisateur en fonction du contexte actuel. La méthode Evaluate d'une stratégie d'autorisation peut être appelée plusieurs fois, étant donné que les revendications sont ajoutées au EvaluationContext par d'autres stratégies d'autorisation. Lorsque toutes les stratégies d'autorisation activées sont effectuées, la classe ServiceAuthorizationManager accorde des autorisations en fonction de l'ensemble final de revendications. La classe ServiceAuthorizationManager crée ensuite un AuthorizationContext qui contient un ensemble immuable de revendications qui reflète ces autorisations.

Propriétés

Id

Obtient une chaîne qui identifie ce composant d'autorisation.

(Hérité de IAuthorizationComponent)
Issuer

Obtient un ensemble de revendications qui représente l'émetteur de la stratégie d'autorisation.

Méthodes

Evaluate(EvaluationContext, Object)

Évalue si un utilisateur répond aux spécifications pour cette stratégie d'autorisation.

S’applique à

Produit Versions
.NET Framework 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1

Voir aussi