Change ClientId - Update on Installation of provider hosted App
-
05-01-2021 - |
题
When you publish a new provider hosted app using F5 - deployment, the new version of the app will be installed and a new ClientId will be generated. In addition that ClientId will automatically be saved in the Web.Config of the according Web-project.
Now I have a solution that has not only an app-project and a web-project, but also a few console applications that use the same mechanisms and configurarions and so also have a ClientId stored in their app.config.
Currently I have to manually copy the updated ClientId from the Web.Config to each App.Config of the console applications.
To avoid this I would like to use a fix ClientId (instead of a new one every time I redeploy the app). As an alternative, if the Clientid also is stored in the App.Configs automatically I would be happy, too.
I did not find where the ClientId-Web.Config-replacement takes place. If I could find that and could change it to also modify the other app.Configs that would be an alternative. But there seems to be nothing in the csproj-files.
TL;DR:
- I want to use a static ClientId on F5-Deployment on a Provider hosted app instead of a dynamic one
- (Alternative) How can I automatically store the ClientId not only in the web.Config but also in different App.Configs if (1.) is not possible
解决方案 2
Answering my own question I was able to copy the clientId and Secret which are automatically dynamically created and stored in the Web.Config to my console applications / Timerjobs:
private static void ReadClientId()
{
string startupPath="C:\\dev\\sharePointApp"; // There is the Web
string config="Web.Config";
string application="MyAppWeb";
var map = new ExeConfigurationFileMap
{
ExeConfigFilename = System.IO.Path.Combine(startupPath, application, config)
};
var configFile = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);
var clientId = configFile.AppSettings.Settings["ClientId"].Value;
var clientSecret = configFile.AppSettings.Settings["ClientSecret"].Value;
ConfigurationManager.AppSettings["ClientId"] = clientId;
ConfigurationManager.AppSettings["ClientSecret"] = clientSecret;
}
其他提示
I want to share my experience here and seems you mixed different approaches.
F5 used to develop a add-in so it's easy for us to dev/debug a add-in in developer site, VS will do many works for us(uninstall/install etc.).
If a add-in ready to development to 'prod', we should package a add-in and deploy to app catalog.
If you want to use fixed add-in ClientID, you could create it in /_layouts/15/AppRegNew.aspx, when you package your add-in, you could use the ClientID for your add-in.
If you create a add-in in a developer site with fixed ClientID, when you deploy the add-in to a different site app catalog and use it in a site, the add-in will use the fixed ClientID also.