简体   繁体   中英

How to delete user accounts in asp.net?

I have a Register page, I used the following Walkthrough: Creating a Web Site with Membership and User Login to make my webpage. The problem is the Registration page creates users, but I am clueless about how to delete user accounts from the Database where it gets stored.

The membership provider has a DeleteUser method.

http://msdn.microsoft.com/en-us/library/w6b0zxdw.aspx

The following works just as well:

Membership.DeleteUser("username");


If you want a SQL based solution:

http://web.archive.org/web/20130407080036/http://blogs.rawsoft.nl/remco/post/2009/02/05/How-to-Remove-users-from-the-ASPNet-membership-database.aspx

Here is a simpler way to delete a user using SQL.

USE ASPNet
GO

DECLARE @UserId uniqueidentifier
SET @UserId = 'THE GUID OF THE USER HERE'

DELETE FROM aspnet_Profile WHERE UserID = @UserId
DELETE FROM aspnet_UsersInRoles WHERE UserID = @UserId
DELETE FROM aspnet_PersonalizationPerUser WHERE UserID = @UserId
DELETE FROM dbo.aspnet_Membership WHERE UserID = @UserId
DELETE FROM aspnet_users WHERE UserID = @UserId

For the completeness' sake, here is a solution similar to Yasser's, however, with using the UserName instead of the GUID as the OP has asked:

DECLARE @UserId uniqueidentifier
SET @UserId = (SELECT TOP(1) UserID FROM aspnet_Users 
  WHERE UserName = 'THE USERNAME OF THE USER HERE')

DELETE FROM aspnet_Profile WHERE UserID = @UserId
DELETE FROM aspnet_UsersInRoles WHERE UserID = @UserId
DELETE FROM aspnet_PersonalizationPerUser WHERE UserID = @UserId
DELETE FROM dbo.aspnet_Membership WHERE UserID = @UserId
DELETE FROM aspnet_users WHERE UserID = @UserId

Note: Base SQL script taken from this blog by Tim Gaunt

On your project (Visual Studio) Top menu > Website > ASP.NET Configurations (Click on this)

It will open the configurations and then Security > Manage Users Do what you need there...

When creating a WebSite that is going to have a Membership manage users and roles, create an Admin/Support Web Page within your site that will be only visible available for Roles that can perform such operations as:

  • Delete User
  • Reset Password
  • Other Users management

It will come in handy when you have to support your End Users and the problems they are going to face.

Membership info from MSDN

Follow below steps :

  • open the folder location C:program file / microsoft visual studio 12/
  • search for developer Command prompt .
  • write the command : devenv /resetuserdata

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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