2017-01-31 15 views
6

जबकि निम्नलिखित ट्यूटोरियल Create a RESTful API with authentication using Web API and Jwt मैं मुसीबत को संकलित करने के CustomJwtFormat वर्ग हो रही हूँ:'Microsoft.IdentityModel.Tokens.SymmetricSecurityKey' से परिवर्तित नहीं कर सकते करने के लिए 'Microsoft.IdentityModel.Tokens.SigningCredentials'

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.SigningCredential s '

इस के लिए खोज करने के बाद मैं इस अतः पद पाया:

ASP.NET v5 Multiple SigningCredentials

मैं जवाब पोस्ट में लेकिन कोई लाभ नहीं हुआ सिफारिश की कोशिश की है। मैं लिंक का पालन:

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

लेकिन फिर भी संघर्ष देख रहा हूँ। मुझे किस पैकेज और नेमस्पेस संयोजन का उपयोग करना चाहिए?

उत्तर

16

मैं एक ही समस्या में भाग गया। आपको System.IdentityModel.Tokens.Jwt का पुराना संस्करण उपयोग करना होगा।

खुला nuget पैकेज प्रबंधक कंसोल और चलाएँ:

Install-Package System.IdentityModel.Tokens.Jwt -Version 4.0.2.206221351 
1

मूल विधि:

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); 
संबंधित मुद्दे

 संबंधित मुद्दे