【问题标题】:How do I resolve the error AADSTS7000218: The request body must contain the following parameter: 'client_secret' or 'client_assertion'如何解决错误 AADSTS7000218:请求正文必须包含以下参数:“client_secret”或“client_assertion”
【发布时间】:2017-08-10 09:18:06
【问题描述】:

这就是我编写代码并尝试获取输出的方式。

请求正文必须包含以下参数:client_secretclient_assertion

 static async Task<AuthenticationResult> getAccessToken()
 {
     string hardcodedUsername = "";
     string hardcodedPassword = "";
     string tenantName = "projectwidgets.com";
     string authString = "https://login.microsoftonline.com/" + tenantName;
     AuthenticationContext authenticationContext = new AuthenticationContext(authString, false);
     //Config for OAuth client credentials
     string clientId = "as";
     string key = "kk";
     string authority = String.Format(CultureInfo.InvariantCulture, aadInstance, tenantName);
     var authContext = new AuthenticationContext(authority);
     AuthenticationResult result = null;
     try
     {
         result = await authContext.AcquireTokenAsync("https://pwsnapitazure.azurewebsites.net", clientId, new UserPasswordCredential(hardcodedUsername, hardcodedPassword));
     }
     catch (Exception ex)
     {
          Console.WriteLine(ex.StackTrace);
          System.Diagnostics.Debug.WriteLine(ex.Message);
     }                        
     return result;
 }

【问题讨论】:

    标签: c# azure-active-directory


    【解决方案1】:

    由于 Azure 应用注册 UI 已从旧式身份验证更改,您将需要启用名为“将应用程序视为公共客户端”的附加设置。在默认客户端类型下,将此设置设置为

    Manifest 中,您也可以通过设置来控制:

    "allowPublicClient": true
    

    【讨论】:

      【解决方案2】:

      根据您的代码,您似乎正在使用使用用户名和密码进行身份验证的网络应用程序/API。

      我们只能使用来自本机客户端的资源所有者流程。机密客户端(例如网站)不能使用直接用户凭据。

      您需要将其作为公共客户端(本机客户端应用)调用,而不是作为机密客户端(网络应用/API)调用。有关如何使用 ADAL .NET 通过用户名/密码对用户进行身份验证的更多信息,请参阅 this document。尤其是 Constraints &amp; Limitations 部分。

      在守护程序或服务器应用程序中,您可以考虑使用 client credential flow ,但在此流程中,应用程序将其客户端凭据提供给 OAuth2 令牌颁发端点,并作为回报获得代表应用程序本身的访问令牌,而无需任何用户信息。请点击here了解更多关于客户端凭证流的详细信息,here是代码示例。

      【讨论】:

        【解决方案3】:

        在尝试使用 UsernamePasswordCredential 凭据访问 Azure 资源时,我们遇到了以下错误。

        这是因为在 Azure AD 中注册的应用程序禁用了允许公共客户端流。

        启用它为我们解决了问题。

        https://nishantrana.me/2020/12/01/fixed-aadsts7000218-the-request-body-must-contain-the-following-parameter-client_assertion-or-client_secret/

        【讨论】:

        • 这只是重复几年前的现有答案。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-08-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-09-05
        • 2015-12-29
        • 2021-11-22
        相关资源
        最近更新 更多