Mat*_*t W 8 oauth-2.0 asp.net-web-api owin asp.net-identity
在遵循教程
使用Web API和Jwt创建具有身份验证的RESTful API时,我无法让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();
}
}
}
Run Code Online (Sandbox Code Playgroud)
我得到的构建错误是:
无法从'Thinktecture.IdentityModel.Tokens.HmacSigningCredentials'转换为'Microsoft.IdentityModel.Tokens.SigningCredentials'
搜索到这个后,我发现了这个帖子:
ASP.NET v5 Multiple SigningCredentials
我在答复帖中尝试过这个建议,但无济于事.我按照链接:
模糊参考问题(Microsoft.AspNet.Identity和Microsoft.AspNet.Identity.Core)
但我仍然看到了冲突.我应该使用哪个包和名称空间组合?
Tyl*_*ler 20
我遇到了同样的问题.您必须使用较旧版本的System.IdentityModel.Tokens.Jwt.
打开nuget包管理器控制台并运行:
Install-Package System.IdentityModel.Tokens.Jwt -Version 4.0.2.206221351
Run Code Online (Sandbox Code Playgroud)
小智 18
原方法:
var signingKey = new HmacSigningCredentials(_secret);
Run Code Online (Sandbox Code Playgroud)
新方法:
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);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6166 次 |
| 最近记录: |