简体   繁体   中英

Can't get API resource using HttpClient

I'm using Identity Server4 and I have configured a web API project using ResourceOwnerPassword grant type.

Web API method:

[HttpGet]
[Authorize(Roles = "Badmin")]
[Route("GetUsers")]
public IActionResult GetUsers()
{
    List<ApplicationUser> users = _context.ApplicationUsers
        .AsNoTracking().ToListAsync().Result;

    return Json(users);
}

In my console program code:

if (response != null)
{
    using (HttpClient client = new HttpClient())
    {
        Console.WriteLine("AccessToken:" + Environment.NewLine + response.AccessToken);
        Console.WriteLine(Environment.NewLine);

        HttpResponseMessage resourceOwnerPasswordResponse = await client.GetAsync("https://localhost:44366/identity/GetUsers");

        client.DefaultRequestHeaders.Add("Bearer", response.AccessToken);

        if (!resourceOwnerPasswordResponse.IsSuccessStatusCode)
        {
            Console.WriteLine(resourceOwnerPasswordResponse.StatusCode);
            Console.WriteLine("HttpClient ResourceOwnerPassword Result: " + resourceOwnerPasswordResponse.StatusCode);
            Console.WriteLine(Environment.NewLine);
        }
        else
        {
            string result = await resourceOwnerPasswordResponse.Content.ReadAsStringAsync();
            Console.WriteLine("HttpClient ResourceOwnerPassword Result: " + Environment.NewLine + JsonConvert.DeserializeObject(result));
            Console.WriteLine(Environment.NewLine);
        }
    }
}

The second way:

if (response != null)
{
    HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create("https://localhost:44366/identity/GetUsers");
    httpWebRequest.ContentType = "application/json";
    httpWebRequest.Headers.Add("Authorization", string.Format("Bearer {0}", response.AccessToken));
    httpWebRequest.Method = "GET";

    HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();

    string result = string.Empty;

    using (Stream responseStream = httpWebResponse.GetResponseStream())
    {
        if (responseStream != null)
        {
            StreamReader streamReader = new StreamReader(responseStream);
            result = streamReader.ReadToEnd();
            streamReader.Close();
        }
    }

    Console.WriteLine("HttpWebRequest ResourceOwnerPassword Result: " + Environment.NewLine + JsonConvert.DeserializeObject(result));
}

see the console result:
查看控制台结果

So my questions is why when I use HttpCliet , I get the Unauthorized (code 401), while the second way got the user data successfully.

Token should be added to the client before making the request and you need to make sure it is the Authorization header.

client.DefaultRequestHeaders.Authorization = 
    new AuthenticationHeaderValue("Bearer", response.AccessToken);

//OR

//client.DefaultRequestHeaders
//  .Add("Authorization", string.Format("Bearer {0}", response.AccessToken));

var resourceOwnerPasswordResponse = await client.GetAsync("https://localhost:44366/identity/GetUsers");

Which is why it worked in the second example and not the first

The Web API action should also be async if using ToListAsync

[HttpGet]
[Authorize(Roles = "Badmin")]
[Route("GetUsers")]
public async Task<IActionResult> GetUsers() {
    var users = await _context.ApplicationUsers.AsNoTracking().ToListAsync();

    return Json(users);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM