asp.net-web-api – 无法从’Microsoft.IdentityModel.Tokens.SymmetricSecurityKey’转换为’Microsoft.IdentityModel.Tokens.SigningCredentials’

前端之家收集整理的这篇文章主要介绍了asp.net-web-api – 无法从’Microsoft.IdentityModel.Tokens.SymmetricSecurityKey’转换为’Microsoft.IdentityModel.Tokens.SigningCredentials’前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
按照教程
Create a RESTful API with authentication using Web API and Jwt我无法编译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();
        }
    }
}

我得到的构建错误是:

Cannot convert from
‘Thinktecture.IdentityModel.Tokens.HmacSigningCredentials’ to
‘Microsoft.IdentityModel.Tokens.SigningCredentials’

搜索到这个后,我发现了这个帖子:

ASP.NET v5 Multiple SigningCredentials

我在答案帖子中尝试过这个建议,但无济于事.我按照链接

Ambiguous reference issue (Microsoft.AspNet.Identity & Microsoft.AspNet.Identity.Core)

但我仍然看到了冲突.我应该使用哪个包和命名空间组合?

解决方法

我遇到了同样的问题.
您必须使用较旧版本的System.IdentityModel.Tokens.Jwt.

打开nuget包管理器控制台并运行:

Install-Package System.IdentityModel.Tokens.Jwt -Version 4.0.2.206221351
原文链接:https://www.f2er.com/aspnet/248088.html

猜你在找的asp.Net相关文章