【问题标题】:.net core web API not getting authenticated even after generating bearer token from Azure AD using AzureADDefaults.BearerAuthenticationScheme即使在使用 AzureADDefaults.BearerAuthenticationScheme 从 Azure AD 生成不记名令牌后,.net 核心 Web API 也未获得身份验证
【发布时间】:2020-08-10 14:11:35
【问题描述】:

我正在开发一个 .net 核心 Web API,并尝试使用 AZURE AD 身份验证对其进行身份验证。 我遵循以下配置。:

1.在 Startup.cs 我添加了身份验证方案:AzureADDefaults.BearerAuthenticationScheme

 services.AddAuthentication(AzureADDefaults.BearerAuthenticationScheme)
              .AddAzureADBearer(options => { Configuration.Bind("AzureAd", options); });

2.在startup.cs的configure方法中我添加了:

app.UseAuthentication();

3.在 app.settings.json 我添加了以下属性:

 "AzureAd": {
    "Instance": "https://login.microsoftonline.com/",
    "ClientId": "<MY client ID>",
    "TenantId": "<My Tenant ID>",
    "Issuer": "https://login.microsoftonline.com/<My Tenant ID>/v2.0",
    "Domain": "<My Domain>",
    "ConfigView": "MVC",
    "CallbackPath": "/signin-oidc",
    "ClientSecret": "<My Client Secret>"
  }
  1. 我在控制器顶部添加了 Authorize 属性
  2. 我使用以下代码生成了我的 Bearer 令牌:
 static void Main(string[] args)
        {
            Program obj = new Program();
            IRestResponse ARMtokenResponse = obj.GetARMAuthToken();
            dynamic response = JsonConvert.DeserializeObject(ARMtokenResponse.Content);
            Console.WriteLine(response["access_token"].ToString());
            Console.ReadKey();
        }
        private IRestResponse GetARMAuthToken()
        {
            var client = new RestClient("https://login.microsoftonline.com/<MY TENANT ID>/oauth2/token"); //tenantid
            client.Timeout = -1;
            var request = new RestRequest(Method.POST);
            request.AddHeader("Content-Type", "application/x-www-form-urlencoded");
            request.AddParameter("grant_type", "client_credentials");
            request.AddParameter("client_id", "<My Client ID>");
            request.AddParameter("client_secret", "<MY CLIENT SECRET>");
            request.AddParameter("resource", "https://management.azure.com/");
            IRestResponse response = client.Execute(request);
            return response;
        }
  1. 此外,我正在使用在邮递员/控制台应用程序中生成的此令牌来调用 API,但在响应标头中出现错误:Bearer error="invalid_token", error_description="观众无效"

请帮助我。我被困在这里了

【问题讨论】:

    标签: asp.net-core asp.net-web-api azure-active-directory asp.net-core-webapi azure-authentication


    【解决方案1】:

    您的代码中有几个问题:

    1. 您应该获取 web api 的访问令牌,而不是获取 Azure Rest API (https://management.azure.com/) 的访问令牌,您的 web api 无法验证 Azure Rest API 的访问令牌。

    2. 获取令牌时您使用的是 Azure AD V1.0 端点,但验证令牌时您使用的是 Azure AD V2.0 端点 (Issuer)。

    对于 Azure AD V1.0,您可以参考代码示例:Call a web API in an ASP.NET Core web app using Azure AD

    对于 Azure AD V2.0,您可以参考代码示例:Enable your Web Apps to sign-in users and call APIs with the Microsoft identity platform for developers,并遵循4-WebApp-your-API 方案。

    【讨论】:

    • 感谢南宇的回答,但是由于我对 azure AD 身份验证有些陌生,请您解释一下我必须进行哪些更改,这样才能正常工作?
    • 你应该获取令牌来访问你的 web api,而不是 Azure Rest API,上面提供了代码示例。
    猜你喜欢
    • 1970-01-01
    • 2019-06-10
    • 2021-02-18
    • 2019-10-14
    • 2016-11-15
    • 2017-08-16
    • 2020-12-22
    • 2020-08-12
    • 2015-03-01
    相关资源
    最近更新 更多