本文整理汇总了C#中System.Security.Cryptography.X509Certificates.X509Certificate2.AssertNotNull方法的典型用法代码示例。如果您正苦于以下问题:C# X509Certificate2.AssertNotNull方法的具体用法?C# X509Certificate2.AssertNotNull怎么用?C# X509Certificate2.AssertNotNull使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Security.Cryptography.X509Certificates.X509Certificate2
的用法示例。
在下文中一共展示了X509Certificate2.AssertNotNull方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: SetJwtAuthorizationHeader
/// <summary>
/// Sets a JWT authorization header on the default request headers of an <see cref="HttpClient"/>.
/// </summary>
/// <param name="client">The client for which to set the authorization header.</param>
/// <param name="signingCertificate">The signing certificate to sign the token.</param>
/// <param name="appliesToAddress">The address for which the token is considered valid.</param>
/// <param name="claims">The claims that define the user. Leave null for an anonymous user.</param>
/// <param name="tokenIssuerName">Name of the token issuer. Defaults to "self".</param>
/// <param name="tokenDuration">
/// The token duration for which it's considered valid. Defaults to 2 hours.
/// </param>
/// <exception cref="ArgumentNullException">
/// <paramref name="signingCertificate"/> is <see langword="null"/>.
/// </exception>
/// <exception cref="ArgumentException">
/// <paramref name="appliesToAddress"/> is <see langword="null"/> or empty.
/// </exception>
/// <exception cref="ArgumentException">
/// <paramref name="tokenIssuerName"/> is <see langword="null"/> or empty.
/// </exception>
public static void SetJwtAuthorizationHeader(
this HttpClient client,
X509Certificate2 signingCertificate,
string appliesToAddress,
IEnumerable<Claim> claims = null,
string tokenIssuerName = "self",
TimeSpan? tokenDuration = null)
{
signingCertificate.AssertNotNull("signingCertificate");
appliesToAddress.AssertNotNullOrWhitespace("appliesToAddress");
tokenIssuerName.AssertNotNullOrWhitespace("tokenIssuerName");
var now = DateTime.UtcNow;
var tokenHandler = new JwtSecurityTokenHandler();
var tokenDescriptor = new SecurityTokenDescriptor
{
Subject = new ClaimsIdentity(claims),
TokenIssuerName = tokenIssuerName,
AppliesToAddress = appliesToAddress,
Lifetime = new Lifetime(now, now.Add(tokenDuration ?? TimeSpan.FromHours(2))),
SigningCredentials = new X509SigningCredentials(signingCertificate)
};
SecurityToken token = tokenHandler.CreateToken(tokenDescriptor);
string tokenString = tokenHandler.WriteToken(token);
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", tokenString);
}