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

How to ban deleting user identity from link

当有人试图删除将 link 更改为“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");
    }

How to ban deleting user identity from link

How can I make so when somebody tries to delete a user changing the link to "localhost:0000/users/delete/3"? I just want to be able to delete the user after clicking on a delete icon.

首先,请注意,如果用户单击 <a> 标签超链接或在浏览器地址栏中输入 http(s)://localhost:xxx/users/delete/3,这将有助于向您的 [=16] 发出 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();
    }
}