【发布时间】:2021-05-29 15:06:47
【问题描述】:
我正在尝试使用 JWT 令牌验证用户。我在下面使用的代码在控制台应用程序中运行良好。但是当我想在我的天蓝色函数中应用它时,它给了我错误:
Could not load file or assembly Microsoft.IdentityModel.Tokens。我的解决方案中确实有另一个 azure 函数,但它不使用这个 NuGet 包。我已经看过这个链接:
Could not load file or assembly 'Microsoft.IdentityModel.Tokens, Version=5.2.0.0。我不能从中得到任何东西。那么我做错了什么?提前致谢
string key = "";
var securityKey = new Microsoft.IdentityModel.Tokens.SymmetricSecurityKey(Encoding.UTF8.GetBytes(key));
var credentials = new Microsoft.IdentityModel.Tokens.SigningCredentials(securityKey, SecurityAlgorithms.HmacSha256Signature);
var header = new JwtHeader(credentials);
var payload = new JwtPayload
{
{ "some ", "hello "},
{ "scope", "http://dummy.com/"},
};
var secToken = new JwtSecurityToken(header, payload);
var handler = new JwtSecurityTokenHandler();
var tokenString = handler.WriteToken(secToken);
var token = handler.ReadJwtToken(tokenString);
log.LogInformation(token.ToString());
【问题讨论】:
-
如果您无法加载库“无法加载文件或程序集 Microsoft.IdentityModel.Tokens”,则查看代码毫无意义。你的代码可能没问题。你提到的问题是关于解决这个问题,如果你忽略它,这个问题就不会消失。
-
你能发布错误的完整堆栈跟踪吗?如果依赖库有问题,也会出现此错误。
-
@stackunderflow
Executed login (Failed, Id=ac3f0995-cc77-48b3-acb1-8d787bb7e4e9, Duration=362ms) [2021-03-03T07:15:59.197Z] System.Private.CoreLib: Exception while executing function: login. DataEndpoint: Could not load file or assembly 'Microsoft.IdentityModel.Tokens, Version=6.8.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. System couldn't find specified file.