protected void WebAppSelector_OnChanged(object sender, EventArgs e)
        {
            string           providerName;
            string           server, dn, loginAttrib;
            bool             useSsl;
            int              port = 0x185;
            ProviderSettings providerSettings;
            SPWebApplication selectedWebApp = ddlWebApp.CurrentItem;
            var              zone           = GetZone(ddlZonePicker.SelectedValue);

            foreach (SPFormsAuthenticationProvider membershipProvider in selectedWebApp.GetIisSettingsWithFallback(zone).ClaimsAuthenticationProviders.OfType <SPFormsAuthenticationProvider>())
            {
                providerName     = membershipProvider.DisplayName;
                providerSettings = GetMembershipProvider(selectedWebApp, zone, providerName);

                if (providerSettings == null)
                {
                    break;
                }
                server      = providerSettings.Parameters["server"];
                port        = Convert.ToInt32(providerSettings.Parameters["port"]);
                loginAttrib = providerSettings.Parameters["userNameAttribute"];
                dn          = providerSettings.Parameters["userContainer"];
                useSsl      = Convert.ToBoolean(providerSettings.Parameters["useSSL"]);

                var de = DirEntry(server, port, dn, useSsl);


                if (de != null)
                {
                    var results = ResultCollection(de);
                    FillTable(results);
                }
            }
        }
Exemplo n.º 2
0
        public List <IdentityProvider> getIdentityProviders()
        {
            UPSBrowserLogger.LogDebug(loggingCategory, "getIdentityProviders invoked");
            List <IdentityProvider> identityProvidersToReturn = new List <IdentityProvider>();

            try
            {
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    UPSBrowserLogger.LogDebug(loggingCategory, "Running with elevated privileges");

                    try
                    {
                        SPContext spContext                  = Microsoft.SharePoint.SPContext.Current;
                        SPWebApplication webApp              = spContext.Site.WebApplication;
                        SPUrlZone spUrlZone                  = spContext.Site.Zone;
                        SPIisSettings spIisSettings          = webApp.GetIisSettingsWithFallback(spUrlZone);
                        SPSecurityTokenServiceManager sptMgr = SPSecurityTokenServiceManager.Local;

                        foreach (SPAuthenticationProvider prov in spIisSettings.ClaimsAuthenticationProviders)
                        {
                            if (prov.GetType() == typeof(Microsoft.SharePoint.Administration.SPTrustedAuthenticationProvider))
                            {
                                var lp =
                                    from SPTrustedLoginProvider spt in
                                    sptMgr.TrustedLoginProviders
                                    where spt.DisplayName == prov.DisplayName
                                    select spt;

                                if ((lp != null) && (lp.Count() > 0))
                                {
                                    SPTrustedLoginProvider loginProv = lp.First();
                                    identityProvidersToReturn.Add(new IdentityProvider
                                    {
                                        Name        = loginProv.Name,
                                        DisplayName = loginProv.DisplayName,
                                        Description = loginProv.Description,
                                    });
                                }
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        UPSBrowserLogger.LogError(loggingCategory, e.Message);
                    };
                });
            }
            catch (System.Exception e)
            {
                UPSBrowserLogger.LogError(loggingCategory, $"Error while trying to elevate privileges: {e.Message}");
            };

            return(identityProvidersToReturn);
        }
Exemplo n.º 3
0
        private ClaimsContext(SPContext context)
        {
            SPWebApplication webApplication = context.Site.WebApplication;

            foreach (SPAlternateUrl mapping in webApplication.AlternateUrls)
            {
                SPIisSettings settings = webApplication.GetIisSettingsWithFallback(mapping.UrlZone);
                if (settings.UseFormsClaimsAuthenticationProvider)
                {
                    this.FormsMembershipProvider = Membership.Providers[settings.FormsClaimsAuthenticationProvider.MembershipProvider];
                    this.FormsRoleProvider       = Roles.Providers[settings.FormsClaimsAuthenticationProvider.RoleProvider];
                    break;
                }
            }

            SPUser currentUser = context.Web.CurrentUser;

            if (currentUser != null && SPClaimProviderManager.IsEncodedClaim(currentUser.LoginName))
            {
                SPClaim claim = SPClaimProviderManager.Local.DecodeClaim(currentUser.LoginName);
                this.IsWindowsUser = claim.OriginalIssuer == "Windows";

                if (claim.OriginalIssuer.StartsWith("Forms:"))
                {
                    if (this.FormsMembershipProvider != null && this.FormsMembershipProvider.Name.Equals(claim.OriginalIssuer.Substring(6), StringComparison.OrdinalIgnoreCase))
                    {
                        this.FormsUser = this.FormsMembershipProvider.GetUser(claim.Value, false);
                        if (this.FormsUser != null)
                        {
                            this.IsFormsUser      = true;
                            this.FormsUserId      = claim.Value;
                            this.FormsUserProfile = ProfileBase.Create(this.FormsUser.UserName);
                        }
                    }
                }
            }
            this.IsAnonymous = !this.IsFormsUser && !this.IsWindowsUser;
        }
Exemplo n.º 4
0
        public static void Delete(SearchResultCollection users, string loginAttribute, SPWebApplication webApplication, string serverName, int portNumber, SPUrlZone zone)
        {
            SPSite site = null;

            try
            {
                site = new SPSite(WebApplication.GetResponseUri(zone).AbsoluteUri);

                SPIisSettings iisSettings = webApplication.GetIisSettingsWithFallback(zone);

                foreach (SPAuthenticationProvider provider in iisSettings.ClaimsAuthenticationProviders)
                {
                    if (provider is SPFormsAuthenticationProvider)
                    {
                        SPFormsAuthenticationProvider formsProvider = provider as SPFormsAuthenticationProvider;

                        SPServiceContext   serviceContext = SPServiceContext.GetContext(site);
                        UserProfileManager uPM            = new UserProfileManager(serviceContext);

                        SPSecurity.RunWithElevatedPrivileges(delegate()
                        {
                            string search = ClaimsIdentifier + "|" + formsProvider.MembershipProvider + "|";

                            List <UserProfile> uPAResults = uPM.Search(search).Cast <UserProfile>().ToList();
                            List <SearchResult> usersList = users.Cast <SearchResult>().ToList();

                            var query = usersList.Select(sr => sr.GetDirectoryEntry().Properties["distinguishedName"].Value.ToString());

                            HashSet <string> paths = new HashSet <string>(query);

                            var profiles = uPAResults.Select(profile => new
                            {
                                ShouldKeep = paths.Contains(profile[PropertyConstants.DistinguishedName].Value.ToString()),
                                Profile    = profile
                            });

                            foreach (var profile in profiles.Where(result => !result.ShouldKeep))
                            {
                                try
                                {
                                    uPM.RemoveProfile(profile.Profile);
                                    Logging.LogMessage(212, Logging.LogCategories.Profiles, TraceSeverity.Verbose, "Removed profile " +
                                                       profile.Profile[PropertyConstants.DistinguishedName].Value, new object[] { null });
                                }
                                catch (Exception ex)
                                {
                                    Logging.LogMessage(502, Logging.LogCategories.Profiles,
                                                       TraceSeverity.Unexpected,
                                                       "Failed to delete profile " + profile.Profile[PropertyConstants.DistinguishedName].Value +
                                                       " " + ex.Message, new object[] { null });
                                }
                            }
                        });
                    }
                }
            }
            catch (Exception ex)
            {
                Logging.LogMessage(502, Logging.LogCategories.Profiles, TraceSeverity.Unexpected, ex.Message, new object[] { null });
            }

            finally
            {
                if (site != null)
                {
                    site.Dispose();
                }
            }
        }
Exemplo n.º 5
0
        public static void Create(SearchResultCollection users, string loginAttribute, SPWebApplication webApplication, string serverName, int portNumber, SPUrlZone zone)
        {
            foreach (SearchResult user in users)
            {
                DirectoryEntry de2  = user.GetDirectoryEntry();
                SPSite         site = null;
                try
                {
                    site = new SPSite(WebApplication.GetResponseUri(zone).AbsoluteUri);

                    SPIisSettings iisSettings = webApplication.GetIisSettingsWithFallback(zone);

                    foreach (SPAuthenticationProvider provider in iisSettings.ClaimsAuthenticationProviders)
                    {
                        if (provider is SPFormsAuthenticationProvider)
                        {
                            SPFormsAuthenticationProvider formsProvider = provider as SPFormsAuthenticationProvider;
                            SPServiceContext   serviceContext           = SPServiceContext.GetContext(site);
                            UserProfileManager uPM = new UserProfileManager(serviceContext);

                            SPSecurity.RunWithElevatedPrivileges(delegate()
                            {
                                if (de2.Properties[loginAttribute].Value != null)
                                {
                                    if (!uPM.UserExists(ClaimsIdentifier + "|" + formsProvider.MembershipProvider + "|" +
                                                        de2.Properties[loginAttribute].Value.ToString()))
                                    {
                                        Department = (de2.Properties[DepartmentAttrib].Value == null) ? String.Empty :
                                                     de2.Properties[DepartmentAttrib].Value.ToString();
                                        DistinguishedName = de2.Properties[DistinguishedNameAttrib].Value.ToString();
                                        FirstName         = (de2.Properties[FirstNameAttrib].Value == null) ? String.Empty :
                                                            de2.Properties[FirstNameAttrib].Value.ToString();
                                        LastName = (de2.Properties[LastNameAttrib].Value == null) ? String.Empty :
                                                   de2.Properties[LastNameAttrib].Value.ToString();
                                        Office = (de2.Properties[OfficeAttrib].Value == null) ? String.Empty :
                                                 de2.Properties[OfficeAttrib].Value.ToString();
                                        PreferredName = (de2.Properties[PreferredNameAttrib].Value == null) ? String.Empty :
                                                        de2.Properties[PreferredNameAttrib].Value.ToString();
                                        UserTitle = (de2.Properties[UserTitleAttrib].Value == null) ? String.Empty :
                                                    de2.Properties[UserTitleAttrib].Value.ToString();
                                        WebSite = (de2.Properties[WebSiteAttrib].Value == null) ? String.Empty :
                                                  de2.Properties[WebSiteAttrib].Value.ToString();
                                        WorkEmail = (de2.Properties[WorkEmailAttrib].Value == null) ? String.Empty :
                                                    de2.Properties[WorkEmailAttrib].Value.ToString();
                                        WorkPhone = (de2.Properties[WorkPhoneAttrib].Value == null) ? String.Empty :
                                                    de2.Properties[WorkPhoneAttrib].Value.ToString();

                                        UserProfile newProfile = uPM.CreateUserProfile(ClaimsIdentifier + "|" + formsProvider.MembershipProvider + "|" +
                                                                                       de2.Properties[loginAttribute].Value.ToString(), PreferredName);

                                        newProfile[PropertyConstants.Department].Add(Department);
                                        newProfile[PropertyConstants.DistinguishedName].Add(DistinguishedName);
                                        newProfile[PropertyConstants.FirstName].Add(FirstName);
                                        newProfile[PropertyConstants.LastName].Add(LastName);
                                        newProfile[PropertyConstants.Office].Add(Office);
                                        newProfile[PropertyConstants.Title].Add(UserTitle);
                                        newProfile[PropertyConstants.WebSite].Add(WebSite);
                                        newProfile[PropertyConstants.WorkEmail].Add(WorkEmail);
                                        newProfile[PropertyConstants.WorkPhone].Add(WorkPhone);

                                        try
                                        {
                                            newProfile.Commit();
                                            Logging.LogMessage(210, Logging.LogCategories.Profiles, TraceSeverity.Verbose, "Created profile " +
                                                               DistinguishedName, new object[] { null });
                                        }
                                        catch (Exception ex)
                                        {
                                            Logging.LogMessage(510, Logging.LogCategories.Profiles, TraceSeverity.Unexpected, "Failed to create profile " +
                                                               DistinguishedName + " " + ex.Message, new object[] { null });
                                        }
                                    }
                                    else if (uPM.UserExists(ClaimsIdentifier + "|" + formsProvider.MembershipProvider + "|" +
                                                            de2.Properties[loginAttribute].Value.ToString()))
                                    {
                                        UserProfile updateProfile = uPM.GetUserProfile(ClaimsIdentifier + "|" + formsProvider.MembershipProvider + "|" +
                                                                                       de2.Properties[loginAttribute].Value.ToString());

                                        updateProfile[PropertyConstants.Department].Value = (de2.Properties[DepartmentAttrib].Value == null) ? String.Empty :
                                                                                            de2.Properties[DepartmentAttrib].Value.ToString();
                                        updateProfile[PropertyConstants.DistinguishedName].Value = de2.Properties[DistinguishedNameAttrib].Value.ToString();
                                        updateProfile[PropertyConstants.FirstName].Value         = (de2.Properties[FirstNameAttrib].Value == null) ? String.Empty :
                                                                                                   de2.Properties[FirstNameAttrib].Value.ToString();
                                        updateProfile[PropertyConstants.LastName].Value = (de2.Properties[LastNameAttrib].Value == null) ? String.Empty :
                                                                                          de2.Properties[LastNameAttrib].Value.ToString();
                                        updateProfile[PropertyConstants.Office].Value = (de2.Properties[OfficeAttrib].Value == null) ? String.Empty :
                                                                                        de2.Properties[OfficeAttrib].Value.ToString();
                                        updateProfile[PropertyConstants.PreferredName].Value = (de2.Properties[PreferredNameAttrib].Value == null) ? String.Empty :
                                                                                               de2.Properties[PreferredNameAttrib].Value.ToString();
                                        updateProfile[PropertyConstants.Title].Value = (de2.Properties[UserTitleAttrib].Value == null) ? String.Empty :
                                                                                       de2.Properties[UserTitleAttrib].Value.ToString();
                                        updateProfile[PropertyConstants.WebSite].Value = (de2.Properties[WebSiteAttrib].Value == null) ? String.Empty :
                                                                                         de2.Properties[WebSiteAttrib].Value.ToString();
                                        updateProfile[PropertyConstants.WorkEmail].Value = (de2.Properties[WorkEmailAttrib].Value == null) ? String.Empty :
                                                                                           de2.Properties[WorkEmailAttrib].Value.ToString();
                                        updateProfile[PropertyConstants.WorkPhone].Value = (de2.Properties[WorkPhoneAttrib].Value == null) ? String.Empty :
                                                                                           de2.Properties[WorkPhoneAttrib].Value.ToString();

                                        try
                                        {
                                            updateProfile.Commit();
                                            Logging.LogMessage(211, Logging.LogCategories.Profiles, TraceSeverity.Verbose, "Updated profile " +
                                                               updateProfile[PropertyConstants.DistinguishedName].Value, new object[] { null });
                                        }
                                        catch (Exception ex)
                                        {
                                            Logging.LogMessage(511, Logging.LogCategories.Profiles, TraceSeverity.Unexpected, "Failed to update profile " +
                                                               updateProfile[PropertyConstants.DistinguishedName].Value + " " + ex.Message, new object[] { null });
                                        }
                                    }
                                }
                            });
                        }
                    }
                }
                catch (Exception ex)
                {
                    Logging.LogMessage(502, Logging.LogCategories.Profiles, TraceSeverity.Unexpected, ex.Message, new object[] { null });
                }

                finally
                {
                    if (site != null)
                    {
                        site.Dispose();
                    }
                }
            }
        }