![](/img/trans.png)
[英]ASP.NET Core 2.0 Web API Azure Ad v2 Token Authorization not working
[英]Getting 404 on the [Authorize] decorated Web API method when securing with Azure AD v2.0
在我的方案中,我有一个Windows UWP Client应用程序对用户进行身份验证,并使用OAuth 2.0访问令牌和Azure AD v2.0端点访问受保护的Web API服务。 Web API是使用ASP.NET Core 2.0构建的。 我没有在Azure样本GitHub上找到任何具有正确配置的现有样本,因此我决定自己构建它。 我能够对用户进行身份验证并访问Microsoft Graph以获取用户的配置文件,但是当我尝试访问Web API时,却收到404 Not Found错误消息。 相同Web API的不安全方法(无[Authorize]装饰)可以正常工作。
Web API的My Startup.cs包含以下部分:
// Add Authentication scheme properties.
services.AddAuthentication(options => {
options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;
});
string clientId = Configuration["AzureAd:ClientId"];
string redirectUri = Configuration["AzureAd:RedirectUri"];
string tenant = Configuration["AzureAd:Tenant"];
string authority = String.Format(System.Globalization.CultureInfo.InvariantCulture,
Configuration["AzureAd:AadInstance"], tenant);
//OpenID Connect (OIDC) Authentication
services.AddAuthentication(options => {
options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;
})
.AddCookie()
.AddOpenIdConnect(options => {
options.ClientId = clientId;
options.Authority = authority;
options.SignedOutRedirectUri = redirectUri;
options.ResponseType = OpenIdConnectResponseType.IdToken;
options.Events = new OpenIdConnectEvents
{
OnRemoteFailure = OnRemoteFailure,
OnTokenValidated = OnTokenValidated
};
options.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuer = false
};
});
其中,appsettings.json是使用从apps.dev.microsoft.com
应用程序注册复制的ClientID
和Tenant = "common"
。 RedirectUri points to https://localhost:44353/signin-oidc
并且AadInstance is set to: https://login.microsoftonline.com/{0}/oauth2/v2.0
RedirectUri points to https://localhost:44353/signin-oidc
AadInstance is set to: https://login.microsoftonline.com/{0}/oauth2/v2.0
然后,为我的客户端UWP应用配置相应的设置:
private static string ClientId = "436b73b7-XXXXXXXXX";
private const string tenant = "common";
private static string authority = String.Format(System.Globalization.CultureInfo.InvariantCulture,
"https://login.microsoftonline.com/{0}/oauth2/v2.0", tenant);
public static PublicClientApplication PublicClientApp = new PublicClientApplication(ClientId, authority);
Microsoft Graph和自定义API的API端点配置如下:
string _sppAPIEndpoint = "https://localhost:44357/api/AAD/secure";
string _graphAPIEndpoint = "https://graph.microsoft.com/v1.0/me";
范围设置为访问身份验证:
//Set the scope for API call to user.read
string[] _scopes = new string[] { "user.read" };
因此,当我运行UWP应用时,我可以获得auth令牌和Graph信息,但是,就像我在一开始所说的那样,执行以下命令时,我会得到404:
var httpClient = new System.Net.Http.HttpClient();
var request = new System.Net.Http.HttpRequestMessage(System.Net.Http.HttpMethod.Get, url);
//Add the token in Authorization header
request.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", token);
HttpResponseMessage response = await httpClient.GetAsync(url);
对于令牌值,我尝试不使用AccessToken和IdpToken。
我究竟做错了什么? 任何提示和指示将不胜感激。
有很多样本,但是如果发现一个样本缺失,它可能会突然弹出。 您最需要的是: WPF应用到带有ASP.NET Core的WebAPI 。
最后,您必须在“融合应用程序”部分下至少拥有两个应用程序,如下所示:
然后,您需要显式地对UWP应用程序进行对API的访问。 这是通过在API应用程序中传递UWP应用程序的客户端ID来完成的:
然后,您的UWP App将创建API的身份验证请求作为资源,并将接收与API一起使用的访问令牌。
如果要获取多资源令牌(要将该令牌用于Microsoft Graph 和 WebAPI,则必须创建一个多资源请求。
您还可以尝试在请求中包含您的API范围(请查看第二张屏幕截图-API的范围,您可以定义自己的范围)。
阿斯塔科夫 ,谢谢。 不幸的是,链接中的代码示例引用的是ADv1 B2B实现,而不是v2。 另外,我做了所有您建议的操作:为客户端创建了第二个应用程序注册(尽管所有文档都告诉所有客户端使用一个应用程序注册),并添加了预授权的应用程序条目:
我什至向客户请求中添加了一个新范围,其中包含本机应用程序ID: "api://436b73b7-8f59-45e7-9449-580fb9a5d90d/access_as_user"
,但是当我尝试使用新令牌调用API时,我收到UI屏幕上显示的错误消息:
HTTP 404指示客户端能够与给定服务器通信,但是服务器找不到请求的内容。
请检查提供相应服务的服务器的URL是否正确。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.