Asp.Net-Jwt承载身份验证:签名无效

Asp.Net-Jwt承载身份验证:签名无效,asp.net,authentication,jwt,azure-active-directory,adal,Asp.net,Authentication,Jwt,Azure Active Directory,Adal,我从AzureAD为我的应用程序获取一个access_令牌和id_令牌,该应用程序使用OAuth2和隐式流。这是我获取令牌的示例URL: 范围是openidhttps://grap.microsoft.com/user.read 响应类型为id\u令牌+令牌 我也有一个Asp.Net后端,我想安全。因此,我为我的控制器使用Authorize属性,并在标头中发送一个令牌,如下所示:“Authentication:Bearer the_token” 我在Startup.cs中的配置如下所示: ap

我从AzureAD为我的应用程序获取一个access_令牌和id_令牌,该应用程序使用OAuth2和隐式流。这是我获取令牌的示例URL:

范围是
openidhttps://grap.microsoft.com/user.read

响应类型为
id\u令牌+令牌

我也有一个Asp.Net后端,我想安全。因此,我为我的控制器使用
Authorize
属性,并在标头中发送一个令牌,如下所示:“Authentication:Bearer the_token”

我在
Startup.cs
中的配置如下所示:

app.UseJwtBearerAuthentication(new JwtBearerOptions
{
    Authority = string.Format("https://login.microsoftonline.com/{0}/v2.0/",
            "d67853c3-db96-4dac-a37b-f2bfb12b42d1"),
    Audience = "8422b3fb-5612-4fdd-a90f-707d7218de57"
});
据我所知,访问令牌应该用于此目的,id_令牌不应该离开前端。但是在我的例子中,后端的身份验证只与id令牌一起工作。无法对访问令牌进行签名
承载错误=“无效令牌”,错误描述=“签名无效”

查看jwt.io中的access_令牌,我发现令牌具有不同的受众和发行者。例如,access_令牌具有以下特性

"aud": "https://graph.microsoft.com",
"iss": "https://sts.windows.net/d67853c3-db96-4dac-a37b-f2bfb12b42d1/",
"aud": "my_client_id",
"iss": "https://login.microsoftonline.com/my_tenant_id/v2.0",
而id令牌具有以下特性:

"aud": "https://graph.microsoft.com",
"iss": "https://sts.windows.net/d67853c3-db96-4dac-a37b-f2bfb12b42d1/",
"aud": "my_client_id",
"iss": "https://login.microsoftonline.com/my_tenant_id/v2.0",
因此,在我看来,access_令牌是以某种方式为Graph API发布的。如果有人能告诉我,我做错了什么,或者我如何解决我的问题,我会很高兴

编辑:
当我使用scope
openid配置文件时,它的工作原理与我之前的预期一致。但由于Azure中的更改,此作用域不再有效,Microsoft指示我使用上述作用域。

如您所述,您请求的访问令牌用于Microsoft Graph。id_令牌仅用于客户端对用户进行身份验证,而不是用于资源服务器

为了使用Azure AD V2.0端点保护web API,我们可以为下面类似web API的请求获取访问令牌:

https://login.microsoftonline.com/common/oauth2/v2.0/authorize?response_type=token&client_id={client_id}&scope=api://{client_id}/access_as_user&redirect_uri={redirect_uri}
下面是通过Azure AD V2.0端点保护web API的代码:

public void ConfigureAuth(IAppBuilder app)
{
    System.Diagnostics.Trace.TraceWarning("Hello");
    var tvps = new TokenValidationParameters
    {
        // The web app and the service are sharing the same clientId
        ValidAudience = clientId,
        ValidateIssuer = false,
    };

    // NOTE: The usual WindowsAzureActiveDirectoryBearerAuthenticaitonMiddleware uses a
    // metadata endpoint which is not supported by the v2.0 endpoint.  Instead, this 
    // OpenIdConenctCachingSecurityTokenProvider can be used to fetch & use the OpenIdConnect
    // metadata document.

    app.UseOAuthBearerAuthentication(new OAuthBearerAuthenticationOptions
    {
        AccessTokenFormat = new JwtFormat(tvps, new OpenIdConnectCachingSecurityTokenProvider("https://login.microsoftonline.com/common/v2.0/.well-known/openid-configuration")),
    });
}
}

有关通过Azure AD V2.0端点保护web API的更多详细信息,您可以参考以下文档:


如您所述,您请求的访问令牌用于Microsoft Graph。id_令牌仅用于客户端对用户进行身份验证,而不是用于资源服务器

为了使用Azure AD V2.0端点保护web API,我们可以为下面类似web API的请求获取访问令牌:

https://login.microsoftonline.com/common/oauth2/v2.0/authorize?response_type=token&client_id={client_id}&scope=api://{client_id}/access_as_user&redirect_uri={redirect_uri}
下面是通过Azure AD V2.0端点保护web API的代码:

public void ConfigureAuth(IAppBuilder app)
{
    System.Diagnostics.Trace.TraceWarning("Hello");
    var tvps = new TokenValidationParameters
    {
        // The web app and the service are sharing the same clientId
        ValidAudience = clientId,
        ValidateIssuer = false,
    };

    // NOTE: The usual WindowsAzureActiveDirectoryBearerAuthenticaitonMiddleware uses a
    // metadata endpoint which is not supported by the v2.0 endpoint.  Instead, this 
    // OpenIdConenctCachingSecurityTokenProvider can be used to fetch & use the OpenIdConnect
    // metadata document.

    app.UseOAuthBearerAuthentication(new OAuthBearerAuthenticationOptions
    {
        AccessTokenFormat = new JwtFormat(tvps, new OpenIdConnectCachingSecurityTokenProvider("https://login.microsoftonline.com/common/v2.0/.well-known/openid-configuration")),
    });
}
}

有关通过Azure AD V2.0端点保护web API的更多详细信息,您可以参考以下文档:


谢谢!同时,我使用了adv1.0,它工作得很好,因为我可以在那里指定一个资源。所以我现在正在工作,但我也会在有时间的时候测试你的解决方案。谢谢!同时,我使用了adv1.0,它工作得很好,因为我可以在那里指定一个资源。所以我现在正在工作,但我也会在有时间的时候测试你的解决方案。