0

In my asp.net Core webapp, I'm putting the following into Startup.cs:

public void ConfigureServices(IServiceCollection services)
{
services.AddAuthentication(OpenIdConnectDefaults.AuthenticationScheme)
.AddMicrosoftIdentityWebApp(Configuration.GetSection("AzureAd"));
services.AddControllersWithViews(options =>
{
var policy = new AuthorizationPolicyBuilder()
.RequireAuthenticatedUser()
.Build();
options.Filters.Add(new AuthorizeFilter(policy));
});

This allows me to get a claim in my Index.cshtml.cs:

System.Security.Claims.ClaimsIdentity ClaimsId = new();
ClaimsId = (System.Security.Claims.ClaimsIdentity)User.Identity;

Can I use this claim and pass it into CSOM or PnP calls to SharePoint Online?

1 Answer 1

0

I would recommend to use PnP Core SDK to connect Asp.net Core to SharePoint Online.

  • Below is ConfigureServices method code

// This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            // Add Microsoft.Identity.Web services
            services.AddMicrosoftIdentityWebAppAuthentication(Configuration)
                    .EnableTokenAcquisitionToCallDownstreamApi(new string[] { "User.Read" })
                    .AddInMemoryTokenCaches();

            // Add the PnP Core SDK library
            services.AddPnPCore();
            services.Configure<PnPCoreOptions>(Configuration.GetSection("PnPCore"));
            services.AddPnPCoreAuthentication();
            services.Configure<PnPCoreAuthenticationOptions>(Configuration.GetSection("PnPCore"));

            services.AddControllersWithViews(options =>
            {
                var policy = new AuthorizationPolicyBuilder()
                    .RequireAuthenticatedUser()
                    .Build();
                options.Filters.Add(new AuthorizeFilter(policy));
            });
            services.AddRazorPages();
        }

Refer this repository for Sample code in Asp.net core.

Refer this docs for detailed step required for configuration

2
  • Thank you for your response. I'll try the sample code to see if it works.
    – Bartels
    Mar 10, 2022 at 16:57
  • When I run the sample ASPNetCore sample, I get this error: Microsoft.Identity.Web.MicrosoftIdentityWebChallengeUserException: 'IDW10502: An MsalUiRequiredException was thrown due to a challenge for the user. See aka.ms/ms-id-web/ca_incremental-consent. ' How do I get around this?
    – Bartels
    Apr 5, 2022 at 18:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.