标签: identitymodel

IDX10503:签名验证失败。托肯没有孩子。尝试过的键:“System.Text.StringBuilder”

我有以下 JWT 令牌,

eyJhbGciOiJIUzUxMiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJjbGllbnRpZCIsImF1ZCI6ImNsaWVudGlkIiwic3ViIjoiMTIzIiwiYSI6IjQ1NiIsImlhdCI6MTYyMTc5OTU5OCwiZXhwIjoxNjIxNzk5NjU4fQ.hglbX63zhPwTOsB-zSiOMfxEKl5OaIk6zX1o9-LEhP3nro8fa5_3QyIH7I5971j-xuO1bccX1TOh0kNcQ-ACAg
Run Code Online (Sandbox Code Playgroud)

这是使用生成的,

    public static string GenerateToken(string key, string a1, string a2)
    {
        var securityKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(key));
        var token = new JwtSecurityToken(
            claims: new Claim[]
            {
            new Claim(JwtRegisteredClaimNames.Iss, "clientid"),
            new Claim(JwtRegisteredClaimNames.Aud, "clientid"),
            new Claim(JwtRegisteredClaimNames.Sub, a1),
            new Claim("a", a2),
            new Claim(JwtRegisteredClaimNames.Iat, DateTimeOffset.UtcNow.ToUnixTimeSeconds().ToString(), ClaimValueTypes.Integer64),
            },
            //notBefore: new DateTimeOffset(DateTime.Now).DateTime,
            expires: new DateTimeOffset(DateTime.Now.AddMinutes(1)).DateTime,
            signingCredentials: new SigningCredentials(securityKey, SecurityAlgorithms.HmacSha512)
        );

        return new JwtSecurityTokenHandler().WriteToken(token);
    }

var key = "Ym7AD3OT2kpuIRcVAXCweYhV64B0Oi9ETAO6XRbqB8LDL3tF4bMk9x/59PljcGbP5v38BSzCjD1VTwuO6iWA8uzDVAjw2fMNfcT2/LyRlMOsynblo3envlivtgHnKkZj6HqRrG5ltgwy5NsCQ7WwwYPkldhLTF+wUYAnq28+QnU=";
// Key is test                
var token = GenerateToken(key, "123", "456");
Run Code Online (Sandbox Code Playgroud)

获得令牌后,我使用下面的代码进行验证,

var key = "Ym7AD3OT2kpuIRcVAXCweYhV64B0Oi9ETAO6XRbqB8LDL3tF4bMk9x/59PljcGbP5v38BSzCjD1VTwuO6iWA8uzDVAjw2fMNfcT2/LyRlMOsynblo3envlivtgHnKkZj6HqRrG5ltgwy5NsCQ7WwwYPkldhLTF+wUYAnq28+QnU="; …
Run Code Online (Sandbox Code Playgroud)

c# security jwt identitymodel

15
推荐指数
2
解决办法
5万
查看次数

为什么从 v6 升级到 IdentityModel v7 后出现 IDX20803 错误?

升级Microsoft.IdentityModel.TokensSystem.IdentityModel.Tokens.Jwt到后7.0.0,我收到此错误:

IDX20803:无法从“https://example.com/realms/Development/.well-known/openid-configuration”获取配置。

无法从程序集“Microsoft.IdentityModel.Tokens,Version=7.0.0.0,Culture=neutral,PublicKeyToken=31bf3856ad364e35”加载类型“Microsoft.IdentityModel.Json.JsonConvert”。无法从程序集“Microsoft.IdentityModel.Tokens,Version=7.0.0.0,Culture=neutral,PublicKeyToken=31bf3856ad364e35”加载类型“Microsoft.IdentityModel.Json.JsonConvert”。=> Microsoft.IdentityModel.Json.JsonConvert

更新之前,我的包参考是:

<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="7.0.10" />
<PackageReference Include="Microsoft.IdentityModel.Tokens" Version="6.32.3" />
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="6.32.3" />
<PackageReference Include="System.Text.Json" Version="7.0.3" />
Run Code Online (Sandbox Code Playgroud)

更新后,我的包参考现在是:

<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="7.0.11" />
<PackageReference Include="Microsoft.IdentityModel.Tokens" Version="7.0.0" />
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="7.0.0" />
<PackageReference Include="System.Text.Json" Version="7.0.3" />
Run Code Online (Sandbox Code Playgroud)

有什么问题吗?

c# jwt identitymodel

15
推荐指数
1
解决办法
3370
查看次数

端点属于不同的权限

尝试将 Azure AD 用作具有IdentityModel包的 OpenID 提供程序

但问题是它会产生错误的端点配置

var client = new HttpClient();

const string identityUrl = "https://login.microsoftonline.com/00edae13-e792-4bc1-92ef-92a02ec1d939/v2.0";
const string restUrl = "https://localhost:44321";

var disco = await client.GetDiscoveryDocumentAsync(identityUrl);
if (disco.IsError)
{
    Console.WriteLine(disco.Error); 
    return;
}
Run Code Online (Sandbox Code Playgroud)

返回错误

端点属于不同的权限: https://login.microsoftonline.com/00edae13-e792-4bc1-92ef-92a02ec1d939/oauth2/v2.0/authorize

openid 配置输出是

{"authorization_endpoint":"https://login.microsoftonline.com/00edae13-e792-4bc1-92ef-92a02ec1d939/oauth2/v2.0/authorize",
"token_endpoint":"https://login.microsoftonline.com/00edae13-e792-4bc1-92ef-92a02ec1d939/oauth2/v2.0/token" ... }
Run Code Online (Sandbox Code Playgroud)

oauth2添加在 tenatID 和版本之间。我想这就是 openid 元数据验证失败的原因。

是否可以将 AzureAD 配置为返回 openid-configuration 的正确元数据?

问候

azure-authentication identitymodel

10
推荐指数
1
解决办法
4873
查看次数

使用 IdentityModel 4.1.1 的刷新令牌请求访问令牌

我正在使用 IdentityModel 4.1.1 for OAuth2.0 现在我在创建 TokenClient 实例期间卡住了,该实例用于使用刷新令牌请求新的访问令牌。

这是我正在做的代码,

TokenClientOptions clientOptions = new TokenClientOptions();
clientOptions.ClientId = _configDetails.Where(x => x.Key == "ClientId").Select(x => x.Value).FirstOrDefault().ToString();
clientOptions.ClientSecret = _configDetails.Where(x => x.Key == "ClientSecret").Select(x => x.Value).FirstOrDefault().ToString();

//Create token client object object
var tokenClient = new TokenClient("?",clientOptions); //Here I need help what I have to pass as first parameter?


TokenResponse refereshtokenCallResponse = await tokenClient.RequestRefreshTokenAsync(token.RefreshToken);
Run Code Online (Sandbox Code Playgroud)

下面是IdentityModel4.1.1 pkg提供的TokenClient类,

public class TokenClient
{
    public TokenClient(HttpMessageInvoker client, TokenClientOptions options);
    public TokenClient(Func<HttpMessageInvoker> client, TokenClientOptions options);

    public Task<TokenResponse> RequestAuthorizationCodeTokenAsync(string code, string …
Run Code Online (Sandbox Code Playgroud)

c# asp.net-mvc identitymodel

6
推荐指数
1
解决办法
6646
查看次数

无法加载类型“IdentityModel.Client.DiscoveryClient”

我尝试使用身份服务器中的访问令牌授权的方法向 API 发出请求,但是当我使用标头中的承载令牌发送请求时,我的请求失败,结果将我标记为以下错误:

无法从程序集“IdentityModel,Version=4.0.0.0,Culture=neutral,PublicKeyToken=e7877f4675df049f”加载类型“IdentityModel.Client.DiscoveryClient”。在 IdentityServer4.AccessTokenValidation.IdentityServerAuthenticationOptions.ConfigureJwtBearer

c# asp.net identitymodel

6
推荐指数
0
解决办法
2313
查看次数

无法找到 IdentityServer4 的 DiscoveryClient

尝试访问发现客户端以访问其他端点并遵循 http://docs.identityserver.io/en/aspnetcore1/endpoints/discovery.html

在 .Net 7.5 MVC 应用程序中安装了 IdentityModel nuget 包。但无法找到DiscoveryClient.

var discoveryClient = new DiscoveryClient("https://demo.identityserver.io");
var doc = await discoveryClient.GetAsync();
Run Code Online (Sandbox Code Playgroud)

有什么变化IdentitymodelIdentityServer4

此外,无法找到“Tokenclient”的参数。

asp.net-mvc identityserver3 identityserver4 identitymodel

5
推荐指数
2
解决办法
4320
查看次数

IdentityModel v.4.3.0 中的 RequestResourceOwnerPasswordAsync 被什么取代了?

我正在尝试获取资源所有者信息,但 RequestResourceOwnerPasswordAsync 方法在 v4.3.0 的 TokenClient 类中不可用。我搜索了文档,但没有找到此方法的替代方法。以下是我的代码:

在此输入图像描述

asp.net-web-api asp.net-core identityserver4 identitymodel

5
推荐指数
1
解决办法
2350
查看次数

.Net 6 的 System.IdentityModel.Tokens.Jwt 的自定义对象 JSON 序列化实现是否存在问题?

我们从 .Net Core (dotnet core) 3.1 迁移到 .Net 6。我们使用 System.IdentityModel.Tokens.Jwt 创建有效负载并使用该有效负载生成安全令牌。

我们的应用程序尚未从Newtonsoft.Json迁移到System.Text.Json,因为许多非标准属性序列化目前更倾向于前者。自定义声明值包含一个之前通过遵守Startup.cs配置中指定的有关 JSON 序列化的驼峰命名约定解析器正确序列化的对象。

我们从System.IdentityModel.Tokens.Jwt版本5.5.0升级到版本6.16.0,序列化的行为有所不同。

我们混合使用了 IdentityModel 众所周知的声明和自定义声明。自定义声明是唯一一个作为对象的声明,也是唯一一个具有这种行为方式的声明。所有其他声明都是原始类型,并按照指定和预期写入令牌。

这是不起作用的代码示例:

 var payload = new JwtPayload()
            {
                {JwtRegisteredClaimNames.Iss, issuer},
                {JwtRegisteredClaimNames.Iat, now},
                {JwtRegisteredClaimNames.Nbf, now},
                {JwtRegisteredClaimNames.Exp, exp},
                {JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString("N")},
                {"role", user.UserType},
                {"customClaim", customClaimObjectInstance }
            };

var jwt = new JwtSecurityToken(_jwtHeader, payload);

/* Token below contains a valid base64 encoded JWT Token with
   the customClaim property containing pascal values that match
   the properties …
Run Code Online (Sandbox Code Playgroud)

jwt asp.net-core asp.net-core-webapi identitymodel .net-6.0

5
推荐指数
1
解决办法
1510
查看次数

添加新包会破坏 .NET 5 应用程序

我一直试图找出为什么我的控制台应用程序在引入新包后立即失败。使用IdentityModel.OidcClientand有效,但添加时会引发异常。我也不在代码中引用新包,我只是将其添加到项目中。Microsoft.AspNetCore.Server.Kestrel Microsoft.Extensions.Configuration.Json

重现步骤:

  1. 克隆https://github.com/IdentityModel/IdentityModel.OidcClient.Samples.git

  2. 将NetCoreConsoleClient升级到 .NET 5(更新包)。

  3. 删除Serilog.Sinks.Literate过时的包。

  4. 在 Program.cs 中删除对 SeriLog 的调用.WriteTo.LiterateConsole并添加using IdentityModel.Client.

  5. 为类中的方法添加CancellationToken cancellationToken = new CancellationToken()参数。接口的签名已更改,新方法应如下所示:InvokeAsyncSystemBrowserIBrowserpublic async Task<BrowserResult> InvokeAsync(BrowserOptions options, CancellationToken cancellationToken = new CancellationToken())

  6. 运行应用程序并使用 alice/alice 登录。获取token成功。

  7. 添加包Microsoft.Extensions.Configuration.Json

  8. 运行应用程序。Object reference not set to an instance of an object现在写入 http 响应时会引发异常。

LoopbackHttpListener.SetResult写入响应时发生异常:ctx.Response.WriteAsync("<h1>You can now return to the application.</h1>");

为什么添加一个包会对运行时产生如此大的影响?

项目文件: …

c# kestrel-http-server identitymodel .net-5

3
推荐指数
1
解决办法
1090
查看次数

将 HttpClient 与 .AddClientAccessTokenHandler() 扩展一起使用时,不包含角色声明

我正在使用IdentityModel.AspNetCore扩展.AddClientAccessTokenHandler()来自动向 API 提供HttpClient访问令牌(至少我知道我可以使用它)。某些 API 端点是根据角色进行授权的。但由于某种原因,添加到请求的访问令牌不包含角色声明。如果我不使用.AddClientAccessTokenHandler()并手动检索令牌并使用它进行设置,SetBearerToken(accessTone)那么我可以到达我的角色授权端点。

我的启动是:

services.AddAccessTokenManagement(options =>
{
    options.Client.Clients.Add("auth", new ClientCredentialsTokenRequest
    {
        Address = "https://localhost:44358/connect/token", 
        ClientId = "clientId",
        ClientSecret = "clientSecret",
    });
});
Run Code Online (Sandbox Code Playgroud)

WebApi 调用:

var response = await _httpClient.GetAsync("api/WeatherForecast/SecretRole");
Run Code Online (Sandbox Code Playgroud)

身份服务器配置:

public static IEnumerable<ApiResource> GetApis() =>
    new List<ApiResource>
    {
        new ApiResource("WebApi", new string[] { "role" })
            { Scopes = { "WebApi.All" }}
    };

public static IEnumerable<ApiScope> GetApiScopes() =>
    new List<ApiScope>
        { new ApiScope("WebApi.All") };

public static IEnumerable<IdentityResource> GetIdentityResources() => …
Run Code Online (Sandbox Code Playgroud)

c# asp.net-core identitymodel

1
推荐指数
1
解决办法
8815
查看次数