private void BuildGraphPropertyDDL() { AzureADObjectProperty[] aadPropValues = (AzureADObjectProperty[])Enum.GetValues(typeof(AzureADObjectProperty)); IEnumerable <AzureADObjectProperty> aadPropValuesSorted = aadPropValues.OrderBy(v => v.ToString()); foreach (AzureADObjectProperty prop in aadPropValuesSorted) { // Ensure property exists for the User object type if (AzureCP.GetPropertyValue(new User(), prop.ToString()) == null) { continue; } // Ensure property is of type System.String PropertyInfo pi = typeof(User).GetProperty(prop.ToString()); if (pi == null) { continue; } if (pi.PropertyType != typeof(System.String)) { continue; } this.DDLGraphPropertyToDisplay.Items.Add(new System.Web.UI.WebControls.ListItem(prop.ToString(), ((int)prop).ToString())); this.DDLDirectoryPropertyMemberUsers.Items.Add(new System.Web.UI.WebControls.ListItem(prop.ToString(), ((int)prop).ToString())); this.DDLDirectoryPropertyGuestUsers.Items.Add(new System.Web.UI.WebControls.ListItem(prop.ToString(), ((int)prop).ToString())); } }
private void ExecBaseFeatureActivated(Microsoft.SharePoint.SPFeatureReceiverProperties properties) { // Wrapper function for base FeatureActivated. // Used because base keywork can lead to unverifiable code inside lambda expression base.FeatureActivated(properties); SPSecurity.RunWithElevatedPrivileges(delegate() { try { ClaimsProviderLogging svc = ClaimsProviderLogging.Local; ClaimsProviderLogging.Log($"[{AzureCP._ProviderInternalName}] Activating farm-scoped feature for claims provider \"{AzureCP._ProviderInternalName}\"", TraceSeverity.High, EventSeverity.Information, ClaimsProviderLogging.TraceCategory.Configuration); var spTrust = AzureCP.GetSPTrustAssociatedWithCP(AzureCP._ProviderInternalName); if (spTrust != null) { AzureCPConfig existingConfig = AzureCPConfig.GetConfiguration(ClaimsProviderConstants.CONFIG_NAME); if (existingConfig == null) { AzureCPConfig.CreateConfiguration(ClaimsProviderConstants.CONFIG_ID, ClaimsProviderConstants.CONFIG_NAME, spTrust.Name); } else { ClaimsProviderLogging.Log($"[{AzureCP._ProviderInternalName}] Use configuration \"{ClaimsProviderConstants.CONFIG_NAME}\" found in the configuration database", TraceSeverity.High, EventSeverity.Information, ClaimsProviderLogging.TraceCategory.Configuration); } } } catch (Exception ex) { ClaimsProviderLogging.LogException(AzureCP._ProviderInternalName, $"activating farm-scoped feature for claims provider \"{AzureCP._ProviderInternalName}\"", ClaimsProviderLogging.TraceCategory.Configuration, ex); } }); }
private void BuildGraphPropertyDDLs(KeyValuePair <int, AzureADObject> azureObject, out string htmlCellGraphProperty, out string htmlCellGraphPropertyToDisplay) { string option = "<option value=\"{0}\" {1}>{2}</option>"; string graphPropertySelected = String.Empty; string graphPropertyToDisplaySelected = String.Empty; StringBuilder graphPropertyOptions = new StringBuilder(); StringBuilder graphPropertyToDisplayOptions = new StringBuilder(); bool graphPropertyToDisplayFound = false; foreach (GraphProperty prop in Enum.GetValues(typeof(GraphProperty))) { // Ensure property exists for the current object type if (azureObject.Value.ClaimEntityType == SPClaimEntityTypes.User) { if (AzureCP.GetGraphPropertyValue(new User(), prop.ToString()) == null) { continue; } } else { if (AzureCP.GetGraphPropertyValue(new Group(), prop.ToString()) == null) { continue; } //if (AzureCP.GetGraphPropertyValue(new Role(), prop.ToString()) == null) continue; } graphPropertySelected = azureObject.Value.GraphProperty == prop ? "selected" : String.Empty; if (azureObject.Value.GraphPropertyToDisplay == prop) { graphPropertyToDisplaySelected = "selected"; graphPropertyToDisplayFound = true; } else { graphPropertyToDisplaySelected = String.Empty; } // Utils.GetPropertyName throws an ArgumentException if GraphProperty == GraphProperty.None // Another problem is that Utils.GetPropertyName(prop) returns string with 1st character in lowercase //string strProp; //if (prop == GraphProperty.None) strProp = "None"; //else strProp = Utils.GetPropertyName(prop); graphPropertyOptions.Append(String.Format(option, prop.ToString(), graphPropertySelected, prop.ToString())); graphPropertyToDisplayOptions.Append(String.Format(option, prop.ToString(), graphPropertyToDisplaySelected, prop.ToString())); } // Insert at 1st position GraphProperty.None in GraphPropertyToDisplay DDL and select it if needed string selectNone = graphPropertyToDisplayFound ? String.Empty : "selected"; graphPropertyToDisplayOptions = graphPropertyToDisplayOptions.Insert(0, String.Format(option, GraphProperty.None, selectNone, GraphProperty.None)); htmlCellGraphProperty = String.Format(HtmlCellGraphProperty, azureObject.Value.GraphProperty, azureObject.Key, graphPropertyOptions.ToString()); //string graphPropertyToDisplaySpanDisplay = azureObject.Value.GraphPropertyToDisplay == GraphProperty.None ? String.Empty : azureObject.Value.GraphPropertyToDisplay.ToString(); htmlCellGraphPropertyToDisplay = String.Format(HtmlCellGraphPropertyToDisplay, azureObject.Value.GraphPropertyToDisplay, azureObject.Key, graphPropertyToDisplayOptions.ToString()); }
protected void Page_Load(object sender, EventArgs e) { if (!this.IsPostBack) { FileVersionInfo fvi = FileVersionInfo.GetVersionInfo(Assembly.GetExecutingAssembly().Location); LblTitle.Text = String.Format("AzureCP v{0} - <a href=\"https://github.com/Yvand/AzureCP\" target=\"_blank\">GitHub.com/Yvand/AzureCP</a>", fvi.FileVersion); } // Get trust currently associated with AzureCP, if any CurrentTrustedLoginProvider = AzureCP.GetSPTrustAssociatedWithCP(AzureCP._ProviderInternalName); if (null == CurrentTrustedLoginProvider) { // Claim provider is currently not associated with any trust. // Display a message in the page and disable controls this.LabelErrorMessage.Text = TextErrorNoTrustAssociation; this.BtnOK.Enabled = this.BtnOKTop.Enabled = this.BtnAddLdapConnection.Enabled = this.BtnTestAzureTenantConnection.Enabled = false; this.AllowPersistedObjectUpdate = false; return; } SPSecurity.RunWithElevatedPrivileges(delegate() { // Get SPPersisted Object and create it if it doesn't exist PersistedObject = AzureCPConfig.GetFromConfigDB(); if (PersistedObject == null) { this.Web.AllowUnsafeUpdates = true; PersistedObject = AzureCPConfig.CreatePersistedObject(); this.Web.AllowUnsafeUpdates = false; } }); this.IdentityClaim = PersistedObject.AzureADObjects.Find(x => String.Equals(CurrentTrustedLoginProvider.IdentityClaimTypeInformation.MappedClaimType, x.ClaimType, StringComparison.InvariantCultureIgnoreCase) && !x.CreateAsIdentityClaim); if (null == this.IdentityClaim) { // Identity claim type is missing in the attributes list this.LabelErrorMessage.Text = String.Format(this.TextErrorNoIdentityClaimType, CurrentTrustedLoginProvider.DisplayName, CurrentTrustedLoginProvider.IdentityClaimTypeInformation.MappedClaimType); this.BtnOK.Enabled = this.BtnOKTop.Enabled = this.BtnAddLdapConnection.Enabled = this.BtnTestAzureTenantConnection.Enabled = false; return; } if (ViewState["PersistedObjectVersion"] == null) { ViewState.Add("PersistedObjectVersion", PersistedObject.Version); } if ((long)ViewState["PersistedObjectVersion"] != PersistedObject.Version) { // PersistedObject changed since last time. Should not allow any update this.LabelErrorMessage.Text = TextErrorPersistedObjectStale; this.AllowPersistedObjectUpdate = false; return; } if (!this.IsPostBack) { PopulateFields(); } }
/// <summary> /// Initialize controls as needed if prerequisites are ok, otherwise deactivate controls and show error message /// </summary> protected void Initialize() { ConfigStatus status = ValidatePrerequisite(); if (status != ConfigStatus.AllGood && status != ConfigStatus.NoIdentityClaimType) { this.LabelErrorMessage.Text = base.MostImportantError; this.HideAllContent = true; this.BtnCreateNewItem.Visible = false; return; } TrustName = CurrentTrustedLoginProvider.Name; if (!this.IsPostBack) { // NEW ITEM FORM // Populate LDAPObjectType DDL foreach (var value in Enum.GetValues(typeof(DirectoryObjectType))) { DdlNewDirectoryObjectType.Items.Add(value.ToString()); } // Populate picker entity metadata DDL DdlNewEntityMetadata.Items.Add(String.Empty); foreach (object field in typeof(PeopleEditorEntityDataKeys).GetFields()) { DdlNewEntityMetadata.Items.Add(((System.Reflection.FieldInfo)field).Name); } DdlNewGraphProperty.Items.Add(String.Empty); DdlNewGraphPropertyToDisplay.Items.Add(String.Empty); foreach (object field in typeof(AzureADObjectProperty).GetFields()) { string prop = ((System.Reflection.FieldInfo)field).Name; if (AzureCP.GetPropertyValue(new User(), prop) == null) { continue; } //if (AzureCP.GetGraphPropertyValue(new Group(), prop) == null) continue; //if (AzureCP.GetGraphPropertyValue(new Role(), prop) == null) continue; DdlNewGraphProperty.Items.Add(prop); DdlNewGraphPropertyToDisplay.Items.Add(prop); } } BuildAttributesListTable(this.IsPostBack); }
private void BuildGraphPropertyDDL() { foreach (GraphProperty prop in Enum.GetValues(typeof(GraphProperty))) { // Ensure property exists for the User object type if (AzureCP.GetGraphPropertyValue(new User(), prop.ToString()) == null) { continue; } // Ensure property is of type System.String PropertyInfo pi = typeof(User).GetProperty(prop.ToString()); if (pi == null) { continue; } if (pi.PropertyType != typeof(System.String)) { continue; } this.DDLGraphPropertyToDisplay.Items.Add(new ListItem(prop.ToString(), ((int)prop).ToString())); } }
/// <summary> /// Ensures configuration is valid to proceed /// </summary> /// <returns></returns> public virtual ConfigStatus ValidatePrerequisite() { if (!this.IsPostBack) { // DataBind() must be called to bind attributes that are set as "<%# #>"in .aspx // But only during initial page load, otherwise it would reset bindings in other controls like SPGridView DataBind(); ViewState.Add("ClaimsProviderName", ClaimsProviderName); ViewState.Add("PersistedObjectName", PersistedObjectName); ViewState.Add("PersistedObjectID", PersistedObjectID); } else { ClaimsProviderName = ViewState["ClaimsProviderName"].ToString(); PersistedObjectName = ViewState["PersistedObjectName"].ToString(); PersistedObjectID = ViewState["PersistedObjectID"].ToString(); } Status = ConfigStatus.AllGood; if (String.IsNullOrEmpty(ClaimsProviderName)) { Status |= ConfigStatus.ClaimsProviderNamePropNotSet; } if (String.IsNullOrEmpty(PersistedObjectName)) { Status |= ConfigStatus.PersistedObjectNamePropNotSet; } if (String.IsNullOrEmpty(PersistedObjectID)) { Status |= ConfigStatus.PersistedObjectIDPropNotSet; } if (Status != ConfigStatus.AllGood) { ClaimsProviderLogging.Log($"[{ClaimsProviderName}] {MostImportantError}", TraceSeverity.Unexpected, EventSeverity.Error, TraceCategory.Configuration); // Should not go further if those requirements are not met return(Status); } if (CurrentTrustedLoginProvider == null) { CurrentTrustedLoginProvider = AzureCP.GetSPTrustAssociatedWithCP(this.ClaimsProviderName); if (CurrentTrustedLoginProvider == null) { Status |= ConfigStatus.NoSPTrustAssociation; return(Status); } } if (PersistedObject == null) { Status |= ConfigStatus.PersistedObjectNotFound; } if (Status != ConfigStatus.AllGood) { ClaimsProviderLogging.Log($"[{ClaimsProviderName}] {MostImportantError}", TraceSeverity.Unexpected, EventSeverity.Error, TraceCategory.Configuration); // Should not go further if those requirements are not met return(Status); } // AzureCPConfig.GetConfiguration will call method AzureCPConfig.CheckAndCleanConfiguration(); //PersistedObject.CheckAndCleanConfiguration(CurrentTrustedLoginProvider.Name); PersistedObject.ClaimTypes.SPTrust = CurrentTrustedLoginProvider; if (IdentityCTConfig == null && Status == ConfigStatus.AllGood) { IdentityCTConfig = PersistedObject.ClaimTypes.FirstOrDefault(x => String.Equals(CurrentTrustedLoginProvider.IdentityClaimTypeInformation.MappedClaimType, x.ClaimType, StringComparison.InvariantCultureIgnoreCase) && !x.UseMainClaimTypeOfDirectoryObject) as IdentityClaimTypeConfig; if (IdentityCTConfig == null) { Status |= ConfigStatus.NoIdentityClaimType; } } if (PersistedObjectVersion != PersistedObject.Version) { Status |= ConfigStatus.PersistedObjectStale; } if (Status != ConfigStatus.AllGood) { ClaimsProviderLogging.Log($"[{ClaimsProviderName}] {MostImportantError}", TraceSeverity.Unexpected, EventSeverity.Error, TraceCategory.Configuration); } return(Status); }
private void BuildGraphPropertyDDLs(KeyValuePair <int, ClaimTypeConfig> azureObject, out string htmlCellGraphProperty, out string htmlCellGraphPropertyToDisplay, out string htmlCellDirectoryObjectType) { string option = "<option value=\"{0}\" {1}>{2}</option>"; string graphPropertySelected = String.Empty; string graphPropertyToDisplaySelected = String.Empty; StringBuilder graphPropertyOptions = new StringBuilder(); StringBuilder graphPropertyToDisplayOptions = new StringBuilder(); StringBuilder directoryObjectTypeOptions = new StringBuilder(); bool graphPropertyToDisplayFound = false; // Build EntityType list string selectedText = azureObject.Value.EntityType == DirectoryObjectType.User ? "selected" : String.Empty; directoryObjectTypeOptions.Append(String.Format(option, DirectoryObjectType.User.ToString(), selectedText, DirectoryObjectType.User.ToString())); selectedText = azureObject.Value.EntityType == DirectoryObjectType.Group ? "selected" : String.Empty; directoryObjectTypeOptions.Append(String.Format(option, DirectoryObjectType.Group.ToString(), selectedText, DirectoryObjectType.Group.ToString())); // Build DirectoryObjectProperty and DirectoryObjectPropertyToShowAsDisplayText lists foreach (AzureADObjectProperty prop in Enum.GetValues(typeof(AzureADObjectProperty))) { // Ensure property exists for the current object type if (azureObject.Value.EntityType == DirectoryObjectType.User) { if (AzureCP.GetPropertyValue(new User(), prop.ToString()) == null) { continue; } } else { if (AzureCP.GetPropertyValue(new Group(), prop.ToString()) == null) { continue; } } graphPropertySelected = azureObject.Value.DirectoryObjectProperty == prop ? "selected" : String.Empty; if (azureObject.Value.DirectoryObjectPropertyToShowAsDisplayText == prop) { graphPropertyToDisplaySelected = "selected"; graphPropertyToDisplayFound = true; } else { graphPropertyToDisplaySelected = String.Empty; } graphPropertyOptions.Append(String.Format(option, prop.ToString(), graphPropertySelected, prop.ToString())); graphPropertyToDisplayOptions.Append(String.Format(option, prop.ToString(), graphPropertyToDisplaySelected, prop.ToString())); } // Insert at 1st position AzureADObjectProperty.NotSet in GraphPropertyToDisplay DDL and select it if needed string selectNotSet = graphPropertyToDisplayFound ? String.Empty : "selected"; graphPropertyToDisplayOptions = graphPropertyToDisplayOptions.Insert(0, String.Format(option, AzureADObjectProperty.NotSet, selectNotSet, AzureADObjectProperty.NotSet)); htmlCellGraphProperty = String.Format(HtmlCellGraphProperty, azureObject.Value.DirectoryObjectProperty, azureObject.Key, graphPropertyOptions.ToString()); string graphPropertyToDisplaySpanDisplay = azureObject.Value.DirectoryObjectPropertyToShowAsDisplayText == AzureADObjectProperty.NotSet ? String.Empty : azureObject.Value.DirectoryObjectPropertyToShowAsDisplayText.ToString(); htmlCellGraphPropertyToDisplay = String.Format(HtmlCellGraphPropertyToDisplay, graphPropertyToDisplaySpanDisplay, azureObject.Key, graphPropertyToDisplayOptions.ToString()); htmlCellDirectoryObjectType = String.Format(HtmlCellDirectoryObjectType, azureObject.Value.EntityType, azureObject.Key, directoryObjectTypeOptions.ToString()); }
protected void Page_Load(object sender, EventArgs e) { // Get trust currently associated with AzureCP, if any CurrentTrustedLoginProvider = AzureCP.GetSPTrustAssociatedWithCP(AzureCP._ProviderInternalName); if (null == CurrentTrustedLoginProvider) { // Claim provider is currently not associated with any trust. // Display a message in the page and disable controls this.LabelErrorMessage.Text = TextErrorNoTrustAssociation; this.HideAllContent = true; this.BtnCreateNewItem.Visible = false; return; } SPSecurity.RunWithElevatedPrivileges(delegate() { // Get SPPersisted Object and create it if it doesn't exist PersistedObject = AzureCPConfig.GetFromConfigDB(); if (PersistedObject == null) { this.Web.AllowUnsafeUpdates = true; PersistedObject = AzureCPConfig.CreatePersistedObject(); this.Web.AllowUnsafeUpdates = false; } }); if (ViewState["PersistedObjectVersion"] == null) { ViewState.Add("PersistedObjectVersion", PersistedObject.Version); } if ((long)ViewState["PersistedObjectVersion"] != PersistedObject.Version) { // PersistedObject changed since last time. Should not allow any update this.LabelErrorMessage.Text = TextErrorPersistedObjectStale; this.AllowPersistedObjectUpdate = false; return; } TrustName = CurrentTrustedLoginProvider.Name; if (!this.IsPostBack) { New_DdlPermissionMetadata.Items.Add(String.Empty); foreach (object field in typeof(PeopleEditorEntityDataKeys).GetFields()) { New_DdlPermissionMetadata.Items.Add(((System.Reflection.FieldInfo)field).Name); } New_DdlGraphProperty.Items.Add(String.Empty); New_DdlGraphPropertyToDisplay.Items.Add(String.Empty); foreach (object field in typeof(GraphProperty).GetFields()) { string prop = ((System.Reflection.FieldInfo)field).Name; if (AzureCP.GetGraphPropertyValue(new User(), prop) == null) { continue; } //if (AzureCP.GetGraphPropertyValue(new Group(), prop) == null) continue; //if (AzureCP.GetGraphPropertyValue(new Role(), prop) == null) continue; New_DdlGraphProperty.Items.Add(prop); New_DdlGraphPropertyToDisplay.Items.Add(prop); } } BuildAttributesListTable(this.IsPostBack); }