Service DotNetNuke服务API授权抛出未经授权的代码
我很难弄清楚为什么我从服务框架中获得了Service DotNetNuke服务API授权抛出未经授权的代码,service,authorization,dotnetnuke,dotnetnuke-module,Service,Authorization,Dotnetnuke,Dotnetnuke Module,我很难弄清楚为什么我从服务框架中获得了401未经授权的状态。目前,我有它的设置,允许每个人做他们喜欢的,但这是因为当我试图启用授权,我得到401错误代码 //[SupportedModules("Boards")] //[DnnModuleAuthorize(AccessLevel = SecurityAccessLevel.View)] [AllowAnonymous] public class BoardsServiceController : DnnApiController { ...
401未经授权的状态。目前,我有它的设置,允许每个人做他们喜欢的,但这是因为当我试图启用授权,我得到401错误代码
//[SupportedModules("Boards")]
//[DnnModuleAuthorize(AccessLevel = SecurityAccessLevel.View)]
[AllowAnonymous]
public class BoardsServiceController : DnnApiController
{ ... }
奇怪的是,我有另一个模块,它非常乐意使用DnnModuleAuthorize
[SupportedModules("Assignments")]
[DnnModuleAuthorize(AccessLevel = SecurityAccessLevel.View)]
public class AsgnsServiceController : DnnApiController
{ ... }
在这两种情况下,我都进行了检查,以确保用户具有查看模块所在页面的权限
我交叉引用了这两个项目,一切似乎都很到位。然而,其中一个工作正常,另一个返回401
有什么建议吗
更新
对于作业模块,我主要使用ajax请求的jQuery
风格,因为我没有时间修改模块。因此,典型的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
});
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);
};
至于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函数本身如下所示:
$.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
});
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
这提供了什么线索吗
更新
现在,在说上述所有内容时,如果你看一下原始版本,就会注意到每个函数都附带了[DnnAuthorize]
注释
在模块修订期间,我删除了[DnnAuthorize]
注释的所有实例,并将其替换为服务类本身的两个实例
当我在服务类本身上添加[DnnAuthorize]
作为注释时,事情按预期工作。那么为什么[SupportedModules(“Boards”)]
和[DNNModuleAuthorization(AccessLevel=SecurityAccessLevel.View)]
组合不起作用呢 我不确定,但使用WebAPI时,您必须注册服务框架防伪内容
ServicesFramework.Instance.RequestAjaxAntiForgerySupport();
这是要求API与特定模块一起工作的一部分。您能展示调用该服务的代码吗?我希望我的最新更新能够帮助您了解我忽略的内容。我已在OnInit()中注册了此代码。
以及其他一些脚本。但我认为它只与[ValidateAntiForgeryToken]
注释一起使用,以双重检查请求的有效性。尤其是POST-requests.can-be-mine要简单得多,因此可能复杂性隐藏了一些东西,您是否正在使用jquery-servcie框架加载var-var sf=$.ServicesFramework(moduleid);是的,它在ServiceCaller
中定义为this.services=$.dnnSF(moduleId)实际上,您知道清单文件中的
或
是否用于匹配[SupportedModules(“?”)
注释吗?还是完全是别的原因?