Ask Learn
Preview
Ask Learn is an AI assistant that can answer questions, clarify concepts, and define terms using trusted Microsoft documentation.
Please sign in to use Ask Learn.
Sign inThis browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Lets say you have Web APIs hosted in an Azure AppService and these Web APIs are protected using Azure AD (EasyAuth). Now you would like to consume them from another website. One approach is using Service Principal account, where you create an Azure AD Application and use ClientID and secret in your website to get an Azure AD Token. Here are the steps. Main disadvantage of this approach is that your application is responsible to protect the ClientID and secret. You can save them in the AppSettings instead of saving it in the web.config file. Or you can use Azure Key Vault. For Key Vault, your code needs to authenticate to access Key Vault.
Better approach is Managed Identity. This option allows to access protected Azure AD resources without any need for secrets or credentials in your code or in web.config. More details on Manage Identity can be found here
Here are the quick steps to use Managed Service Identity Azure AppService
Open Visual Studio
Create a new Website project (either .net core or .net framework)
Select MVC option
In the Home controller, add following code to About action
public ActionResult About()
{
ViewBag.Message = "User Name is " + User.Identity.Name;
return View();
}
In the Contact action, add following code
static HttpClient client = new HttpClient();
public ActionResult Contact()
{
var azureServiceTokenProvider = new AzureServiceTokenProvider();
var accessTokenTask = azureServiceTokenProvider.GetAccessTokenAsync("https://wabaconead.azurewebsites.net/");
accessTokenTask.Wait();
var accessToken = accessTokenTask.Result;
var url = "https://wabaconead.azurewebsites.net/home/about";
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
var responseTask = client.GetStringAsync(url);
responseTask.Wait();
var response = responseTask.Result;
ViewBag.Message = "Your contact page." + accessToken + " HTML Response " + response;
return View();
}
You may need to add Microsoft.AzureServices.AppAuthentication NuGet Package
Now deploy this website to above two websites
When you access the first website, you get promoted for Azure AD credential so we are sure that this website is protected with Azure AD
Now try to access Home/About…this should return your email address you have used to log into Azure AD
Now browse to the second website, access the Home/Contact…this should call the first website and display the content. Now check for the username (search for text “User Name is”)
It doesn't add the website URL in the allowed token audiences. Follow these steps to fix this bug
Ask Learn is an AI assistant that can answer questions, clarify concepts, and define terms using trusted Microsoft documentation.
Please sign in to use Ask Learn.
Sign in