【发布时间】:2019-01-10 05:19:03
【问题描述】:
我正在尝试使用 C# 在 Azure 函数中创建和读取(验证)JSON Web 令牌 (JWT)。我偶然发现了这个帖子:
https://www.codeproject.com/Tips/1208535/Create-And-Consume-JWT-Tokens-in-csharp
很好地概述了这个过程。由于对 Azure Functions 来说相对较新,因此我将“System.IdentityModel.Tokens.Jwt”的引用放在我的 project.json 文件中,如下所示:
{
"frameworks": {
"net46":{
"dependencies": {
"System.IdentityModel.Tokens.Jwt" : "5.0"
}
}
}
}
我使用的版本来自这篇文章:Namespaces for .NET JWT token validation: System vs. Microsoft,它谈到了 2016 年的版本控制问题。
很遗憾,这不起作用。对 SecurityAlgorithms、JwtHeader、JwtPayload、JwtSecurityToken 和 JwtSecurityTokenHandler 的引用都报告,“[run.csx] 找不到类型或命名空间名称‘类名’(您是否缺少 using 指令或程序集引用?)”。
进一步研究,我发现了这个页面:https://www.nuget.org/packages/System.IdentityModel.Tokens.Jwt/,它显示了 System.IdentityModel.Tokens.Jwt 的 Nuget 版本信息。在尝试了几个版本之后(通过更改我的 project.json 文件中的版本),我仍然没有运气让 Function App 识别我需要的类。
我认为这是一个版本问题。如果是这样,我在哪里可以确定哪个版本的“System.IdentityModel.Tokens.Jwt”与“net46”兼容?我已经好几年没有写过 C# 代码了(我是一名 Java 开发人员),所以我可能对版本控制假设有误。
顺便说一句,我的函数中的代码如下所示,它与以下代码示例完全相同:https://www.codeproject.com/Tips/1208535/Create-And-Consume-JWT-Tokens-in-csharp。唯一的区别是我将它包装在一个函数应用程序中。
using System.Net;
using Microsoft.Azure.Documents;
using Microsoft.Azure.Documents.Client;
using System.IdentityModel;
using System.Security;
using System.Text;
using System.IdentityModel.Tokens;
public static async Task<HttpResponseMessage> Run(HttpRequestMessage req, TraceWriter log)
{
// Define const Key this should be private secret key stored in some safe place
string key = "401b09eab3c013d4ca54922bb802bec8fd5318192b0a75f201d8b3727429090fb337591abd3e44453b954555b7a0812e1081c39b740293f765eae731f5a65ed1";
// Create Security key using private key above:
// not that latest version of JWT using Microsoft namespace instead of System
var securityKey = new Microsoft
.IdentityModel.Tokens.SymmetricSecurityKey(Encoding.UTF8.GetBytes(key));
// Also note that securityKey length should be >256b
// so you have to make sure that your private key has a proper length
//
var credentials = new Microsoft.IdentityModel.Tokens.SigningCredentials
(securityKey, SecurityAlgorithms.HmacSha256Signature);
// Finally create a Token
var header = new JwtHeader(credentials);
//Some PayLoad that contain information about the customer
var payload = new JwtPayload
{
{ "some ", "hello "},
{ "scope", "http://dummy.com/"},
};
//
var secToken = new JwtSecurityToken(header, payload);
var handler = new JwtSecurityTokenHandler();
// Token to String so you can use it in your client
var tokenString = handler.WriteToken(secToken);
// And finally when you received token from client
// you can either validate it or try to read
var token = handler.ReadJwtToken(tokenString);
return req.CreateResponse(HttpStatusCode.Created, "test");
}
所以,我的问题是:
- 我应该在我的项目文件中使用哪个版本的 System.IdentityModel.Tokens 和“net46”?
- 下次发生这种情况时,我如何确定哪些版本可以协同工作?
【问题讨论】:
-
如果您从软件包列表中删除
DocumentDB.Core会怎样?我想知道你是否遇到了冲突,所以安装不正确。 -
谢谢brettsam。我在发完这篇文章后确实删除了它。我还删除了对 Console 的引用。这些变化没有任何影响。
标签: azure c#-4.0 jwt azure-functions