我正在尝试在Web API 2.2项目中使用JWT格式。我已按照this article - 'Implement OAuth JSON Web Tokens Authentication in ASP.NET Web API and Identity 2.1"
上的步骤进行操作 下载了源代码但是我使用的是.NET 4.6.1,所以我的NuGet包管理器显示了从版本4.0到5.0的包“System.IdentityModel.Tokens.Jwt”的更新,在我更新这个包之后,我的项目将无法编译因为我在CustomJwtFormat类上遇到“Missing reference error”:
这是我的CustomJwtFormat类的代码:
using Microsoft.Owin.Security;
using Microsoft.Owin.Security.DataHandler.Encoder;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.IdentityModel.Tokens;
using System.Linq;
using System.Web;
using Thinktecture.IdentityModel.Tokens;
namespace AspNetIdentity.WebApi.Providers
{
public class CustomJwtFormat : ISecureDataFormat<AuthenticationTicket>
{
private readonly string _issuer = string.Empty;
public CustomJwtFormat(string issuer) {_issuer = issuer;}
public string Protect(AuthenticationTicket data)
{
if (data == null)
{throw new ArgumentNullException("data");}
string audienceId = ConfigurationManager.AppSettings["as:AudienceId"];
string symmetricKeyAsBase64 = ConfigurationManager.AppSettings["as:AudienceSecret"];
var keyByteArray = TextEncodings.Base64Url.Decode(symmetricKeyAsBase64);
var signingKey = new HmacSigningCredentials(keyByteArray);
var issued = data.Properties.IssuedUtc;
var expires = data.Properties.ExpiresUtc;
var token = new JwtSecurityToken(_issuer, audienceId, data.Identity.Claims, issued.Value.UtcDateTime, expires.Value.UtcDateTime, signingKey);
var handler = new JwtSecurityTokenHandler();
var jwt = handler.WriteToken(token);
return jwt;
}
public AuthenticationTicket Unprotect(string protectedText)
{
throw new NotImplementedException();
}
}
}
我想知道是否有人在4.6.1更新所有NuGet包时使用这种方法。