473,487 Members | 2,622 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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
1 4291
bm0061
1 New Member
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
7074
by: red | last post by:
I know this is probably a faq but it is hard to search for this exact problem. When I put this on a php page: <? $link = mysql_connect("localhost", "cardini", "password") or die("Could not...
2
2276
by: Rod | last post by:
I have a requirement in which I need to allow the user to log into our ASP.NET application, but also be able to change their Windows password, if it is expired. I had thought that Windows...
1
2235
by: Hardy Wang | last post by:
Hi, I have my own user table with definition like UserID int not null primary key, Username varchar(50) not null, Password varchar(50) not null, Firstname varchar(50) not null, Lastname...
2
3391
by: Kent | last post by:
I cannot find any methods to change the username of a MembershipUser. I know I could just go hit the database and write my own method but this seems like a feature that should be included.
2
2543
by: daz_oldham | last post by:
Hi All Bit of an odd one - very basically I created an app using the SqlMembershipProvider in VS.NET 2005, and I did it on a MS SQL 2005 server. However, I then find out that for other...
0
2279
by: Mwob | last post by:
Hi all, I'm about to start creating a custom membership provider. Its for a website that already has a table of users in a single table, so I need to create a custom MP to talk to the data in...
18
4740
by: =?Utf-8?B?VG9t?= | last post by:
is it possible to add a bunch of users to group and only allow group to access the web page or do I need to add each user to the web.config file? Or is there another way to do this? I just took...
4
4298
by: VB Programmer | last post by:
I have an ASP.NET 2.0 site i'm creating in VS.NET. I left it a few days ago, after I had implemented membership, worked on some db tables, etc... Today I go to open the Data Connection to the...
2
2543
by: William Gill | last post by:
I have a db of people in a group and want to allow individual group members a way to view and update their own contact information, but not anyone else's. I assume the safest/smartest way is via...
0
7106
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6967
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7137
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
5442
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4874
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3076
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3071
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
600
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
267
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.