Reputation: 82
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));
}
So my questions is why when I use HttpCliet
, I get the Unauthorized (code 401),
while the second way got the user data successfully.
Upvotes: 2
Views: 292
Reputation: 246998
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);
}
Upvotes: 3