Share via


SecurityTokenService.GetScope(ClaimsPrincipal, RequestSecurityToken) Methode

Definition

Ruft ein Scope-Objekt ab, das Informationen über die vertrauende Seite enthält, die der angegebenen Anforderung zugeordnet ist. In Ihrer Implementierung der SecurityTokenService-Klasse müssen Sie diese Methode überschreiben.

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

Parameter

principal
ClaimsPrincipal

Ein ClaimsPrincipal, der den Client darstellt, von dem die Anforderung stammt.

request
RequestSecurityToken

Ein RequestSecurityToken, das die eingehende Anforderung (RST) darstellt.

Gibt zurück

Ein Scope, die RP-Informationen kapselt, die der Anforderung zugeordnet ist.

Beispiele

Das Codebeispiel, das in diesem Thema verwendet wird, stammt aus dem Custom Token Beispiel. Dieses Beispiel enthält benutzerdefinierte Klassen, die die Verarbeitung von Einfachen Webtoken (SWT) ermöglichen, sowie eine Implementierung eines passiven STS, der ein SWT-Token bereitstellen kann. Ein Beispiel für die Implementierung eines aktiven STS finden Sie im Federation Metadata Beispiel. Informationen zu diesen Beispielen und anderen für WIF verfügbaren Beispielen und zum Herunterladen finden Sie unter WIF-Codebeispielindex.

Im folgenden Codebeispiel wird eine Implementierung der GetScope-Methode veranschaulicht. Diese Implementierung überprüft, ob der Rp vom STS erkannt wird, überprüft die ReplyTo Adresse in der Anforderung und legt die Scope.ReplyToAddress Eigenschaft entsprechend fest und legt die Signierungs- und Verschlüsselungsanmeldeinformationen fest, die mit dem RP verwendet werden sollen, basierend auf Zertifikaten, die in der Datei hartcodiert sind.

// 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));
    }
}

Hinweise

Die GetScope -Methode wird von der Tokenausstellungspipeline nach der ValidateRequest -Methode aufgerufen und sollte ein für die eingehende Anforderung konfiguriertes Scope Objekt zurückgeben. (Die Tokenausstellungspipeline ist in der Issue -Methode implementiert.) Das Scope -Objekt kapselt Informationen zum RP, der der Sicherheitstokenanforderung (Security Token Request, RST) zugeordnet ist. Dies umfasst Informationen zu den Verschlüsselungs- und Signaturanmeldeinformationen, die mit dem RP verwendet werden sollen, und darüber, ob ausgestellte Token und/oder symmetrische Schlüssel in der Antwort verschlüsselt werden sollen. Einige typische Aufgaben, die in der GetScope -Methode ausgeführt werden, sind:

  • Bestimmen Sie, ob es sich bei dem RP, für den das Token vorgesehen ist, um einen erkannten RP handelt. Wie dies erreicht wird, hängt von Ihrer Implementierung ab. Wenn der beabsichtigte RP kein gültiger RP für diesen STS ist, sollte die -Methode einen InvalidRequestExceptionauslösen.

  • Bestimmen Sie die Signaturanmeldeinformationen, die in der Antwort (RSTR) verwendet werden sollen, und legen Sie die SigningCredentials -Eigenschaft entsprechend fest.

  • Bestimmen Sie, ob die Antwort und/oder alle enthaltenen symmetrischen Schlüssel verschlüsselt und die Anmeldeinformationen für die Verschlüsselung verwendet werden sollen. Legen Sie die TokenEncryptionRequiredEigenschaften , SymmetricKeyEncryptionRequiredund EncryptingCredentials entsprechend fest.

    Wichtig

    Standardmäßig sind die TokenEncryptionRequired Eigenschaften und SymmetricKeyEncryptionRequired festgelegt true , um zu verhindern, dass der STS Token ausgibt, die nicht sicher sind. Es wird empfohlen, diese Eigenschaften in einer Produktionsumgebung niemals auf false festzulegen.

  • Bestimmen Sie die Adresse, an die die Antwort zurückgegeben werden soll. Legen Sie entweder die - oder ReplyToAddress -AppliesToAddressEigenschaft entsprechend fest.

Hinweise für Ausführende

In Ihrer Implementierung der SecurityTokenService-Klasse müssen Sie diese Methode überschreiben.

Gilt für:

Weitere Informationen