如何以编程方式删除SharePoint用户配置文件 - 使用FBA时? [英] How to delete SharePoint User Profile programmatically - while using FBA?

查看:80
本文介绍了如何以编程方式删除SharePoint用户配置文件 - 使用FBA时?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我正在尝试开发一个webpart,管理员可以使用它来添加,编辑和删除SharePoint和SQL成员中的用户。

此webpart必须在FBA门户中工作。

这里,我在删除SharePoint用户配置文件时遇到问题。我的代码如下。但是,此代码仅适用于Windows身份验证门户,而不适用于基于表单的身份验证门户。

当我使用SQL-Membership用户登录门户时收到错误消息。

以下是错误消息:拒绝访问:只有管理员可能会删除用户个人资料。,我怎么用 RunWithElevatedPrivileges



似乎 SPSecurity.RunWithElevatedPrivileges 在SPFarm中不起作用?

我不知道如何摆脱这个错误。:(



有没有人可以帮助我?



干杯,

Hi guys,

I'm trying to develope a webpart with which "Admins" can to add, edit and delete users both in SharePoint and SQL-Membership.
This webpart has to work in a FBA portal.
Here, I have a problem with deleting SharePoint User profiles. My code is as below. But this code is working in just in Windows Authentication portals, not Form Based Authentication portals.
I get an error message when I log in to the portal with a SQL-Membership User.
Here is the error message:"Access Denied: Only an administrator may delete a user profile.", How ever I am using RunWithElevatedPrivileges!

It seems that SPSecurity.RunWithElevatedPrivileges does not work in SPFarm?
I don't know how I can get rid of this error.:(

Is there anyone who can help me?

Cheers,

SPSecurity.RunWithElevatedPrivileges(
           delegate
           {
                   try
                   {
                       //- Step 1: Delete user from membership
                       Utility.DeleteMembershipUser(fullUserName);
                   }
                   catch (Exception ex)
                   {
                       HandleExceptionAsMinor(ex, lblErrorMessage, "deleting the user from Membership Provider", OverrideErrorMessage);
                   }

                   bool catchAccessDenied = SPSecurity.CatchAccessDeniedException;
                   SPSecurity.CatchAccessDeniedException = false;
                   try
                   {
                       //- Step 2: Delete User Profile
                       SPUtility.ValidateFormDigest();
                       SPContext.Current.Site.AllowUnsafeUpdates = true;
                       var sc = ServerContext.GetContext(site);
                       var userProfileManager = new UserProfileManager(sc);
                       if (userProfileManager.UserExists(fullUserName))
                       {
                           UserProfile userProfile = userProfileManager.GetUserProfile(fullUserName);
                           if (userProfile.PersonalSite != null)
                               userProfile.PersonalSite.Delete();
                           
          /////***** HERE I GET THE ERROR*****/////////////                           
userProfileManager.RemoveUserProfile(fullUserName);
                           /////***** HERE I GET THE ERROR ******/////////////
                       }
                   }
                   catch (Exception ex)
                   {
                       HandleExceptionAsMinor(ex, lblErrorMessage, "deleting the user from SharePoint Profile", OverrideErrorMessage);
                   }
                   finally
                   {
                       SPSecurity.CatchAccessDeniedException = catchAccessDenied;
                       SPContext.Current.Site.AllowUnsafeUpdates = false;
                   }
                   try
                   {
                       //- Step 3: Delete User from SharePoint Site
                       site.RootWeb.SiteUsers.Remove(fullUserName);
                   }
                   catch (Exception ex)
                   {
                       HandleExceptionAsMinor(ex, lblErrorMessage, "deleting the user from Site Groups", OverrideErrorMessage);
                   }
               }
           });
       string connectionString = ConfigurationManager.ConnectionStrings[ConnStringName].ConnectionString;

       Grid.DataBind();
   }
   catch (Exception ex)
   {
       HandleExceptionAsMinor(ex, lblErrorMessage, "deleting the user", OverrideErrorMessage);
   }

推荐答案

要删除其他用户的用户个人资料,您必须具有PortalRight.ManageUserProfile权限。

http://msdn.microsoft.com/en -us / library / microsoft.office.server.userprofiles.userprofilemanager_methods.aspx [ ^ ]



您确定已经授予了正确的权利。 />


使用RunWithElevatedPrivileges时,将使用为运行代码的Web应用程序的应用程序池配置的帐户。如果您是从中央管理员运行它,那么帐户将为此设置。
"To remove a user profile for another user you must have PortalRight.ManageUserProfile permission."
http://msdn.microsoft.com/en-us/library/microsoft.office.server.userprofiles.userprofilemanager_methods.aspx[^]

Are you sure the proper right have been granted.

When using RunWithElevatedPrivileges the account configured for the Application Pool of the Web Application the code is running in is used. If you are running this from Central Admin it would the account setup of for that.


管理以修复代码



Manage to fix the code by

  SPSecurity.RunWithElevatedPrivileges(delegate()
           {
string AccountName="yourAccountNamehere";
                using (SPSite site = new SPSite(context.GetUrl()))
                {
                    SPServiceContext current = SPServiceContext.GetContext(site);
                    UserProfileManager upm = new UserProfileManager(current);

                    if (upm.UserExists(AccountName))
                    {

                        ProfileBase profile = upm.GetUserProfile(AccountName);
                        upm.RemoveProfile(profile);                    
                       
                    }
                    else
                    {
                        return;
                    }
                }
           });


这篇关于如何以编程方式删除SharePoint用户配置文件 - 使用FBA时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆