共用方式為


SecurityTokenService.GetScope(ClaimsPrincipal, RequestSecurityToken) 方法

定義

取得 Scope 物件,其中包含與所指定的要求 (RST) 相關聯之信賴憑證者 (RP) 的相關資訊。 您必須在 SecurityTokenService 類別的實作中覆寫此方法。

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

參數

principal
ClaimsPrincipal

ClaimsPrincipal,表示提出要求的用戶端。

request
RequestSecurityToken

表示傳入要求 (RST) 的 RequestSecurityToken

傳回

封裝與要求相關聯之 RP 資訊的 Scope

範例

本主題中使用的程式代碼範例取自 Custom Token 範例。 此範例提供自定義類別,可讓您處理簡單的 Web 令牌 (SWT) ,並包含能夠提供服務 SWT 令牌的被動 STS 實作。 如需如何實作使用中 STS 的範例,您可以看到 Federation Metadata 範例。 如需這些範例和其他適用於 WIF 之範例的相關信息,以及下載這些範例的位置,請參閱 WIF 程式代碼範例索引

下列程式碼範例示範 GetScope 方法的實作。 此實作會驗證 STS 可辨識 RP、驗證 ReplyTo 要求中的位址,並據以設定 Scope.ReplyToAddress 屬性,以及根據檔案中硬式編碼的憑證,設定要與 RP 搭配使用的簽署和加密認證。

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

備註

方法 GetScope 會在方法之後 ValidateRequest 從令牌發行管線呼叫,而且應該傳回 Scope 針對傳入要求設定的物件。 (方法中 Issue 實作令牌發行管線。) 物件 Scope 會封裝與安全性令牌要求相關聯的 RP 相關信息, (RST) 。 這包括加密和簽署認證的相關信息,這些認證要與 RP 搭配使用,以及是否要在回應中加密任何發行的令牌和/或對稱密鑰。 方法中 GetScope 執行的一些典型工作如下:

給實施者的注意事項

您必須在 SecurityTokenService 類別的實作中覆寫此方法。

適用於

另請參閱