gpt4 book ai didi

c# - System.IdentityModel.Tokens 和 Microsoft.IdentityModel.Tokens 之间的冲突

转载 作者:IT王子 更新时间:2023-10-29 03:51:56 30 4
gpt4 key购买 nike

我在使用 System.IdentityModel.Tokens 时遇到冲突:

using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.IdentityModel.Tokens;
using System.IdentityModel.Tokens.Jwt;
using System.Text;

public voidGenereToken()
{
const string sec = "401b09eab3c013d4ca54922bb802bec8fd5318192b0a75f201d8b3727429090fb337591abd3e44453b954555b7a0812e1081c39b740293f765eae731f5a65ed1";
var now = DateTime.UtcNow;
var securityKey = new InMemorySymmetricSecurityKey(Encoding.Default.GetBytes(sec));
var signingCredentials = new SigningCredentials(securityKey,
SecurityAlgorithms.RsaSha256Signature, SecurityAlgorithms.Sha256Digest);

var header = new JwtHeader(signingCredentials);

var payload = new JwtPayload
{
{"iss", "a5fgde64-e84d-485a-be51-56e293d09a69"},
{"scope", "https://example.com/ws"},
{"aud", "https://example.com/oauth2/v1"},
{"iat", now},
};

var secToken = new JwtSecurityToken(header, payload);

var handler = new JwtSecurityTokenHandler();
var tokenString = handler.WriteToken(secToken);
Console.writeLine(tokenString)
}

创建 header 时出现以下错误 (var header = new JwtHeader(signingCredentials);) :

Argument type 'System.IdentityModel.Tokens.SigningCredentials' is notassignable to parameter type'Microsoft.IdentityModel.Tokens.SigningCredentials'

我不明白,因为我的所有类型都引用 System.IdentityModel.Tokens。在文档中 JwtHeader Constructor需要 System.IdentityModel.Tokens.SigningCredentials

我不知道怎么回事...

最佳答案

System.IdentityModel.Tokens.Jwt 版本 5.0.0.0 依赖于 Microsoft.IdentityModel.Tokens。

您需要在 Microsoft.IdentityModel.Tokens 命名空间中使用 SigningCredentials。

例子:

using System;
using System.IdentityModel.Tokens;
using System.IdentityModel.Tokens.Jwt;
using System.Text;

public void voidGenereToken() {
const string sec = "401b09eab3c013d4ca54922bb802bec8fd5318192b0a75f201d8b3727429090fb337591abd3e44453b954555b7a0812e1081c39b740293f765eae731f5a65ed1";
var now = DateTime.UtcNow;
var securityKey = new Microsoft.IdentityModel.Tokens.SymmetricSecurityKey(Encoding.Default.GetBytes(sec));
var signingCredentials = new Microsoft.IdentityModel.Tokens.SigningCredentials(
securityKey,
SecurityAlgorithms.HmacSha256Signature);

var header = new JwtHeader(signingCredentials);

var payload = new JwtPayload
{
{"iss", "a5fgde64-e84d-485a-be51-56e293d09a69"},
{"scope", "https://example.com/ws"},
{"aud", "https://example.com/oauth2/v1"},
{"iat", now},
};

var secToken = new JwtSecurityToken(header, payload);

var handler = new JwtSecurityTokenHandler();
var tokenString = handler.WriteToken(secToken);
Console.WriteLine(tokenString);
}

关于c# - System.IdentityModel.Tokens 和 Microsoft.IdentityModel.Tokens 之间的冲突,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38338580/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com