如何在Web API中删除用户? [英] How to delete user in web api?
问题描述
我已经使用个人用户身份验证创建了一个Web API.用户注册后,其信息存储在2个表中,其常规信息存储在"AspNetUsers"表中,其角色存储在"AspNetUserRoles"表中.现在的问题是,当我尝试从列表中删除任何用户时,它不起作用.这是删除操作:
I have created a web api with Individual User authentication. After user get registered his information is stored in 2 tables, his general information is stored in "AspNetUsers" table and his role is stored in "AspNetUserRoles" table. Now the problem is when I am trying to delete any user from the list, its not working.Here is the delete action:
[ResponseType(typeof(AspNetUser))]
[ActionName("EmployeeInfo")]
[DeflateCompression]
public IHttpActionResult DeleteEmployeeInformation(string Id)
{
AspNetUser AspNetUser = db.AspNetUsers.Find(Id);
if (AspNetUser == null)
{
return NotFound();
}
db.AspNetUsers.Remove(AspNetUser);
db.SaveChanges();
return Ok(AspNetUser);
}
这是ajax调用:
function DeleteEmployeeInformations(recordID) {
jQuery.support.cors = true;
var id = recordID
$.ajax({
url: 'http://localhost:61115/api/EmployeeInformations/EmployeeInfo/' + id,
type: 'DELETE',
contentType: "application/json;charset=utf-8",
success: function (data) {
GetAllEmployeeInformations();
},
error: function (x, y, z) {
alert(x + '\n' + y + '\n' + z);
}
});
}
在没有asp.net身份的情况下,此操作可以正常运行,但在使用asp.net身份时,此操作不起作用.所以我该怎么做? 谢谢.
This action is working fine without asp.net identity but its not working when I am using asp.net identity. So what should I do? Thanks.
推荐答案
应该使用ASP .NET Identity框架访问AspNetUser
和关联的表.
The AspNetUser
and associated tables are supposed to be accessed using the ASP .NET Identity framework.
这将涉及使用特定的类,以便对用户,角色等执行CRUD操作.
在这种情况下,它应该是UserManager
.
This would involve using specific classes in order to perform CRUD operation on User, Roles etc.
In this case it would be a UserManager
.
本文是对此的介绍:
In order to delete a User
, you would use the UserManager.Delete method. You would also need to check and remove any existing roles via the UserManager
too.
GitHub上有一个很好的示例,显示了我发现对入门有用的常见功能.
There is a very good example on GitHub which shows the common features that I found useful to get started.
这篇关于如何在Web API中删除用户?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!