Mik*_*ike 1 authentication token azure oauth-2.0 owin
我正在尝试让我的 .Net Web API(使用 OWIN 的 Azure API 应用程序)接受用于 client_credentials 授予的 OAuth Bearer 令牌,但我不断收到 401 Unauthorized。
似乎所有 Microsoft 示例都已过时(不符合 Owin 的最新 nuget 包)。
JwtFormat 需要一个 IIssuerSecurityTokenProvider,但它不再存在 - 相反,JwtFormat 需要一个 IIssuerSecurityKeyProvider,但我无法理解如何使用它。
Azure OAuth 服务器正在运行
我已在 Azure Active Directory 中注册了两个应用程序(api 和客户端)。由于这只是快速演示,我将向您提供所有 id 和秘密;)
我可以从 Azure AD 获取令牌,请参阅https://reqbin.com/817shtc2以获取完整的请求,到目前为止一切顺利。
应用程序编程接口
客户端ID:44cf7574-88a2-42d6-9497-bff43cc8dc09
端点:https ://apim-demo-mglentoft.azure-api.net/api/Values (GET)
客户
客户端ID:5f7ee334-b8db-46d3-972f-09f52e186d1d
秘密:ggKp94]HZHWZ.c*5wUC?ToSVfknyqLB3
我按照https://github.com/azureadquickstarts/appmodelv2-nativeclient-dotnet上的示例进行操作,但它引用了 Microsoft.Owin.Security.Jwt.IIssuerSecurityTokenProvider,它在 nuget v4.0.1.0 中不存在。
我尝试仅注释掉 JwtFormat 的第二个参数,但这不起作用。有什么想法如何使用 Microsoft.Owin.Security.Jwt.IIssuerSecurityKeyProvider 来实现此功能吗?
下面是整个startup.cs
我可以使用 .Net Core 来实现此功能,但由于各种原因,我必须坚持使用 .Net Framework 4.7.2
using System;
using System.Configuration;
using System.Threading.Tasks;
using System.Web.Http;
using DemoAPI.Middleware;
using Microsoft.IdentityModel.Tokens;
using Microsoft.Owin;
using Microsoft.Owin.Cors;
using Microsoft.Owin.Security;
using Microsoft.Owin.Security.Jwt;
using Microsoft.Owin.Security.OAuth;
using Owin;
[assembly: OwinStartup(typeof(DemoAPI.App_Start.Startup))]
namespace DemoAPI.App_Start
{
public partial class Startup
{
public void Configuration(IAppBuilder app)
{
var config = new HttpConfiguration();
ConfigureAuth(app);
app.Use(typeof(CorrelationHandlerMiddleware));
app.UseCors(CorsOptions.AllowAll);
WebApiConfig.Register(config);
app.UseWebApi(config);
}
private static string clientId = ConfigurationManager.AppSettings["ida:ClientId"];
public void ConfigureAuth(IAppBuilder app)
{
// NOTE: The usual WindowsAzureActiveDirectoryBearerAuthentication middleware uses a
// metadata endpoint which is not supported by the v2.0 endpoint. Instead, this
// OpenIdConnectSecurityTokenProvider implementation can be used to fetch & use the OpenIdConnect
// metadata document - which for the v2 endpoint is https://login.microsoftonline.com/common/v2.0/.well-known/openid-configuration
app.UseOAuthBearerAuthentication(new OAuthBearerAuthenticationOptions
{
AccessTokenFormat = new JwtFormat(
new TokenValidationParameters
{
// Check if the audience is intended to be this application
ValidAudiences = new[] { clientId, $"api://{clientId}" },`enter code here`
// Change below to 'true' if you want this Web API to accept tokens issued to one Azure AD tenant only (single-tenant)
// Note that this is a simplification for the quickstart here. You should validate the issuer. For details,
// see https://github.com/Azure-Samples/active-directory-dotnet-native-aspnetcore
ValidateIssuer = false,
}//,
//new OpenIdConnectSecurityKeyProvider("https://login.microsoftonline.com/common/v2.0/.well-known/openid-configuration")
//the OpenIdConnectSecurityKeyProvider implements IIssuerSecurityTokenProvider, which is not part of Microsoft.Owin.Security.Jwt 4.0
),
});
}
}
}
Run Code Online (Sandbox Code Playgroud)
使用 Microsoft.Owin.diagnostics 时的实际错误
Microsoft.Owin.Security.OAuth.OAuthBearerAuthenticationMiddleware 错误:0:身份验证失败 Microsoft.IdentityModel.Tokens.SecurityTokenSignatureKeyNotFoundException:IDX10501:签名验证失败。无法匹配密钥:孩子:'[PII 已隐藏。有关更多详细信息,请参阅 https://aka.ms/IdentityModel/PII。] '。捕获异常:'[PII 被隐藏。有关更多详细信息,请参阅https://aka.ms/IdentityModel/PII。] '。令牌:'[PII 已隐藏。有关更多详细信息,请参阅 https://aka.ms/IdentityModel/PII。] '。在System.IdentityModel.Tokens.Jwt.JwtSecurityTokenHandler.ValidateSignature(字符串令牌,TokenValidationParameters验证参数)在System.IdentityModel.Tokens.Jwt.JwtSecurityTokenHandler.ValidateToken(字符串令牌,TokenValidationParameters验证参数,SecurityToken&validatedToken)
尝试将“令牌”重命名为“密钥”,您可能会很好。
所以,而不是
IssuerSecurityTokenProviders = new IIssuedSecurityTokenProvider[]
{
new symmetricKeyIssuerSecurityTokenProvider(issuer, audienceSecret)
}
Run Code Online (Sandbox Code Playgroud)
你应该有类似的东西
IssuerSecurityKeyProviders= new IIssuerSecurityKeyProvider[]
{
new SymmetricKeyIssuerSecurityKeyProvider(issuer, audienceSecret)
}
Run Code Online (Sandbox Code Playgroud)
欲了解更多信息:问题主题或实际的 Github 存储库
希望对你有帮助...
| 归档时间: |
|
| 查看次数: |
5389 次 |
| 最近记录: |