在 webpart 代码中模拟不同的用户
我使用共享点列表作为数据库。 我想以某种方式在 Web 部件代码中模拟不同的用户 作为该用户,我将拥有对该列表的写入和编辑权限。
我的目标是只能通过 webpart 代码获得完全权限。
我正在使用 MOSS 2007。
I use the sharepoint lists as a database.
I want to somehow impersonate as different user inside the webpart code
and than as this user I will have both write and edit permission to the list.
My goal is to be able to have full premission only through the webpart code.
I am using MOSS 2007.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
SPSecurity.RunWithElevatedPrivilieges() 将以系统帐户(即应用程序池运行的帐户)的身份执行您的代码,这可能是也可能不是您想要执行的操作。例如,如果您有一个附加到列表的工作流程,该工作流程应该在将新项目添加到列表时触发,但如果您在系统帐户的凭据下插入新的列表项目,则该工作流程不会触发(这是一个安全修复程序)在 SharePoint 2007 SP 1) 中引入。在这种情况下,您将必须在对列表具有正确权限的不同帐户下执行插入操作。
您可以使用以下代码获取任何用户的 UserToken:
将“domain\username”替换为正确的 Windows 帐户名。然后,您可以将此用户令牌传递给 SPSite 对象构造函数的重载之一,以在该用户的凭据下执行代码,如下所示:
希望这会有所帮助。
SPSecurity.RunWithElevatedPrivilieges() will execute your code as the system account, i.e. the account under which the application pool runs, which might or might not be what you want to do. For example, if you have a workflow attached to the list which is supposed to trigger when new items are added to the list, it will not fire if you insert a new list item under the credentials of the system account (this was a security fix introduced in SharePoint 2007 SP 1). In that case you will have to perform the insert operation under a different account that has the correct permissions on the list.
You can get the UserToken for any user using the following code:
Replace the "domain\username" with the correct windows account name. Then you can pass this user token to one of the overloads of the SPSite object constructor to execute the code under this user's credentials like so:
Hope this helps.
您正在寻找SPSecurity.RunWithElevatedPrivileges 方法。
You are looking for SPSecurity.RunWithElevatedPrivileges Method.