简体   繁体   English

在 MVC Web 应用程序中删除 ASP.NET 标识的问题

[英]Issue deleting ASP.NET Identity in MVC Web Application

So after looking around for guides and tutorials of how can I delete ASP Users, I found the following code to be pretty neat:因此,在查找有关如何删除 ASP 用户的指南和教程后,我发现以下代码非常简洁:

 [HttpPost]
    [ValidateAntiForgeryToken]
    public async Task<ActionResult> DeleteConfirmed(string id)
    {
        if (ModelState.IsValid)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }

            var user = await UserManager.FindByIdAsync(id);
            var logins = user.Logins;
            var rolesForUser = await UserManager.GetRolesAsync(id);

            using (var transaction = context.Database.BeginTransaction())
            {
                foreach (var login in logins.ToList())
                {
                    await UserManager.RemoveLoginAsync(login.UserId, new UserLoginInfo(login.LoginProvider, login.ProviderKey));
                }

                if (rolesForUser.Count() > 0)
                {
                    foreach (var item in rolesForUser.ToList())
                    {
                        // item should be the name of the role
                        var result = await UserManager.RemoveFromRoleAsync(user.Id, item);
                    }
                }

                await UserManager.DeleteAsync(user);
                transaction.Commit();
            }

            return RedirectToAction("Index");
        }
        else
        {
            return View();
        }
    }

My view looks something like this:我的观点是这样的: 在此处输入图片说明

<td>
    @Html.ActionLink("Edit", "Edit", new { id = user.UserId }) |
    @Html.ActionLink("Delete", "DeleteConfirmed", new { id = user.UserId })
</td>

After clicking "Delete" here, in theory, it should have called the DeleteConfirmed method from the controller called "ManageUsersController".在此处单击“删除”后,理论上它应该从名为“ManageUsersController”的控制器调用了 DeleteConfirmed 方法。 However, it returns this error:但是,它返回此错误:

Server Error in '/' Application. “/”应用程序中的服务器错误。

The resource cannot be found.无法找到该资源。

Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable.描述:HTTP 404。您要查找的资源(或其依赖项之一)可能已被删除、更改名称或暂时不可用。 Please review the following URL and make sure that it is spelled correctly.请检查以下 URL 并确保其拼写正确。

Requested URL: /ManageUsers/DeleteConfirmed/29ad177f-0285-43d2-b065-109876f270b9请求的 URL:/ManageUsers/DeleteConfirmed/29ad177f-0285-43d2-b065-109876f270b9

What might be going wrong here?这里可能出了什么问题? Is there another way that I should write the method in the controller?还有另一种方法可以在控制器中编写方法吗? Thank you in advance先感谢您

This answer is based on the default codes that .NET scaffold for us.这个答案基于 .NET 为我们搭建的默认代码。

  • You're generating the Delete link using an extension of ActionLink which needs link text as the first parameter and action name as the second one .您正在使用ActionLink的扩展生成Delete链接,该扩展需要链接文本作为第一个参数,操作名称作为第二个参数 Your DeleteConfirmed action is a POST method;您的DeleteConfirmed操作是一个POST方法; you can't generate a link to POST, .NET sees GET methods for links.您无法生成 POST 链接,.NET 会看到链接的 GET 方法。 So:所以:
<td>
  @Html.ActionLink("Edit", "Edit", new { id = user.UserId }) |
  @Html.ActionLink("Delete", "Delete", new { id = user.UserId })
</td>
  • Make sure you have another method called Delete which is a GET one, in your controller.确保您的控制器中有另一个名为Delete方法,它是一个GET方法。

  • Add another attribute to DeleteConfirmed :DeleteConfirmed添加另一个属性:

[ActionName("Delete")]
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<ActionResult> DeleteConfirmed(string id)
{ ... }
  • If you need to delete the user when you click the Delete , you should use a POST form including the user id and anti-forgery token as hidden inputs and a submit button instead of link.如果您在单击Delete时需要删除用户,则应使用包含用户 ID 和防伪令牌作为隐藏输入和提交按钮而不是链接的 POST 表单。 But it's a best practice to show the user what they're deleting.但最好的做法是向用户展示他们正在删除的内容。 That's why .NET generated two related actions for delete;这就是 .NET 生成两个相关删除操作的原因; Delete (to review) and DeleteConfirmed (to actually delete). Delete (审查)和DeleteConfirmed (实际删除)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM