【问题标题】:How to ban deleting user identity from link如何禁止从链接中删除用户身份
【发布时间】:2021-06-20 23:44:01
【问题描述】:

当有人试图删除将链接更改为“localhost:0000/users/delete/3”的用户时,我该怎么做?我只想在单击删除图标后能够删除用户。 这是删除图标的代码:

<a asp-action="Delete" asp-route-id="@item.ID" onclick="return confirm('Are you sure you want to remove the user?')"><i class="fa fa-trash-o"></i></a>

这是我的删除操作:

public async Task<IActionResult> Delete(int id)
    {
        var usersModel = await _context.Users.FindAsync(id);
        if (usersModel != null)
        {
            _context.Users.Remove(usersModel);
            await _context.SaveChangesAsync();
        }
        return RedirectToAction("Index", "Users");
    }

【问题讨论】:

标签: asp.net asp.net-mvc asp.net-core hyperlink identity


【解决方案1】:

如何禁止从链接中删除用户身份

当有人试图删除将链接更改为“localhost:0000/users/delete/3”的用户时,我该怎么做?我只是希望能够在单击删除图标后删除用户。

首先,请注意,如果用户点击&lt;a&gt;标签超链接或在浏览器地址栏中输入http(s)://localhost:xxx/users/delete/3,这将有助于向您的delete端点发出HTTP GET请求以删除特定用户记录。

如果您想阻止浏览器客户端用户通过地址栏中的 URL 删除用户帐户,并启用通过单击删除图标来删除用户。

要达到您的上述要求,您可以尝试以下解决方法。

应用 [HttpPost] 并确定支持 HTTP POST 方法的 delete 操作

[HttpPost]
public async Task<IActionResult> Delete(int id)
{
    //...

在 MVC 视图页面上

<form id="delform" method="post" asp-action="Delete" asp-route-id="@item.ID">
    <a asp-action="Delete" asp-route-id="@item.ID" onclick="DelFunc();"><i class="fa fa-trash-o"></i></a>
</form>

点击删除图标时触发表单提交

function DelFunc() {
    event.preventDefault();

    if (confirm('Are you sure you want to remove the user?')) {
        $("#delform").submit();
    }
}

【讨论】:

  • 谢谢你救了我的命!!!我的项目真的需要它!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-02-28
  • 2019-03-04
  • 1970-01-01
  • 2016-08-09
  • 2011-05-01
  • 1970-01-01
相关资源
最近更新 更多