We are exploring code sample provided by Microsoft for implementing "Azure AD B2C Authentication". We are using "4-2-B2C" (Refer the screen shot) code samples from "https://learn.microsoft.com/en-us/azure/active-directory-b2c/integrate-with-app-code-samples", 4th sample with name dotnetcore-webapp-msal-api
.
Following is what we are trying to achieve:
- Get hold of access token in a controller.
- Display menu items based on role in claims .
Any input would be helpful.
Thanks in advance.
I tried the sample code provided by Microsoft by making required changes but I am not able to access the token. Refer to the following code:
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Identity.Web;
using System;
using System.Diagnostics;
using System.Threading.Tasks;
using WebApp_OpenIDConnect_DotNet.Models;
namespace WebApp_OpenIDConnect_DotNet.Controllers
{
[Authorize]
public class HomeController : Controller
{
private readonly ITokenAcquisition tokenAcquisition;
public HomeController(ITokenAcquisition tokenAcquisition)
{
this.tokenAcquisition = tokenAcquisition;
}
public async Task<IActionResult> Index()
{
// Getting Token
string accessToken = await this.tokenAcquisition.GetAccessTokenForAppAsync("https://<Domain>.<Tenant ID of the web api>/access_as_user");
return View();
}
[AllowAnonymous]
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
public IActionResult Error()
{
return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
}
}
}
What do you expect to happen? Get hold of "access" toke.
What did actually happen? Please refer to the Index
method.
I am not able to get the access token. When I check the details in variable accessToken
, I am getting the following exception:
InnerException = {"IDW10404: 'scope' parameter should be of the form 'AppIdUri/.default'. See https://aka.ms/ms-id-web/daemon-scenarios. (Parameter 'scope')"}