【问题标题】:ASPX Page will only populate details from a specific AJAX call if user is in a specific Role如果用户处于特定角色,ASPX 页面将仅填充来自特定 AJAX 调用的详细信息
【发布时间】: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 成功。

【问题讨论】:

    标签: jquery asp.net webforms


    【解决方案1】:

    这实际上与身份验证/授权无关。当以前处于 CCManager 角色(因此也处于 CCOperator 角色)的用户从该角色中删除时,它与数据库中的陈旧数据有关。我需要修改Operator.GetAllOperatorAssignedAndQualifiedCategories() 方法,因此如果以前是CCOperator 的用户留下了数据,它不会尝试将该数据插入到该方法返回的List&lt;Operator&gt; 中,而无需先检查用户是否还在在 CCOperator 角色中。否则,它会尝试更新 List&lt;Operator&gt; 中不存在于 List&lt;Operator&gt; 中的对象的属性。

    【讨论】:

    • 我刚想说,Operator.GetAllOperatorAssignedAndQualifiedCategories() 中有些问题,可能不在 auth 中。尽可能标记为已回答。 stackoverflow.blog/2009/01/06/accept-your-own-answers
    • 开发环境中没有发生。我不得不追溯在生产环境中发生的事情(我无法调试),然后最终在开发环境中发生了同样的错误。
    猜你喜欢
    • 2014-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多