当前位置: 首页>>代码示例>>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;未经允许,请勿转载。