467,868 Members | 1,287 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,868 developers. It's quick & easy.

allow username update/change with SqlMembershipProvider

Hi,

what would be the simplest way to add a functionality so it is possible
to change the username in an asp.net 2.0 webapplication that uses
SqlMembershipProvider? By default, this providers does not allow to
update, but is there a quick way to enable it, without rewriting my
whole new MembershipProvider class?

thanks a lot,

ibiza

Jan 24 '06 #1
  • viewed: 4075
Share:
1 Reply
Hi Ibiza,

The AspNetSQLMembershipProvider does not include the ability to change usernames and if you look at the stored procedures that are installed when you run AspNet_RegSQL.Exe (or that are created automatically by the providers), there is no stored procedure available to call.

I wrote an Asp.Net Server control called the Membership Manager Control that includes this much needed functionality so I happen to have handy a script to create the "ChangeUserName" stored procedure:

Expand|Select|Wrap|Line Numbers
  1. create PROCEDURE [dbo].[Aspnet_Membership_ChangeUserName]
  2.     @ApplicationName                        nvarchar(256),
  3.     @UserId                                 uniqueidentifier,
  4.     @NewUserName                            nvarchar(256)
  5. AS
  6. BEGIN
  7.     DECLARE @ApplicationId uniqueidentifier
  8.     SELECT  @ApplicationId = NULL
  9.  
  10.     DECLARE @ExistingUserId uniqueidentifier
  11.     SELECT @ExistingUserId = NULL
  12.  
  13.     DECLARE @ErrorCode     int
  14.     SET @ErrorCode = 0
  15.  
  16.     DECLARE @TranStarted   bit
  17.     SET @TranStarted = 0
  18.  
  19.     IF( @@TRANCOUNT = 0 )
  20.     BEGIN
  21.         BEGIN TRANSACTION
  22.         SET @TranStarted = 1
  23.     END
  24.     ELSE
  25.         SET @TranStarted = 0
  26.  
  27.     EXEC dbo.aspnet_Applications_CreateApplication @ApplicationName, @ApplicationId OUTPUT
  28.  
  29.     IF( @@ERROR <> 0 )
  30.     BEGIN
  31.         SET @ErrorCode = -1
  32.         GOTO Cleanup
  33.     END
  34.  
  35.  
  36.     SELECT  @ExistingUserId = UserId FROM dbo.aspnet_Users WITH ( UPDLOCK, HOLDLOCK )
  37.         WHERE LOWER(@NewUserName) = LoweredUserName AND @ApplicationId = ApplicationId
  38.  
  39.     IF ( @ExistingUserID IS NULL)
  40.         BEGIN
  41.             /* userName is available */
  42.             UPDATE dbo.aspnet_Users SET UserName = @NewUserName, LoweredUserName= LOWER(@NewUserName) WHERE userID=@UserID
  43.         END
  44.     ELSE
  45.         BEGIN
  46.             IF( @ExistingUserID = @UserId)
  47.                 BEGIN
  48.                     SET @ErrorCode = 1
  49.                     GOTO Cleanup
  50.                 END
  51.             ELSE
  52.                 BEGIN
  53.                     SET @ErrorCode = 2
  54.                     GOTO Cleanup
  55.  
  56.                 END
  57.         END
  58.  
  59.     IF( @@ERROR <> 0 )
  60.     BEGIN
  61.         SET @ErrorCode = -1
  62.         GOTO Cleanup
  63.     END
  64.  
  65.     IF( @TranStarted = 1 )
  66.     BEGIN
  67.         SET @TranStarted = 0
  68.         COMMIT TRANSACTION
  69.     END
  70.  
  71.     RETURN 0
  72.  
  73. Cleanup:
  74.  
  75.     IF( @TranStarted = 1 )
  76.     BEGIN
  77.         SET @TranStarted = 0
  78.         ROLLBACK TRANSACTION
  79.     END
  80.  
  81.     RETURN @ErrorCode
  82.  
  83. END
If you run that script, you will create a new stored procedure that you can call from your own code. You can also check out my blog at mishler.net that has other good material on the subject of Asp.Net membership and in particular, the capabilities and limitations of the AspNetSqlMembershipProvider.

Regards,

Brian Mishler
http://www.pocketforms.net
May 4 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by red | last post: by
2 posts views Thread by Kent | last post: by
18 posts views Thread by =?Utf-8?B?VG9t?= | last post: by
4 posts views Thread by VB Programmer | last post: by
2 posts views Thread by William Gill | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.