Sdílet prostřednictvím


IAuthorizationPolicy.Evaluate(EvaluationContext, Object) Metoda

Definice

Vyhodnotí, jestli uživatel splňuje požadavky pro tyto zásady autorizace.

public:
 bool Evaluate(System::IdentityModel::Policy::EvaluationContext ^ evaluationContext, System::Object ^ % state);
public bool Evaluate (System.IdentityModel.Policy.EvaluationContext evaluationContext, ref object state);
abstract member Evaluate : System.IdentityModel.Policy.EvaluationContext * obj -> bool
Public Function Evaluate (evaluationContext As EvaluationContext, ByRef state As Object) As Boolean

Parametry

evaluationContext
EvaluationContext

Obsahuje EvaluationContext sadu deklarací identity, kterou zásady autorizace vyhodnocují.

state
Object

A Object, předaný odkazem, který představuje vlastní stav pro tuto zásadu autorizace.

Návraty

false pokud je nutné metodu Evaluate(EvaluationContext, Object) pro tuto zásadu autorizace volat, pokud jsou další deklarace identity přidány do evaluationContextjiných zásad autorizace. true V opačném případě tato zásada autorizace nevyžaduje žádné další vyhodnocení.

Příklady

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 Function Evaluate(ByVal evaluationContext As EvaluationContext, _
     ByRef state As Object) As Boolean Implements IAuthorizationPolicy.Evaluate

    Dim bRet As Boolean = False
    Dim customstate As CustomAuthState = Nothing

    ' If state is null, then this method has not been called before, so 
    ' set up a custom state.
    If state Is Nothing Then
        customstate = New CustomAuthState()
        state = customstate
    Else
        customstate = CType(state, CustomAuthState)
    End If
    Console.WriteLine("Inside MyAuthorizationPolicy::Evaluate")

    ' If the claims have not been added yet...
    If Not customstate.ClaimsAdded Then
        ' Create an empty list of Claims
        Dim claims As New List(Of Claim)

        ' Iterate through each of the claimsets in the evaluation context.
        Dim cs As ClaimSet
        For Each cs In evaluationContext.ClaimSets
            ' Look for Name claims in the current claim set.
            Dim c As Claim
            For Each c In cs.FindClaims(ClaimTypes.Name, Rights.PossessProperty)
                ' Get the list of operations the given username is allowed to call.
                Dim s As String
                For Each 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)
                Next s
            Next c
        Next cs 
        
        ' Add claims to the evaluation context.
        evaluationContext.AddClaimSet(Me, New DefaultClaimSet(Me.Issuer, claims))

        ' Record that claims have been added.
        customstate.ClaimsAdded = True

        ' Return true, which indicates the method need not to be called again.
        bRet = True
    Else
        ' Should never get here, but just in case...
        bRet = True
    End If


    Return bRet

End Function 'Evaluate

Poznámky

Poznámka

Implementátoři IAuthorizationPolicy rozhraní by měli očekávat Evaluate , že metoda bude volána vícekrát různými vlákny.

Implementátoři IAuthorizationPolicy rozhraní mohou pomocí parametru state sledovat stav mezi voláními Evaluate metody. Pokud je objekt stavu nastaven uvnitř daného volání Evaluate metody, je stejná instance objektu předána každému a každému následnému Evaluate volání metody v aktuálním procesu vyhodnocení.

Platí pro