在下面的教程使用Web和Jwt创建具有身份验证的RESTful API中,我很难获得要编译的CustomJwtFormat类:
using System.IdentityModel.Tokens;
using Microsoft.Owin.Security;
using Microsoft.Owin.Security.DataHandler.Encoder;
using Thinktecture.IdentityModel.Tokens;
namespace BooksAPI.Identity
{
public class CustomJwtFormat : ISecureDataFormat<AuthenticationTicket>
{
private static readonly byte[] _secret =
TextEncodings.Base64Url.Decode(ConfigurationManager.AppSettings["secret"]);
private readonly string _issuer;
public CustomJwtFormat(string issuer)
{
_issuer = issuer;
}
public string Protect(AuthenticationTicket data)
{
if (data == null)
throw new ArgumentNullException(nameof(data));
var signingKey = new HmacSigningCredentials(_secret);
var issued = data.Properties.IssuedUtc;
var expires = data.Properties.ExpiresUtc;
return new JwtSecurityTokenHandler().WriteToken(
new JwtSecurityToken( _issuer, null, data.Identity.Claims,
issued.Value.UtcDateTime, expires.Value.UtcDateTime, signingKey));
}
public AuthenticationTicket Unprotect(string protectedText) {
throw new NotImplementedException();
}
}
}我得到的构建错误是:
无法从'Thinktecture.IdentityModel.Tokens.HmacSigningCredentials‘转换为'Microsoft.IdentityModel.Tokens.SigningCredentials’
在搜索了这个之后,我找到了这个帖子:
ASP.NET v5多重SigningCredentials
我已经在回信中试过了这个建议,但没有结果。我跟踪了链接:
模棱两可的参考问题(Microsoft.AspNet.Identity & Microsoft.AspNet.Identity.Core)
但我还是看到了冲突。我应该使用哪个包和名称空间组合?
发布于 2017-03-01 19:22:17
我也遇到了同样的问题。您必须使用较早版本的System.IdentityModel.Tokens.Jwt。
打开nuget软件包管理控制台并运行:
Install-Package System.IdentityModel.Tokens.Jwt -Version 4.0.2.206221351发布于 2018-02-26 02:37:22
原始方法:
var signingKey = new HmacSigningCredentials(_secret);新方法:
var securityKey = new Microsoft.IdentityModel.Tokens.SymmetricSecurityKey(_secret);
var signingCredentials = new Microsoft.IdentityModel.Tokens.SigningCredentials(
securityKey,SecurityAlgorithms.HmacSha256Signature);
//---
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, signingCredentials);https://stackoverflow.com/questions/41963934
复制相似问题