I have a .net core 3.1 website which uses Active Directory for authentication. I can sign in with a user I have created in the Azure portal.
I then added an API controller.
I have managed to get a token using the following code:
public static async Task<ADAuthResponse> GetAuthToken()
{
using HttpClient httpClient = new HttpClient();
StringContent body = new StringContent("client_id=6865ee8xxxxxxx7-9f28-867ff93b079c&scope=user.read%20openid%20profile%20offline_access&username=cardiffwebjob@xxxxxx.onmicrosoft.com&password=!!XXXXX!123&grant_type=password&client_secret=jJg.6mXXXXXXXXX-w-3l9SHv-T", Encoding.UTF8, "application/x-www-form-urlencoded");
HttpResponseMessage response = await httpClient.PostAsync("https://login.microsoftonline.com/62580128-946f-467b-ae83-7924e7e4fb18/oauth2/v2.0/token", body);
ADAuthResponse result = await JsonSerializer.DeserializeAsync<ADAuthResponse>(await response.Content.ReadAsStreamAsync());
return result;
}
I have then tried to call the endpoint with this code:
public static async Task UpdateWebJobStatus(UpdateFunctionValues updateFunctionValues)
{
// get the auth token
ADAuthResponse authResponse = await GetAuthToken();
using HttpClient httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", authResponse.access_token);
StringContent stringContent = new StringContent(JsonSerializer.Serialize(updateFunctionValues), Encoding.UTF8, "application/json");
HttpResponseMessage httpRequestMessage = await httpClient.PostAsync("https://cardiffwebsite.azurewebsites.net/api/DashboardAPI/SetFunctionStatus", stringContent);
}
And the controller in the website looks like this:
[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
[ServiceFilter(typeof(IExceptionFilter))]
[ResponseCache(Location = ResponseCacheLocation.None, NoStore = true)]
[Route("api/[controller]")]
[ApiController]
public class DashboardAPIController : ControllerBase
{
private readonly BaseContext _db;
private readonly IHubContext<WebJobStatusHub> _hub;
private readonly IHttpContextAccessor _httpContextAccessor;
public DashboardAPIController(BaseContext db, IHubContext<WebJobStatusHub> hubContext, IHttpContextAccessor httpContextAccessor)
{
_db = db;
_hub = hubContext;
_httpContextAccessor = httpContextAccessor;
}
[HttpPost]
[Route("SetFunctionStatus")]
public async Task SetFunctionStatus([FromBody] UpdateFunctionValues updateFunctionValues)
{
WebJobStatusHub hub = new WebJobStatusHub(_db, _hub);
await hub.SendJobStatusUpdate(updateFunctionValues.WebJobId, updateFunctionValues.FunctionId, updateFunctionValues);
}
}
The startup.cs in the website relating to authentication looks like this:
services.AddAuthentication(AzureADDefaults.AuthenticationScheme)
.AddAzureAD(options => _config.Bind("AzureAd", options))
.AddJwtBearer(options =>{
options.Authority = "https://login.microsoftonline.com/62580128-946f-467b-ae83-7924e7e4fb18/";
options.Audience = "f8954991-11xxxxx-73769d3c98cd";
options.TokenValidationParameters.ValidateLifetime = true;
options.TokenValidationParameters.ClockSkew = TimeSpan.Zero;
});
I am getting this error when calling the API:
HTTP/1.1 401 Unauthorized Server: Microsoft-IIS/10.0 WWW-Authenticate: Bearer error="invalid_token", error_description="The signature is invalid"
I have read about 100 threads about how to fix/configure Azure and/or my app to get this to work but with no luck.
Can anyone give me any pointers please? It maybe something I have/have not done correctly in Azure or it could be the way I have re-configured authentication in my startup. I just cannot find the problem.
Any pointers/help would be greatly appreciated.
in response to the comment here is what my app registration looks like:
in response to people helping me in the Azure configuration about exposing an API... i don't appear to have done anything here.