【发布时间】:2018-02-10 21:02:11
【问题描述】:
我有一个用作管理器仪表板的 ASPX 页面。该网站使用 Windows 身份验证和表单身份验证。还有一个自定义角色提供程序和一个站点地图,用于指定用户必须是其成员才能评估每个页面的角色。
我在 ASPX 页面上有一个 AJAX 调用,它在后面的代码中调用一个 WebMethod,它返回我需要填充条形图和一些饼图的 JSON 数据。管理员角色的用户可以查看图表。
我在 ASPX 页面上有另一个 AJAX 调用,它在后面的代码中调用了不同的 WebMethod,并且它仅在用户处于 CCManager 角色时才返回成功响应。然后它会填充允许用户查看和修改一些数据的 div 的内容。
当用户不是 CCManager 角色而是管理员角色时,我无法确定为什么这个 AJAX 调用不起作用。
从 IE 中的 F12 开发人员工具中,如果用户处于 Admin 角色但不是 CCManager 角色,则此 AJAX 调用将返回 HTTP 500:
responseText: "{"Message":"Object reference not set to an instance of an object.","StackTrace":" at Operator.GetAllOperatorAssignedAndQualifiedCategories()","ExceptionType":"System.NullReferenceException"}"
status: 500
statusText: "Internal Server Error"
这里是 AJAX 调用:
function getOperatorDetails()
{
$.ajax({
type: "GET",
url: "Dashboard.aspx/GetOperatorDetails",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
operatorDetails = response.d;
},
error: function (error) {
console.log(error);
},
async: false
});
}
这是后面代码中的 WebMethod:
[WebMethod(EnableSession = true)]
[ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
public static List<Operator> GetOperatorDetails()
{
return Operator.GetAllOperatorAssignedAndQualifiedCategories();
}
经理仪表板位于主管目录中。在 Web.config 中,我允许角色 Admin 和 CCManager:
<configuration>
<location path="Supervisor">
<system.web>
<authorization>
<allow roles="Admin,CCManager" />
<deny users="*" />
</authorization>
</system.web>
</location>
...
在站点地图中,我指定了 CCManager 和 Admin 角色:
<?xml version="1.0" encoding="utf-8" ?>
<siteMap xmlns="http://schemas.microsoft.com/AspNet/SiteMap-File-1.0" >
<siteMapNode roles="*">
<siteMapNode title="Manager Dashboard" roles="CCManager,Admin" url="~/Supervisor/Dashboard.aspx" />
...
当另一个 AJAX 调用对管理员角色的用户正常工作时,我无法确定为什么这个特定的 AJAX 调用返回 HTTP 500。另一个适用于管理员用户的 AJAX 调用几乎是相同的。如果我遗漏了什么,这里供参考:
function getCategoryImageCounts()
{
$.ajax({
type: "GET",
url: "Dashboard.aspx/GetCategoryImageCounts",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
categoryImageCounts = response.d;
},
error: function (error) {
alert("Error getting Category Image Counts: " + error.d);
},
async: false
});
}
下面是上面的 AJAX 调用引用的代码中的 WebMethod:
[WebMethod(EnableSession = true)]
[ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
public static List<CategoryImageCount> GetCategoryImageCounts()
{
return CategoryImageCount.GetImageCountsForAllCategories();
}
CategoryImageCount 和 Operator 类都位于 App_Code > Models 文件夹中。我没有在这些类的静态方法中包含任何需要用户处于特定角色的东西,所以我完全不知道为什么两个 AJAX 调用对于 CCManager 角色的用户都是成功的,但对于管理员角色,调用 GetOperatorDetails 返回 HTTP 500 但调用 GetCategoryImageCounts 成功。
【问题讨论】: