public static string SetIdpConfiguration(Context context, int tenantId, bool startup = false) { var contractSettings = TenantUtilities.GetContractSettings(context, tenantId); if (contractSettings == null || contractSettings.SamlCompanyCode.IsNullOrEmpty() || contractSettings.SamlLoginUrl.IsNullOrEmpty() || contractSettings.SamlThumbprint.IsNullOrEmpty()) { return(null); } if (!FindCert(context, contractSettings.SamlThumbprint)) { return(null); } try { var section = (SustainsysSaml2Section)ConfigurationManager.GetSection("sustainsys.saml2"); var loginUrl = contractSettings.SamlLoginUrl; var idp = loginUrl.TrimEnd(new[] { '/' }).Substring(0, loginUrl.LastIndexOf('/') + 1); if (processing) { System.Threading.Thread.Sleep(300); } if (processing == false) { processing = true; try { IdentityProviderElement newProvider = null; CertificateElement newCert = null; var provider = section.IdentityProviders.FirstOrDefault(p => p.EntityId == idp); if (provider != null) { string signOnUrl = provider.SignOnUrl.ToString(); string findValue = provider.SigningCertificate.FindValue; if (signOnUrl == contractSettings.SamlLoginUrl && findValue == contractSettings.SamlThumbprint) { return($"~/Saml2/SignIn?idp={idp}"); } else { newProvider = provider; newCert = provider.SigningCertificate; WriteIdPSettings(contractSettings?.SamlLoginUrl, contractSettings?.SamlThumbprint, idp, newProvider, newCert); try { var spOptions = new Sustainsys.Saml2.Configuration.SPOptions(SustainsysSaml2Section.Current); var options = new Options(spOptions); SustainsysSaml2Section.Current.IdentityProviders.RegisterIdentityProviders(options); SustainsysSaml2Section.Current.Federations.RegisterFederations(options); var optionsFromConfiguration = typeof(Options).GetField("optionsFromConfiguration", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static); optionsFromConfiguration.SetValue(null, new Lazy <Options>(() => options, true)); //Sustainsys.Saml2.Mvc.Saml2Controller.Options = Options.FromConfiguration; } catch { WriteIdPSettings(signOnUrl, findValue, idp, newProvider, newCert); throw; } } } else { newProvider = new IdentityProviderElement(); newCert = new CertificateElement(); WriteIdPSettings(contractSettings?.SamlLoginUrl, contractSettings?.SamlThumbprint, idp, newProvider, newCert); AddIdP(section, newProvider); if (startup == false) { var spOptions = new Sustainsys.Saml2.Configuration.SPOptions(SustainsysSaml2Section.Current); var options = new Options(spOptions); SustainsysSaml2Section.Current.IdentityProviders.RegisterIdentityProviders(options); SustainsysSaml2Section.Current.Federations.RegisterFederations(options); var optionsFromConfiguration = typeof(Options).GetField("optionsFromConfiguration", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static); optionsFromConfiguration.SetValue(null, new Lazy <Options>(() => options, true)); //Sustainsys.Saml2.Mvc.Saml2Controller.Options = Options.FromConfiguration; } } } finally { processing = false; } } return($"~/Saml2/SignIn?idp={idp}"); } catch (System.Exception e) { new SysLogModel(context, e); return(null); } }