【问题标题】:c# mvc - authorize ajax functionc# mvc - 授权ajax函数
【发布时间】:2018-05-15 06:24:46
【问题描述】:

我有一个代码从我的 FriendApi 调用“删除”操作,但我想授权它。我在 Api 中授权了删除操作,不是管理员的用户仍然可以“删除”数据,但是当我刷新页面时,数据仍然存在。我如何授权并确保如果某人不是管理员并且单击“删除”,他会被要求使用有权执行此操作的帐户登录。这是我的 Api 和我的索引页面,其中调用了 Api。

// DELETE: api/FriendApi/5
        [Authorize(Roles = "Admin")]
        [ResponseType(typeof(FriendModel))]
        public IHttpActionResult DeleteFriendModel(int id)
        {
            FriendModel friendModel = db.friends.Find(id);
            if (friendModel == null)
            {
                return NotFound();
            }

            db.friends.Remove(friendModel);
            db.SaveChanges();

            return Ok(friendModel);
        }

这是我的索引。所以我的主要目标是让它对“经理”角色可见,但只有“管理员”角色才能删除内容。

@model List<MVCLab5._1.Models.FriendModel>

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>

<table class="table table-bordered table-responsive table-hover" id="myTable">
    <thead>
        <tr>
            <td>Friend ID</td>
            <td>Name</td>
            <td>City</td>
            <td></td>
        </tr>
    </thead>
    <tbody>
        @{
            //int i = 0;

            foreach (var obj in Model)
            {

                <tr>
                    <td>@obj.FriendId</td>
                    <td>@obj.Name</td>
                    <td>@obj.Place</td>
                    <td>
                        @if (User.IsInRole("Admin") || User.IsInRole("Manager"))
                        {

                        <button data-friend-id="@obj.Id" class="btn btn-default js-delete">Delete</button>
                        }

                        @*@Html.ActionLink("Delete", "DeleteFriend", new { id = obj.Id }, new { @class = "btn btn-default" })*@
                @if (User.IsInRole("Admin") || User.IsInRole("Manager"))
                {
                        @Html.ActionLink("Edit", "EditFriend", new { id = obj.Id }, new { @class = "btn btn-default" })
                }
                    </td>


                </tr>
                //i++;

            }
        }
    </tbody>
</table>

@section scripts{
    <script>
        $(document).ready(function () {
            var table = $('#myTable').DataTable();
            $("#myTable .js-delete").on("click", function () {
                var button = $(this);
                $.ajax({
                    url: "/api/FriendApi/" + button.attr("data-friend-id"),
                    method: "DELETE",
                    success: function (result) {
                        console.log(result)
                        table.row(button.parents("tr")).remove().draw();
                    }
                });
            });
        });
    </script>
}

【问题讨论】:

  • 能否在DeleteFriendModel action 方法中设置断点并确保断点被命中?

标签: c# asp.net-mvc


【解决方案1】:

这取决于当前登录的用户。我不知道您使用的是哪种身份验证方法。

举个例子,您正在使用自定义 cookie 身份验证。在这种情况下,您在每个请求中都会收到一个唯一令牌(包装为 cookie 条目),并且使用此令牌您可以知道当前登录的用户,即您可以查看数据库以检查他有哪些权限,并据此决定授权与否。

【讨论】:

  • 是的,我知道。我的问题是如何授权 Ajax 函数。 “删除”按钮应该对经理和管理员可见,但只有管理员可以删除它。如果经理点击它,它应该将他重定向到登录页面
  • @AnthinoRusso 您的问题是在客户端还是服务器端?你不清楚哪一部分的实现思路?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-05-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多