SPRole.AddUser方法出错

     SPWeb mysite = SPControl.GetContextWeb( Context );
    bool isExistUserGroup = false;
    foreach(SPRole tmpRole in  mysite.Roles )
    {
     if( tmpRole.Name == "普通员工组" )
     {
      isExistUserGroup = true;
      break;
     }
    }

    SPRole admins;
    if( isExistUserGroup )
    {
     admins = mysite.Roles["普通员工组"];
    }
    else
    {
     admins = mysite.Roles["读者"];
    }
    
    admins.AddUser( @"domainname/" + account , "" , cn , "员工" );

用上面的语句,结果在执行到最后一句的时候出现如下的错误提示:


Microsoft.SharePoint.SPException: 此网页的安全性验证无效并且可能损坏。请单击 Web 浏览器中的“后退”,刷新网页,再重试操作。 ---> System.Runtime.InteropServices.COMException (0x8102006D): 此网页的安全性验证无效并且可能损坏。请单击 Web 浏览器中的“后退”,刷新网页,再重试操作。
   at Microsoft.SharePoint.Library.SPRequestInternalClass.EnsureUserAndAddToGroup(String bstrUrl, String bstrLogin, String bstrEmail, String bstrTitle, String bstrNotes, Int32 lGroupID, Boolean bGlobal, Boolean bSendEmail)
   at Microsoft.SharePoint.Library.a.a(String A_0, String A_1, String A_2, String A_3, String A_4, Int32 A_5, Boolean A_6, Boolean A_7)
   --- 内部异常堆栈跟踪的结尾 ---
   at Microsoft.SharePoint.Library.a.a(String A_0, String A_1, String A_2, String A_3, String A_4, Int32 A_5, Boolean A_6, Boolean A_7)
   at Microsoft.SharePoint.SPGroup.AddUser(String loginName, String email, String name, String notes)
   at Microsoft.SharePoint.SPRole.AddUser(String loginName, String email, String name, String notes)
   at ManagerUser.TopAdmin.ActiveUserToSite(String account, String cn)

用了身份模拟也不行,不知道到底是哪里出了问题


分析1:  sharepoint sdk中关于SPRole.AddUser方法的定义

AddUser(String, String, String, String) Method

The AddUser method of the SPRole class adds the user specified by user name, e-mail address, display name, and notes to the site group

[C#]

public void AddUser(
   string loginName,
   string email,
   string name,
   string notes
);

Parameters

loginName   A string that contains the user name (DOMAIN/User_Alias).

email   A string that contains the e-mail address.

name   A string that contains the display name of the user.

notes   A string that contains notes for the user.

Requirements

Platforms: Microsoft Windows Server 2003

Security: Code Access Security

从上面可以看出,代码调用没有问题。下面研究一下Code Access Security


分析二: SPRole.AddUser方法的Code Access Security

    因为SPRole.AddUser方法属于Microsoft.SharePoint名称空间,所以要确定在我的程序中是否有权限访问Microsoft.SharePoint.dll,因为我的程序是位于sharepoint网站上的应用程序, 访问本站点下的动态库应该没有问题,看了一下文件Microsoft.SharePoint.dll所在的文件夹的权限

  我在 Microsoft.SharePoint.dll文件所在的文件夹设置如下的权限:

  1. 将 machineName/ASPNET 加入到访问用户组,权限为可读  , 结果无效

  2. 将 domainName/NETSERVICE权限加入到访问用户组,权限为可读 , 结果无效

  3.  将domainName/Authenticated Users加入到访问用户组,权限为可读, 结果无效

 4. 特别的,将当前访问用户的帐号加入到访问用户组,权限为可读,结果无效

以上可以看出,应该不是代码访问安全的问题,从错误信息分析,是在执行Microsoft.SharePoint.Library.SPRequestInternalClass.EnsureUserAndAddToGroup(String bstrUrl, String bstrLogin, String bstrEmail, String bstrTitle, String bstrNotes, Int32 lGroupID, Boolean bGlobal, Boolean bSendEmail)这个方法的时候出现了此网页的安全性验证无效并且可能损坏的问题,而这种此网页的安全性验证无效并且可能损坏的问题我也遇见过,那是在我重新编译了一个web part或者很久没有使用网页的时候出现的错误提示,只要刷新页面或者重新进入就可以了。

    另外,还有一个com错误,System.Runtime.InteropServices.COMException (0x8102006D):

    查找了一下COMException (0x8102006D)的错误原因,大部分是Access Denied的错误,其中有一个解决方案是加上一个AllowUnsafeUpdates属性并且设置为true


经过分析,终于解决,解决方案如下:

将    

mysite.AllowUnsafeUpdates = true;

加入到

    admins.AddUser( @"domainname/" + account , "" , cn , "员工" );

的前面。


相关经验1

在很多操作sharepoint站点资源的地方需要设置一个属性AllowUnsafeUpdates 属性,并且设置为true,下列类具有AllowUnsafeUpdates 属性:

◆SPGlobalAdmin

◆SPSite

◆  SPWeb


相关经验2

SPRole.AddUser方法的Code Access Security 中有如下的描述:

If your code tries to write data to a database on an HTTP Get or if the code needs to enable Web Part-to-Web Part connections, your code might also need two other permissions specific to Microsoft Office SharePoint Products and Technologies. They are: SharePointPermission.UnsafeSaveOnGet and WebPartPermission.Connections.


综上所述,该问题还是属于Code Access Security 的问题


添加用户时的错误

问题描述

本来在一个portal网站的用户列表中不存在testuser,可是我用SPRole.AddUser()将一个用户添加到站点的时候,出现如下的错误提示:

ManagerUser.TopAdmin.ActiveUserToSite产生错误:Microsoft.SharePoint.SPException: 无法添加用户,因为同名用户已存在。 ---> System.Runtime.InteropServices.COMException (0x 8102004A ): 无法添加用户,因为同名用户已存在。

   at Microsoft.SharePoint.Library.SPRequestInternalClass.EnsureUserAndAddToGroup(String bstrUrl, String bstrLogin, String bstrEmail, String bstrTitle, String bstrNotes, Int32 lGroupID, Boolean bGlobal, Boolean bSendEmail)

   at Microsoft.SharePoint.Library.a.a(String A_0, String A_1, String A_2, String A_3, String A_4, Int 32 A _5, Boolean A_6, Boolean A_7)

   --- 内部异常堆栈跟踪的结尾 ---

   at Microsoft.SharePoint.Library.a.a(String A_0, String A_1, String A_2, String A_3, String A_4, Int 32 A _5, Boolean A_6, Boolean A_7)

   at Microsoft.SharePoint.SPGroup.AddUser(String loginName, String email, String name, String notes)

   at Microsoft.SharePoint.SPRole.AddUser(String loginName, String email, String name, String notes)

   at K2SystemFramework.SPS.MySPWeb.AddUserToGroup(String account, String email, String user_title, String user_notes, String group_name)

到底是什么原因哪?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值