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

标签 asp.net-web-api oauth-2.0 asp.net-identity owin

在遵循教程时 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)

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

最佳答案

原方法:

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);

关于asp.net-web-api - 无法从 'Microsoft.IdentityModel.Tokens.SymmetricSecurityKey' 转换为 'Microsoft.IdentityModel.Tokens.SigningCredentials',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41963934/

相关文章:

调用 web api 时 C# 不支持授权类型

coldfusion - Google 登录与 ColdFusion 的集成

java - 如何在成功验证后使用 Microsoft OAuth 获取用户信息

asp.net-mvc - MVC Identity (2.0.1) 中的 regenerateIdentity/validateInterval 持续时间后忽略 ExpireTimeSpan

asp.net-mvc-4 - MVC4 WebApi OData Delta<T> 未填充 GUID 或 Int 字段

c# - 在 mvc asp.net 的浏览器中找不到 API Controller

ruby-on-rails - "application_id"上的门卫 ActiveRecord::NotNullViolation(密码授予)

asp.net-identity - Microsoft.AspNet.Identity 3 中 ClaimsAuthorizationAttribute 的等效项

c# - 如何在同一项目中运行 IdentityServer 和 WebAPI

azure - 只有一个选项 : Connect to an existing user store in the cloud