mercredi 7 novembre 2018

How to write injectable reusable code instead of hardcoding the authentication on every method

I have a webapi which talks with Sharepoint to do tenant level operations, like listing site collections, creating sites, etc etc.

The web api methods works perfect, but however the authentication mechanism might change in the future, now the only way to create a modern site collection is via authenticated user and password, in the future they might enable app only authentication to create modern sites: https://sharepoint.uservoice.com/forums/329220-sharepoint-dev-platform/suggestions/34236700-ability-to-use-app-only-calls-to-create-modern-sit

The problem with my code, its that I have hardcoded the authentication line in every method of the webapi, and I would like to abstract that line in a way outside of the webapi class, that whenever Microsoft implements a different authentication mechanism, that I can easily change that class and not the web api methods one by one.

 [HttpPost]
        [Route("")]
        //[Route("api/SiteCollections/CreateModernSite")]
        public async Task<IHttpActionResult> CreateModernSite([FromBody]NewSiteInformation model)
        {

            if (ModelState.IsValid)

            {
                var tenant = await TenantHelper.GetTenantAsync();
                using (var context = new OfficeDevPnP.Core.AuthenticationManager().GetSharePointOnlineAuthenticatedContextTenant("https://xxx.sharepoint.com/sites/tesmmodernsite1", "xxx@xxx.onmicrosoft.com", "xxx"))
                {
                    try
                    {

Aucun commentaire:

Enregistrer un commentaire