Delete user programmatically in dotnetnuke
I'm using DNN 5.x;I want to delete an user from C# code. The code I tested is:
UserInfo objUserInfo = DotNetNuke.Entities.Users.UserController.GetUser(0, userid, false);
UserController.DeleteUser(ref objUserInf开发者_JAVA技巧o, false, false);
Howver, the correponding record remains in tables: aspnet_users, aspnet_membership, users. What should I use to get full deletion?
Use UserController.RemoveUser(objUserInfo)
after the DeleteUser is called. So your code should look like
UserInfo objUserInfo = DotNetNuke.Entities.Users.UserController.GetUser(0, userid, false);
UserController.DeleteUser(ref objUserInfo, false, false);
UserController.RemoveUser(objUserInfo);
I don't believe they've implemented complete deletion in DNN 5.x yet (in DNN 4, they had full deletion, then moved to soft deletion in DNN 5). You may want to check the source code for that DeleteUser method in DNN 4.9.x or 4.8.x and see what it's doing (probably just a difference in what the stored procedure is doing). You should then be able to copy that stored procedure implementation and call it directly.
I use the following SQL statements to delete a user. You can easily integrate that into .net code...
DECLARE @NumTablesDeletedFrom int;
DECLARE @userid INT;
EXECUTE [aspnet_Users_DeleteUser] @ApplicationName = 'DotNetNuke', @UserName = 'testUser', @TablesToDeleteFrom = 15, @NumTablesDeletedFrom = 1;
SELECT @userid = UserID from users where Lower(username) = 'testUser';
EXECUTE [DeleteUser] @userid;
I hacked DeleteUserPortal sproc to hard delete users in DNN5.02.03
ALTER PROCEDURE [dbo].[DeleteUserPortal]
@UserID int,
@PortalID int
AS
IF @PortalID IS NULL
BEGIN
UPDATE dbo.Users
SET
IsDeleted = 1
WHERE UserId = @UserID
END
ELSE
BEGIN
UPDATE dbo.UserPortals
SET
IsDeleted = 1
WHERE UserId = @UserID
AND PortalId = @PortalID
END
-- Custom modification to delete user records from db instead of soft delete
-- Code above is redundant
DECLARE @userName nvarchar(100)
DECLARE @aspnetUserId uniqueidentifier
SELECT @userName = username FROM dbo.Users WHERE UserId = @UserID
SELECT @aspnetUserId = UserId FROM dbo.aspnet_Users WHERE UserName = @userName
DELETE FROM Users WHERE UserId = @UserID
DELETE FROM aspnet_Membership WHERE UserId = @aspnetUserId
DELETE FROM aspnet_Users WHERE UserId = @aspnetUserId
精彩评论