【发布时间】:2016-11-08 22:29:05
【问题描述】:
我正在尝试使用包 System.IdentityModel.Tokens.Jwt 来生成令牌。我在网上找到了一些代码示例,非常简单,但后来我遇到了一个我无法弄清楚的错误。这是我正在使用的代码(为简洁起见稍作修改):
<%@ Application Language="C#" %>
<%@ Import Namespace="System" %>
<%@ Import Namespace="System.Text" %>
<%@ Import Namespace="System.Reflection" %>
<%@ Import Namespace="System.Collections" %>
<%@ Import Namespace="System.IdentityModel.Tokens" %>
<%@ Import Namespace="System.IdentityModel.Tokens.Jwt" %>
<%@ Import Namespace="System.Security.Claims" %>
<%@ Import Namespace="System.IdentityModel.Protocols.WSTrust" %>
<script runat="server">
public class TestClass
{
public static string GetJwtToken()
{
var tokenHandler = new JwtSecurityTokenHandler();
var input = "anyoldrandomtext";
var securityKey = new byte[input.Length * sizeof(char)];
Buffer.BlockCopy(input.ToCharArray(), 0, securityKey, 0, securityKey.Length);
var now = DateTime.UtcNow;
var tokenDescriptor = new SecurityTokenDescriptor
{
Subject = new ClaimsIdentity(new[]
{
new Claim( ClaimTypes.UserData,
"IsValid", ClaimValueTypes.String, "(local)" )
}),
TokenIssuerName = "self",
AppliesToAddress = "https://www.mywebsite.com",
Lifetime = new Lifetime(now, now.AddMinutes(60)),
SigningCredentials = new SigningCredentials(new InMemorySymmetricSecurityKey(securityKey),
"http://www.w3.org/2001/04/xmldsig-more#hmac-sha256",
"http://www.w3.org/2001/04/xmlenc#sha256"),
};
var token = tokenHandler.CreateToken(tokenDescriptor);
var tokenString = tokenHandler.WriteToken(token);
return tokenString;
}
}
</script>
我在第 113 行不断收到以下错误 (var token = tokenHandler.CreateToken(tokenDescriptor);):
参数 1:无法从 'System.IdentityModel.Tokens.SecurityTokenDescriptor' 转换为 'Microsoft.IdentityModel.Tokens.SecurityTokenDescriptor'
但是我在网上看到很多例子都和我做的一模一样。我还看到了这篇文章 (https://msdn.microsoft.com/en-us/library/jj157089(v=vs.110).aspx),其中陈述了以下内容:
在 WIF 3.5 中,所有 WIF 类都包含在 Microsoft.IdentityModel 程序集 (microsoft.identitymicrosoft.identitymodel.dll)。在 WIF 4.5 中,WIF 类已拆分为以下程序集: mscorlib (mscorlib.dll), System.IdentityModel (System.IdentityModel.dll), System.IdentityModel.Services (System.IdentityModel.Services.dll),以及 System.ServiceModel (System.ServiceModel.dll)。
WIF 3.5 类都包含在其中一个 Microsoft.IdentityModel 命名空间;例如, Microsoft.IdentityModel、Microsoft.IdentityModel.Tokens、 Microsoft.IdentityModel.Web 等等。在 WIF 4.5 中,WIF 类 现在分布在 System.IdentityModel 命名空间中, System.Security.Claims 命名空间和 System.ServiceModel.Security 命名空间。除了这次重组之外,一些 WIF 3.5 类 已在 WIF 4.5 中删除。
为了调试,我尝试将 Microsoft.* 命名空间用于 SecurityTokenDescriptor,然后我收到另一系列错误,指出 TokenIssuerName、AppliesToAddress 和 Lifetime 不是该类的有效属性。然而,当我在线查看文档时,似乎这些属性确实存在于 Microsoft.IdentityModel.Tokens.SecurityTokenDescriptor 上。然而,在我的 Visual Studio 中,当我为该类执行 Go to Definition 时,它们并不存在,这让我相信我的 Visual Studio 中存在某种配置问题。在我的包管理器中,它显示我安装了 Microsoft.IdentityModel.Tokens v5.0.0。我还将项目更改为 .NET 框架 4.5.1,因为 JWT 库需要它。除此之外,我不知道还能去哪里。
【问题讨论】: