當前位置: 首頁>>代碼示例>>C#>>正文


C# SecurityTokenHandler.WriteToken方法代碼示例

本文整理匯總了C#中System.IdentityModel.Tokens.SecurityTokenHandler.WriteToken方法的典型用法代碼示例。如果您正苦於以下問題:C# SecurityTokenHandler.WriteToken方法的具體用法?C# SecurityTokenHandler.WriteToken怎麽用?C# SecurityTokenHandler.WriteToken使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。


在下文中一共展示了SecurityTokenHandler.WriteToken方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。

示例1: WriteToken

/// <summary>
/// Serializes the given SecurityToken to the XmlWriter.
/// </summary>
/// <param name="writer">XmlWriter into which the token is serialized.</param>
/// <param name="token">SecurityToken to be serialized.</param>
public override void WriteToken( XmlWriter writer, SecurityToken token )
{
    SimpleWebToken simpleWebToken = token as SimpleWebToken;
    if ( simpleWebToken == null )
    {
        throw new SecurityTokenException("The given token is not of the expected type 'SimpleWebToken'.");
    }

    string signedToken = null;

    if ( String.IsNullOrEmpty( simpleWebToken.SerializedToken ) )
    {
        StringBuilder strBuilder = new StringBuilder();

        bool skipDelimiter = true;
        NameValueCollection tokenProperties = simpleWebToken.GetAllProperties();

        // remove the signature if present
        if ( String.IsNullOrEmpty( tokenProperties[SimpleWebTokenConstants.Signature] ) )
        {
            tokenProperties.Remove( SimpleWebTokenConstants.Signature );
        }

        foreach ( string key in tokenProperties.Keys )
        {
            if ( tokenProperties[key] != null )
            {
                if ( !skipDelimiter )
                {
                    strBuilder.Append( ParameterSeparator );
                }

                strBuilder.Append( String.Format(
                    CultureInfo.InvariantCulture,
                    "{0}={1}",
                    HttpUtility.UrlEncode( key ),
                    HttpUtility.UrlEncode( tokenProperties[key] ) ) );

                skipDelimiter = false;
            }
        }

        string serializedToken = strBuilder.ToString();

        SimpleWebTokenKeyIdentifierClause clause = new SimpleWebTokenKeyIdentifierClause(simpleWebToken.Audience);
        InMemorySymmetricSecurityKey securityKey = null;
        try
        {
            securityKey = (InMemorySymmetricSecurityKey)this.Configuration.IssuerTokenResolver.ResolveSecurityKey(clause);
        }
        catch (InvalidOperationException)
        {
            throw new SecurityTokenValidationException("A Symmetric key was not found for the given key identifier clause.");
        }
       
        // append the signature
        string signature = GenerateSignature( serializedToken, securityKey.GetSymmetricKey() );
        strBuilder.Append( String.Format(
                    CultureInfo.InvariantCulture,
                    "{0}{1}={2}",
                    ParameterSeparator,
                    HttpUtility.UrlEncode( SimpleWebTokenConstants.Signature ),
                    HttpUtility.UrlEncode( signature ) ) );

        signedToken = strBuilder.ToString();
    }
    else
    {
        // reuse the stored serialized token if present
        signedToken = simpleWebToken.SerializedToken;
    }

    string encodedToken = Convert.ToBase64String( Encoding.UTF8.GetBytes( signedToken ) );
    writer.WriteStartElement(BinarySecurityToken);
    writer.WriteAttributeString("Id", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd", token.Id);
    writer.WriteAttributeString( ValueType, SimpleWebTokenConstants.ValueTypeUri );
    writer.WriteAttributeString( EncodingType, Base64EncodingType );
    writer.WriteString( encodedToken );
    writer.WriteEndElement();
}
開發者ID:.NET開發者,項目名稱:System.IdentityModel.Tokens,代碼行數:85,代碼來源:SecurityTokenHandler.WriteToken

示例2: GenerateSignature

/// <summary>
/// Generates an HMACSHA256 signature for a given string and key.
/// </summary>
/// <param name="unsignedToken">The token to be signed.</param>
/// <param name="signingKey">The key used to generate the signature.</param>
/// <returns>The generated signature.</returns>
protected static string GenerateSignature(string unsignedToken, byte[] signingKey)
{
    using (HMACSHA256 hmac = new HMACSHA256(signingKey))
    {
        byte[] signatureBytes = hmac.ComputeHash(Encoding.ASCII.GetBytes(unsignedToken));
        string signature = HttpUtility.UrlEncode(Convert.ToBase64String(signatureBytes));

        return signature;
    }
}
開發者ID:.NET開發者,項目名稱:System.IdentityModel.Tokens,代碼行數:16,代碼來源:SecurityTokenHandler.WriteToken


注:本文中的System.IdentityModel.Tokens.SecurityTokenHandler.WriteToken方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。