microsoft-identity-web icon indicating copy to clipboard operation
microsoft-identity-web copied to clipboard

Provide ASP.NET core template example of calling Azure SDK

Open cbrooksmsft opened this issue 2 years ago • 0 comments

Is your feature request related to a problem? Please describe. it was very confusing to figure out how to use the ASP.NET core webapp template (with razor pages) to call an Azure SDK with the user's identity, as the various example of this are based on MVC and explicity controller classes.

Describe the solution you'd like Could we add something like a "--calls-azure-sdk" parameter to the webapp templates that includes an example of wiring up the ITokenAquisition to a page constructor and reading a blob from a storage account with the Azure Storage sdk?

using System.Net;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.Identity.Web;
using Azure.Storage.Blobs;
using Azure.Identity;

namespace UserAuthCallsAPI.Pages;

[AuthorizeForScopes(Scopes = (new[] {"https://storage.azure.com/.default"}))]
public class PrivacyModel : PageModel
{
    private readonly ILogger<PrivacyModel> _logger;

    private readonly ITokenAcquisition _tokenAcquisition;

    private readonly IConfiguration _configuration;

    public PrivacyModel(ILogger<PrivacyModel> logger,
                        ITokenAcquisition tokenAcquisition,
                        IConfiguration configuration)
    {
        _logger = logger;
        _tokenAcquisition = tokenAcquisition;
        _configuration = configuration;
    }

    public void OnGet()
    {
        string blobUrl = _configuration["blobUrl"];
        BlobClient blob = new BlobClient(new Uri(blobUrl), new TokenAcquisitionTokenCredential(_tokenAcquisition));

        string blobContent = blob.DownloadContent().Value.Content.ToString();
        ViewData["ApiResult"] = blobContent;
    }
}

Describe alternatives you've considered duplicating all the MVC samples to show the razor pages

Additional context Add any other context or screenshots about the feature request here.

cbrooksmsft avatar Apr 14 '22 17:04 cbrooksmsft