解决SharePoint Online Oauth 认证非full control用户不能登录的问题

来源:互联网 发布:后台数据怎么传到前台 编辑:程序博客网 时间:2024/06/06 08:29

背景(Background)

SharePoint Online 提供一个不错的授权和认证机制, 它是基于Oauth2.0协议的。我们可以利用它来为第三方站点授权。这样我自己的网站就可以利用SharePoint的用户名密码来登录,而不用自己维护这些信息。http://msdn.microsoft.com/en-us/library/jj687470.aspx。


问题(Problem)

在利用Oauth实现认证的时候,利用上面例子中的代码,我们发现SharePoint只对管理员账号授权。也就是说尽管我只想读取sharepoint上的信息,可是他仍然要求要full control的权限。作为member拥有contributor的权限也得不到认证。

Using sharepoint Oauth example, user can not be authorized if he/she is a non-full control user.

原理

这个问题网上基本上没有资料,本人是花了一整天的时间反编译微软的代码找到的方法。下面是做认证时跳转到的OAuthAuthorizePage.aspx页面OnLoad 方法:

I spent a whole day to reflect Micorsoft's code and found following clues. Below is OnLoad method of the redirect OAuthAuthorizePage.aspx page.

 protected override void OnLoad(EventArgs e)    {        SPUtility.EnsureAuthentication();        this.ValidateRequest();        SPSecurity.SuppressAccessDeniedRedirectInScope scope = new SPSecurity.SuppressAccessDeniedRedirectInScope();        try        {            if (!base.get_Web().DoesUserHavePermissions(this.get_RightsRequired()))            {                throw new UnauthorizedAccessException();            }            base.OnLoad(e);        }        catch (UnauthorizedAccessException)        {            this.RedirectWithError("access_denied", null);        }        finally        {            if (scope != null)            {                scope.Dispose();            }        }    }


关键点就在RightsRequired这个属性,而这个属性是这样写的(The key point is RightsRequired, here is the property):

protected override SPBasePermissions RightsRequired{    get    {        if (string.IsNullOrEmpty(base.Request.QueryString["scope"]))        {            return (base.get_RightsRequired() | (SPBasePermissions.EmptyMask | SPBasePermissions.ViewFormPages));        }        return (base.get_RightsRequired() | SPBasePermissions.ManagePermissions);    }} 

也就是说,如果你如例子中这样写下面这句(As you see, it needs ManagePermissions if you simpliy copy the below line from the reference article)

Response.Redirect(TokenHelper.GetAuthorizationUrl(sharePointSiteUrl.ToString(), "Web.Write"));


只要scope参数不为空(“”),那么就需要ManagePermissions(管理权限)。


解决方法

所以说在redirect的时候我们只需要写成下面这样(scope设置为空),只需要有View权限的用户就可以通过认证了。(The issue can be resovled once you change the scope parameter toempty.)

Response.Redirect(TokenHelper.GetAuthorizationUrl(sharePointSiteUrl.ToString(), ""));



原创粉丝点击