In general, it's possible to use ASP.NET Core web app as add-in web for provider hosted SharePoint add-in and it's not very difficult. The main issues are
SharePointContext.cs, which use System.Web namespace, which is not available in .NET Standard. However, it can be easily replaced with classes from .NET Standard.
I've created a sample SharePoint (Online) add-in with ASP.NET Core 2.1 web app here.
How to run it
As you rightly mentioned SharePoint add-in project type will be unlinked. That's why F5 experience will not work. Instead, you should upload your add-in to SharePoint manually.
For that purpose, you should have developer site collection. Alternatively you can activate developer feature and enable sideloading of apps on an existing site collection using below PnP-PowerShell:
Connect-PnPOnline -Url https://org.sharepoint.com/sites/my-site
Enable-PnPFeature -Identity e374875e-06b6-11e0-b0fa-57f5dfd72085 -Scope Site
Steps to run the sample
- In VS press Ctrl+F5 to run the web app. You will see a web page with an error, it's ok because we haven't opened it from SharePoint yet. We just need to make sure that it's running.
- In your site collection go to
_layouts/15/appregnew.aspx and register new app. For app domain use
localhost:44316 (be aware that port might be different in your environment, so please check that port match the port from web app running from step
#1), for redirect url
https://localhost:44316/ (or from step
- In SharePointAddIn project -> AppManifest.xml replace ClientId with one generated at step
AddinWebApp -> appsettings.Development.json update ClientId and ClientSecret with values from step
Right click on SharePointAddIn -> Publish -> Package. Upload .app file to App Packages library.
- Go to Apps in Testing library -> new app to deploy and deploy the app from step
#5. Wait for the installation to be completed.
- You're' ready to go. Click on your app from Site Contents page and you will see the home page with your logged in SharePoint user in the middle of the page.
This solution is similar to OOB "SharePoint Addin" in VS. This solution works with SharePoint Online only (it's a lot more difficult to make it work for on-prem). The only differences from OOB project type are that SharePoint project is unlinked, ASP.NET Core 2.1 is used, TokenHelper and SharePointContext were modified a bit to support ASP.NET Core.
TargetFramework is set to net471, because SharePoint CSOM libraries aren't yet ported to .NET Standard, which means that you can't run this sample on Linux
If you use Azure ad authentication and have access token with
user_impersonation scope, you can use on-behalf-of flow to exchange that token on an access token for another resource (SharePoint):
var userImpersonationAccessToken = "initial access token";
ClientCredential clientCred = new ClientCredential(ClientId, ClientSecret);
UserAssertion userAssertion = new UserAssertion(userImpersonationAccessToken);
//For production, use a Token Cache like Redis https://blogs.msdn.microsoft.com/mrochon/2016/09/19/using-redis-as-adal-token-cache/
var authContext = new AuthenticationContext(authority);
AuthenticationResult authResult = await authContext.AcquireTokenAsync("https://org.sharepoint.com", clientCred, userAssertion);
var spAccessToken = authResult.AccessToken;
the token will be app+user