首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >无法从'Microsoft.IdentityModel.Tokens.SymmetricSecurityKey‘转换为'Microsoft.IdentityModel.Tokens.SigningCredentials’

无法从'Microsoft.IdentityModel.Tokens.SymmetricSecurityKey‘转换为'Microsoft.IdentityModel.Tokens.SigningCredentials’
EN

Stack Overflow用户
提问于 2017-01-31 17:33:59
回答 2查看 11.4K关注 0票数 11

在下面的教程使用Web和Jwt创建具有身份验证的RESTful API中,我很难获得要编译的CustomJwtFormat类:

代码语言:javascript
运行
复制
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)

但我还是看到了冲突。我应该使用哪个包和名称空间组合?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-03-01 19:22:17

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

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

代码语言:javascript
运行
复制
Install-Package System.IdentityModel.Tokens.Jwt -Version 4.0.2.206221351
票数 22
EN

Stack Overflow用户

发布于 2018-02-26 02:37:22

原始方法:

代码语言:javascript
运行
复制
var signingKey = new HmacSigningCredentials(_secret);

新方法:

代码语言:javascript
运行
复制
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);
票数 34
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41963934

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档