在访问WebApi时,我得到登录HTML页面作为响应而不是api结果? [英] While WebApi access I'm getting Login Html Page as response instead api result?

查看:64
本文介绍了在访问WebApi时,我得到登录HTML页面作为响应而不是api结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

I have a web app and a web API which are register into Azure AD.

我正在使用以下代码从Web应用程序调用Web API方法:

I'm invoking web API method from the web app using below code:

AuthenticationResult result = null;

string userObjectID = ClaimsPrincipal.Current.FindFirst("http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier").Value;
AuthenticationContext authContext = new AuthenticationContext(Startup.Authority, new ADALTokenCache(userObjectID));
ClientCredential credential = new ClientCredential(clientIdSimulator, clientSecretSimulator);
result = authContext.AcquireTokenAsync("my webapi app uri from azure ad", credential).Result;

HttpClient client = new HttpClient();
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, "https://demoapi.azurewebsites.net/api/getdata");
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", result.AccessToken);
HttpResponseMessage response = await client.SendAsync(request);

// Return the user's profile in the view.
if (response.IsSuccessStatusCode)
{
    string responseString = await response.Content.ReadAsStringAsync();
    var test = JsonConvert.DeserializeObject(responseString);
} 

responseString . 输出:

After execution I get the result as an HTML page into responseString. Output:

<!DOCTYPE html>
<html dir="ltr" class="" lang="en">
<head>
    <title>Sign in to your account</title>
    <noscript>
        <meta http-equiv="Refresh" content="0; URL=https://login.microsoftonline.com/jsdisabled" />
    </noscript>    

        <link rel="shortcut icon" href="https://secure.aadcdn.microsoftonline-p.com/ests/2.1.8233.17/content/images/favicon_a_eupayfgghqiai7k9sol6lg2.ico" />

I expect to get the API method output. How can I get the API method output?

Web应用程序和Web API都在同一Azure AD租户中.

Both the web app and the web API are in the same Azure AD tenant.

我是否需要使用 authContext.AcquireTokenSilentAsync ? 当我使用此方法时,出现异常

Do I need to use authContext.AcquireTokenSilentAsync? When I use this method, I get teh exception failed_to_acquire_token_silently, even if the ID used matches.

SE

推荐答案

您是否尝试过直接通过Fiddler或Postman测试Web API?
Have you tried testing the web API directly through Fiddler or Postman ?


这篇关于在访问WebApi时,我得到登录HTML页面作为响应而不是api结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆