C# 401在控制台应用程序中调用Azure AD web API方法时出错

C# 401在控制台应用程序中调用Azure AD web API方法时出错,c#,azure,authentication,azure-active-directory,azure-web-app-service,C#,Azure,Authentication,Azure Active Directory,Azure Web App Service,在我发布这个问题之前,我尝试过其他答案 我已经创建了一个web API并发布到azure web app中。我已启用azure AD身份验证。我已经创建了一个应用程序注册,并授予了广告应用程序的权限。在我的控制台应用程序中,我按如下方式调用: private static async Task<string> GetToken() { string aadInstance = "https://login.windows.net/c

在我发布这个问题之前,我尝试过其他答案

我已经创建了一个web API并发布到azure web app中。我已启用azure AD身份验证。我已经创建了一个应用程序注册,并授予了广告应用程序的权限。在我的控制台应用程序中,我按如下方式调用:

private static async Task<string> GetToken()
        {

            string aadInstance = "https://login.windows.net/common";
            string ResourceId = ConfigurationManager.AppSettings["ResourceId"];
            string tenantId = ConfigurationManager.AppSettings["TenantId"];
            string clientId = ConfigurationManager.AppSettings["ClientId"];
            string replyAddress = ConfigurationManager.AppSettings["ReplyAddressConfigured"];
            string password = ConfigurationManager.AppSettings["ida:Password"];
            AuthenticationContext authenticationContext =
              new AuthenticationContext(string.Format(aadInstance, tenantId));
            PlatformParameters k = new PlatformParameters(PromptBehavior.Auto);            
            AuthenticationResult authenticationResult = await authenticationContext.AcquireTokenAsync(ResourceId, clientId, new Uri(replyAddress),k).ConfigureAwait(false);
            return authenticationResult.AccessToken;
        } 

static async Task Main(string[] args)
        {
           var accessToken = await GetToken();
            using (var client = new HttpClient())
            {
                client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", accessToken);
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));                
                var responseTask = client.GetAsync("https://myapi.azurewebsites.net/api/GetData/GetLicensing?appName=test&tenantId=f556301-8a1e-48c3-b59e-55a9036f843e");
                responseTask.Wait();

                var result = responseTask.Result;
                if (result.IsSuccessStatusCode)
                {

                    var readTask = result.Content.ReadAsStringAsync();
                    readTask.Wait();                   
                }
            }
        }
private静态异步任务GetToken()
{
字符串aadInstance=”https://login.windows.net/common";
字符串ResourceId=ConfigurationManager.AppSettings[“ResourceId”];
字符串tenantId=ConfigurationManager.AppSettings[“tenantId”];
字符串clientId=ConfigurationManager.AppSettings[“clientId”];
字符串replyAddress=ConfigurationManager.AppSettings[“ReplyAddressConfigured”];
字符串密码=ConfigurationManager.AppSettings[“ida:密码”];
AuthenticationContext AuthenticationContext=
新的AuthenticationContext(string.Format(aadInstance,tenantId));
PlatformParameters k=新的PlatformParameters(PromptBehavior.Auto);
AuthenticationResult AuthenticationResult=await authenticationContext.AcquireTokenAsync(ResourceId,clientId,新Uri(replyAddress),k)。ConfigureAwait(false);
返回authenticationResult.AccessToken;
} 
静态异步任务主(字符串[]args)
{
var accessToken=await GetToken();
使用(var client=new HttpClient())
{
client.DefaultRequestHeaders.Authorization=new System.Net.Http.Headers.AuthenticationHeaderValue(“Bearer”,accessToken);
client.DefaultRequestHeaders.Accept.Add(新的MediaTypeWithQualityHeaderValue(“应用程序/json”);
var responseTask=client.GetAsync(“https://myapi.azurewebsites.net/api/GetData/GetLicensing?appName=test&tenantId=f556301-8a1e-48c3-b59e-55a9036f843e”);
responseTask.Wait();
var结果=响应任务结果;
if(结果。IsSuccessStatusCode)
{
var readTask=result.Content.ReadAsStringAsync();
readTask.Wait();
}
}
}
IsSuccessStatusCode
401的代码为false。这里的错误是什么。但如果我使用client和secret,它是成功的。如果不传递
客户端密码
值,则无法调用? 以下仅为说明:

如果不使用调用API,则在没有客户端密码的情况下无法获得正确的令牌


但是,如果在API和云上托管的控制台应用程序中都使用,则可以从控制台应用程序调用API,而无需客户端密码。

如果不使用调用API,则无法获得无客户端密码的正确令牌


但是,如果您在API和云上托管的控制台应用程序中都使用,您可以从控制台应用程序调用API,而无需客户端密码。

请共享完整的响应正文。401可能发生有几个原因。请分享完整的回复正文。401可能发生的原因有几个。