【问题标题】:DotNetNuke Service API Authorization throwing 401 Unauthorized codeDotNetNuke Service API Authorization throwing 401 Unauthorized code
【发布时间】:2015-01-27 11:01:13
【问题描述】:

我很难弄清楚为什么我会从服务框架获得401 Unauthorized 状态。目前我已将其设置为允许每个人随心所欲,但这是因为当我尝试启用授权时,我收到 401 错误代码。

//[SupportedModules("Boards")]
//[DnnModuleAuthorize(AccessLevel = SecurityAccessLevel.View)]
[AllowAnonymous]
public class BoardsServiceController : DnnApiController
{ ... }

奇怪的是我有另一个模块非常乐意使用 DnnModuleAuthorize

[SupportedModules("Assignments")]
[DnnModuleAuthorize(AccessLevel = SecurityAccessLevel.View)]
public class AsgnsServiceController : DnnApiController
{ ... }

在这两种情况下,我都已检查以确保用户有权查看模块所在的页面。

我已经交叉引用了这两个项目,一切似乎都是正确的。然而,一个工作正常,另一个返回 401。

有什么建议吗?

更新

对于作业模块,我主要使用jQuery ajax 请求样式,只是因为我没有时间修改模块。所以一个典型的GET 请求看起来像这样:

$.ajax({
    type: "GET",
    url: sf.getServiceRoot( "Assignments" ) + "AsgnsService/GetAssignments",
    data: data,
    beforeSend: sf.setModuleHeaders
}).done( function ( items ) {
    //removed for brevity
}).fail( function ( xhr, result, status ) {
    //removed for brevity
});

至于 Boards 模块,由于淘汰赛的实现,代码结构略有不同。有一个专用的ServiceCaller,但它都归结为对服务器的同一个 ajax 调用,除了上面定义的完整的 ajax 调用看起来更整洁。

var that = this;

that.serviceCaller = new dnn.boards.ServiceCaller($, this.moduleId, 'BoardsService');

var success = function (model) {
    if (typeof model !== "undefined" && model != null) {
        viewModel = new boardViewModel(model.colLists);

        ko.bindingHandlers.sortable.beforeMove = viewModel.verifyAssignments;
        ko.bindingHandlers.sortable.afterMove = viewModel.updateLastAction;

        // normally, we apply moduleScope as a second parameter
        ko.applyBindings(viewModel, settings.moduleScope);
    }

    //console.log('success', model);
};

var failure = function (response, status) {
    console.log('request failure: ' + status);
};

var params = {
    BoardId: this.boardId
};

that.serviceCaller.get('GetBoardLists', params, success, failure);

ServiceCaller ajax 函数本身看起来像这样:

function (httpMethod, method, params, success, failure, synchronous) {
    var options = {
        url: that.getRoot() + method,
        beforeSend: that.services.setModuleHeaders,
        type: httpMethod,
        async: synchronous == false,
        success: function (d) {
            if (typeof (success) != 'undefined') {
                success(d || {});
            }
        },
        error: function (xhr, textStatus, errorThrown) {
            if (typeof (failure) != 'undefined') {
                var message = undefined;

                if (xhr.getResponseHeader('Content-Type').indexOf('application/json') == 0) {
                    try {
                        message = $.parseJSON(xhr.responseText).Message;
                    } catch (e) {
                    }
                }

                failure(xhr, message || errorThrown);
            }
        }
    };

    if (httpMethod == 'GET') {
        options.data = params;
    } else {
        options.contentType = 'application/json; charset=utf-8';
        options.data = ko.toJSON(params);
        options.dataType = 'json';
    }

    $.ajax(options);
};

这将是来自两个不同模块的两个 GET 请求,当我启用相同的注释时,一个很高兴,另一个抛出状态 401。

这有什么线索吗?

更新

现在说到以上所有内容,如果你看看原来的Boards module code base,你会注意到每个函数都附有[DnnAuthorize]注解。

在模块修订期间,我删除了 [DnnAuthorize] 注释的所有实例,并在服务类本身上用我自己的两个实例替换它。

当我将[DnnAuthorize] 添加为服务类本身的注释时,事情会按预期工作。那么为什么[SupportedModules("Boards")][DnnModuleAuthorize(AccessLevel = SecurityAccessLevel.View)] 组合没有呢!?

【问题讨论】:

  • 你能展示你调用服务的代码吗?
  • 我希望我的最新更新有助于阐明我所忽略的内容。

标签: service authorization dotnetnuke dotnetnuke-module


【解决方案1】:

我不确定,但使用 WebAPI 你必须注册服务框架防伪材料

 ServicesFramework.Instance.RequestAjaxAntiForgerySupport();

这是要求 API 使用特定模块的一部分。

【讨论】:

  • 我在OnInit() 中注册了这个以及其他一些脚本。但我认为它只能与 [ValidateAntiForgeryToken] 注释一起使用来仔细检查请求的有效性。特别是 POST 请求。
  • 可能是 - 我的要简单得多,所以也许复杂性隐藏了一些东西,你用 jquery servcie 框架加载一个 var - var sf = $.ServicesFramework(moduleid);
  • 是的,它在ServiceCaller 中定义为this.services = $.dnnSF(moduleId);
  • 其实你知道manifest文件中的<packageName>或者<moduleName>是用来匹配[SupportedModules("?")]注解的吗?还是完全是另外一回事?
猜你喜欢
  • 2020-08-15
  • 1970-01-01
  • 2019-06-18
  • 2019-07-21
  • 1970-01-01
  • 1970-01-01
  • 2018-02-09
  • 1970-01-01
  • 2017-06-25
相关资源
最近更新 更多