asp.net-core - 缺少客户端密码或客户端证书

标签 asp.net-core azure-active-directory azure-ad-graph-api

MsalClientException:IDW10104:客户端密码和客户端证书都不能为 null 或空格,调用 Web API 时,Web 应用程序的配置中只能包含一个。例如,在 appsettings.json 文件中。

Microsoft.Identity.Web.MicrosoftIdentityOptionsValidation.ValidateEitherClientCertificateOrClientSecret(string clientSecret, IEnumerable<CertificateDescription> cert)
Microsoft.Identity.Web.TokenAcquisition.BuildConfidentialClientApplicationAsync()
Microsoft.Identity.Web.TokenAcquisition.GetOrBuildConfidentialClientApplicationAsync()
Microsoft.Identity.Web.TokenAcquisition.AddAccountToCacheFromAuthorizationCodeAsync(AuthorizationCodeReceivedContext context, IEnumerable<string> scopes)
Microsoft.Identity.Web.MicrosoftIdentityWebAppAuthenticationBuilder+<>c__DisplayClass11_1+<<WebAppCallsWebApiImplementation>b__1>d.MoveNext()
Microsoft.AspNetCore.Authentication.OpenIdConnect.OpenIdConnectHandler.RunAuthorizationCodeReceivedEventAsync(OpenIdConnectMessage authorizationResponse, ClaimsPrincipal user, AuthenticationProperties properties, JwtSecurityToken jwt)
Microsoft.AspNetCore.Authentication.OpenIdConnect.OpenIdConnectHandler.HandleRemoteAuthenticateAsync()

这发生在通过 Azure AD 成功登录后。我也传递了客户端 secret (通过用户 secret 和 appSettings)。对于源代码引用,我使用以下示例项目:

https://github.com/damienbod/AspNetCoreUsingGraphApi

最佳答案

如果想在web app中调用AzureAD转换的web api,请引用以下步骤

  1. appsettings.json
{
  "AzureAd": {
    "Instance": "https://login.microsoftonline.com/",
    "ClientId": "[Client_id-of-web-app-eg-2ec40e65-ba09-4853-bcde-bcb60029e596]",
    "TenantId": "common"

   // To call an API
   "ClientSecret": "[Copy the client secret added to the app from the Azure portal]",

 },
 "MyApi": {
    "BaseUrl": "https://graph.microsoft.com/beta",
    "Scopes": "user.read"
    }
}
  1. startup.cs
using Microsoft.Identity.Web;

public class Startup
{
  // ...
  public void ConfigureServices(IServiceCollection services)
  {
  // ...
  services.AddAuthentication(OpenIdConnectDefaults.AuthenticationScheme)
          .AddMicrosoftIdentityWebApp(Configuration, "AzureAd")
            .EnableTokenAcquisitionToCallDownstreamApi(new string[]{"" })
               .AddDownstreamWebApi("MyApi", Configuration.GetSection("MyApi"))
            .AddInMemoryTokenCaches();
   // ...
  }
  // ...
}
  1. 调用接口(interface)
[Authorize]
public class HomeController : Controller
{
 readonly ITokenAcquisition tokenAcquisition;

 public HomeController(ITokenAcquisition tokenAcquisition)
 {
  this.tokenAcquisition = tokenAcquisition;
 }

 [AuthorizeForScopes(Scopes = new[] { "user.read" })]
public async Task<IActionResult> Profile()
{
 // Acquire the access token.
 string[] scopes = new string[]{"user.read"};
 string accessToken = await tokenAcquisition.GetAccessTokenForUserAsync(scopes);

 // Use the access token to call a protected web API.
 HttpClient client = new HttpClient();
 client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
 string json = await client.GetStringAsync(url);
}

}

更多详情请引用here .

关于asp.net-core - 缺少客户端密码或客户端证书,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66861890/

相关文章:

azure-active-directory - 根据任何自定义策略在 Azure AD B2C 中兑换刷新 token

c# - 如何在 ASP.Net Core 中实现自己的 DBContext 并仍然使用给定的 ApplicationUser 进行身份验证?

asp.net-core - 外部文档包中的模型显示在庞大的文档中

asp.net - 尝试访问 Azure Web 应用程序中的特定页面时发生错误

azure - 尽管授予了权限,Graph API 仍不断返回 Authorization_RequestDenied

c# - 从 Graph API 获取指定用户

ASP.NET Core 2.0 Preview 2 on IIS 错误 502.5

azure - 无法在 .Net Core Web 应用程序上使用 Azure AD 身份验证注销

azure - 当尝试从 Adal4j 中的刷新 token 访问访问 token 时,如何定义 AuthenticationCallback?

azure - 如何使用 MS Graph 编程 API 检查服务主体的登录列表