SecurityTokenService.GetScope(ClaimsPrincipal, RequestSecurityToken) Metod

Definition

Hämtar ett Scope objekt som innehåller information om den förlitande part (RP) som är associerad med den angivna begäran (RST). Du måste åsidosätta den här metoden i implementeringen av SecurityTokenService klassen.

protected:
 abstract System::IdentityModel::Scope ^ GetScope(System::Security::Claims::ClaimsPrincipal ^ principal, System::IdentityModel::Protocols::WSTrust::RequestSecurityToken ^ request);
protected abstract System.IdentityModel.Scope GetScope(System.Security.Claims.ClaimsPrincipal principal, System.IdentityModel.Protocols.WSTrust.RequestSecurityToken request);
abstract member GetScope : System.Security.Claims.ClaimsPrincipal * System.IdentityModel.Protocols.WSTrust.RequestSecurityToken -> System.IdentityModel.Scope
Protected MustOverride Function GetScope (principal As ClaimsPrincipal, request As RequestSecurityToken) As Scope

Parametrar

principal
ClaimsPrincipal

En ClaimsPrincipal som representerar klienten som skickar begäran.

request
RequestSecurityToken

En RequestSecurityToken som representerar den inkommande begäran (RST).

Returer

En Scope som kapslar in den RP-information som är associerad med begäran.

Exempel

Kodexemplet som används i det här avsnittet tas från Custom Token exemplet. Det här exemplet innehåller anpassade klasser som möjliggör bearbetning av enkla webbtoken (SWT) och innehåller en implementering av en passiv STS som kan hantera en SWT-token. Ett exempel på hur du implementerar en aktiv STS kan du se Federation Metadata exemplet. Information om dessa exempel och andra exempel som är tillgängliga för WIF och var du kan ladda ned dem finns i WIF Code Sample Index.

I följande kodexempel visas en implementering av GetScope metoden. Den här implementeringen verifierar att RP identifieras av STS, verifierar ReplyTo adressen i begäran och anger Scope.ReplyToAddress egenskapen i enlighet med detta och anger signerings- och krypteringsautentiseringsuppgifterna som ska användas med RP baserat på certifikat som är hårdkodade i filen.

// Certificate Constants
private const string SIGNING_CERTIFICATE_NAME = "CN=localhost";
private const string ENCRYPTING_CERTIFICATE_NAME = "CN=localhost";

private SigningCredentials _signingCreds;
private EncryptingCredentials _encryptingCreds;
// Used for validating applies to address, set to URI used in RP app of application, could also have been done via config
private string _addressExpected = "http://localhost:19851/";
/// <summary>
/// This method returns the configuration for the token issuance request. The configuration
/// is represented by the Scope class. In our case, we are only capable of issuing a token to a
/// single RP identity represented by the _encryptingCreds field.
/// </summary>
/// <param name="principal">The caller's principal</param>
/// <param name="request">The incoming RST</param>
/// <returns></returns>
protected override Scope GetScope(ClaimsPrincipal principal, RequestSecurityToken request)
{
    // Validate the AppliesTo address
    ValidateAppliesTo( request.AppliesTo );

    // Create the scope using the request AppliesTo address and the RP identity
    Scope scope = new Scope( request.AppliesTo.Uri.AbsoluteUri, _signingCreds );

    if (Uri.IsWellFormedUriString(request.ReplyTo, UriKind.Absolute))
    {
        if (request.AppliesTo.Uri.Host != new Uri(request.ReplyTo).Host)
            scope.ReplyToAddress = request.AppliesTo.Uri.AbsoluteUri;
        else
            scope.ReplyToAddress = request.ReplyTo;
    }
    else
    {
        Uri resultUri = null;
        if (Uri.TryCreate(request.AppliesTo.Uri, request.ReplyTo, out resultUri))
            scope.ReplyToAddress = resultUri.AbsoluteUri;
        else
            scope.ReplyToAddress = request.AppliesTo.Uri.ToString() ;
    }

    // Note: In this sample app only a single RP identity is shown, which is localhost, and the certificate of that RP is 
    // populated as _encryptingCreds
    // If you have multiple RPs for the STS you would select the certificate that is specific to 
    // the RP that requests the token and then use that for _encryptingCreds
    scope.EncryptingCredentials = _encryptingCreds;

    return scope;
}
/// <summary>
/// Validates the appliesTo and throws an exception if the appliesTo is null or appliesTo contains some unexpected address.
/// </summary>
/// <param name="appliesTo">The AppliesTo parameter in the request that came in (RST)</param>
/// <returns></returns>
void ValidateAppliesTo(EndpointReference appliesTo)
{
    if (appliesTo == null)
    {
        throw new InvalidRequestException("The appliesTo is null.");
    }

    if (!appliesTo.Uri.Equals(new Uri(_addressExpected)))
    {
        throw new InvalidRequestException(String.Format("The relying party address is not valid. Expected value is {0}, the actual value is {1}.", _addressExpected, appliesTo.Uri.AbsoluteUri));
    }
}

Kommentarer

Metoden GetScope anropas från pipelinen för tokenutfärdande efter ValidateRequest metoden och ska returnera ett Scope objekt som konfigurerats för den inkommande begäran. (Pipelinen för tokenutfärdning implementeras i Issue -metoden.) Objektet Scope kapslar in information om den RP som är associerad med begäran om säkerhetstoken (RST). Detta inkluderar information om krypterings- och signeringsautentiseringsuppgifterna som ska användas med RP och om du vill kryptera utfärdade token och/eller symmetriska nycklar i svaret. Några vanliga uppgifter som utförs i GetScope metoden är:

  • Avgör om den RP som token är avsedd för är en identifierad RP. Hur detta görs beror på din implementering. Om den avsedda RP:en inte är en giltig RP för denna STS ska metoden utlösa en InvalidRequestException.

  • Fastställ de autentiseringsuppgifter för signering som ska användas i svaret (RSTR) och ange egenskapen SigningCredentials i enlighet med detta.

  • Avgör om svaret och/eller eventuella inkluderade symmetriska nycklar ska krypteras och vilka autentiseringsuppgifter som ska användas för kryptering. TokenEncryptionRequiredAnge egenskaperna , SymmetricKeyEncryptionRequiredoch EncryptingCredentials därefter.

    Important

    Som standard TokenEncryptionRequired är egenskaperna och SymmetricKeyEncryptionRequired inställda true för att förhindra att STS utfärdar token som inte är säkra. Vi rekommenderar att dessa egenskaper aldrig anges till false i en produktionsmiljö.

  • Fastställ adressen som svaret ska returneras till. Ange antingen AppliesToAddress egenskapen eller ReplyToAddress i enlighet med detta.

Anteckningar till implementerare

Du måste åsidosätta den här metoden i implementeringen av SecurityTokenService klassen.

Gäller för

Se även