微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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.jb51.cc/aspnet/248088.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐