Asp.net web api 无法从';Microsoft.IdentityModel.Tokens.SymmetricSecurity';至';Microsoft.IdentityModel.Tokens.SigningCredentials';
在学习教程时 我在编译Asp.net web api 无法从';Microsoft.IdentityModel.Tokens.SymmetricSecurity';至';Microsoft.IdentityModel.Tokens.SigningCredentials';,asp.net-web-api,oauth-2.0,asp.net-identity,owin,Asp.net Web Api,Oauth 2.0,Asp.net Identity,Owin,在学习教程时 我在编译CustomJwtFormat类时遇到问题: using System.IdentityModel.Tokens; using Microsoft.Owin.Security; using Microsoft.Owin.Security.DataHandler.Encoder; using Thinktecture.IdentityModel.Tokens; namespace BooksAPI.Identity { public class Custom
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();
}
}
}
使用System.IdentityModel.Tokens;
使用Microsoft.Owin.Security;
使用Microsoft.Owin.Security.DataHandler.Encoder;
使用Thinktecture.IdentityModel.Tokens;
名称空间API.Identity
{
公共类CustomJwtFormat:ISecureDataFormat
我在回复帖子中尝试了这个建议,但没有效果。我点击了以下链接:
但是我仍然看到了冲突。我应该使用哪个包和名称空间组合?我遇到了同样的问题。
您必须使用较旧版本的System.IdentityModel.Tokens.Jwt
打开nuget package manager控制台并运行:
Install-Package System.IdentityModel.Tokens.Jwt -Version 4.0.2.206221351
原始方法:
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);