DotNetNuke Service API 授权抛出 401 未经授权的代码



我很难弄清楚为什么我从服务框架中获得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
});

至于电路板模块,由于敲除实现,代码结构略有不同。有一个专用的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 模块代码库,您会注意到每个函数都附加[DnnAuthorize]注释。

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

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

我不确定,但使用 WebAPI 时,您必须注册服务框架防伪的东西

 ServicesFramework.Instance.RequestAjaxAntiForgerySupport();

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

最新更新