1 Oct 2010

Delete a User from the ASP.NET membership database

Here's a sweet stored procedure to delete an ASP.NET user by only passing through a UserName, and not the 4 obscure arguments that the aspnet_Users_DeleteUser sproc requires.


CREATE PROCEDURE [dbo].[DeleteUser]
@UserName nvarchar(50)
AS
BEGIN

DECLARE @Error int;
DECLARE @UserGUID uniqueidentifier;


BEGIN TRAN

SELECT @UserGUID = UserID FROM aspnet_Users WHERE UserName = @UserName;
DELETE FROM aspnet_Profile WHERE UserID = @UserGUID;
SET @Error = @@ERROR; IF @Error <> 0 GOTO ErrorHandling;
DELETE FROM aspnet_UsersInRoles WHERE UserID = @UserGUID;
SET @Error = @@ERROR; IF @Error <> 0 GOTO ErrorHandling;
DELETE FROM dbo.aspnet_PersonalizationPerUser WHERE UserID = @UserGUID;
SET @Error = @@ERROR; IF @Error <> 0 GOTO ErrorHandling;
DELETE FROM aspnet_Membership WHERE UserID = @UserGUID;
SET @Error = @@ERROR; IF @Error <> 0 GOTO ErrorHandling;
DELETE FROM aspnet_users WHERE UserID = @UserGUID;
SET @Error = @@ERROR; IF @Error <> 0 GOTO ErrorHandling;

GOTO Final

ErrorHandling:

ROLLBACK TRAN
GOTO EndOfProc

Final:

COMMIT TRAN

EndOfProc:
SELECT @Error as Result
END

No comments:

Post a Comment

Comments are very welcome but are moderated to prevent spam.

If I helped you out today, you can buy me a beer below. Cheers!