Script to create an ASP.NET Membership provider User
15,099
Solution 1
From MSDN:
declare @now datetime
set @now= GETDATE()
exec aspnet_Membership_CreateUser 'MyAppName','admin1','pass@word1', '','[email protected]','','',1,@now,@now,0,0,null
http://msdn.microsoft.com/en-us/library/gg252020(v=office.14).aspx
Solution 2
Try the following scripts:
Step 1. User Defined Function to encode password and salt
CREATE FUNCTION [dbo].[base64_encode] (@data VARBINARY(MAX)) RETURNS VARCHAR(MAX) WITH SCHEMABINDING,
RETURNS NULL ON NULL INPUT BEGIN RETURN
( SELECT [text()] = @data
FOR XML PATH('') ) END GO
Step 2. Stored procedure to create membership user
CREATE PROCEDURE [dbo].[CreateUser]
@UserName nvarchar(256)
, @ClearTextPassword nvarchar(128)
, @Email nvarchar(256)
, @pUserId uniqueidentifier
AS
BEGIN
DECLARE @ApplicationName nvarchar(256)
DECLARE @PasswordFormat int
DECLARE @UnencodedSalt uniqueidentifier
DECLARE @Password nvarchar(128)
DECLARE @PasswordSalt nvarchar(128)
DECLARE @Now DATETIME
DECLARE @UniqueEmail int
DECLARE @UserId uniqueidentifier
SET @ApplicationName = 'ApplicationName' --Find in aspnet_Applications.ApplicationName
SET @PasswordFormat = 1
SET @UnencodedSalt = NEWID()
SET @PasswordSalt = dbo.base64_encode(@UnencodedSalt)
SET @Password = dbo.base64_encode(HASHBYTES('SHA1',
CAST(@UnencodedSalt as varbinary(MAX))
+ CAST(@ClearTextPassword AS varbinary(MAX)) ))
SET @Now = getutcdate()
SET @UniqueEmail = 1
SET @UserId=@pUserId
BEGIN TRANSACTION
--DECLARE @UserId uniqueidentifier
EXECUTE [dbo].[aspnet_Membership_CreateUser]
@ApplicationName
,@UserName
,@Password
,@PasswordSalt
,@Email
,NULL
,NULL
,1 -- IsApproved == true
,@Now
,@Now
,@UniqueEmail
,@PasswordFormat
,@UserId OUTPUT
COMMIT
END
GO
Step 3. Create the user
DECLARE @UserId uniqueidentifier
SET @UserId = NewId()
EXECUTE [dbo].[CreateUser]
'UserName' --@UserName
,'UserP@ssword '--@ClearTextPassword
,'[email protected] '--@Email
,@UserId --User's uniqueidentifier
Additional Notes:
- Check your application name is correct in your web.config membership element.
- Remember create roles and add user to roles.
Author by
PositiveGuy
Updated on June 04, 2022Comments
-
PositiveGuy almost 2 years
I created a bunch of insert scripts to add a new user to the aspnet_Membership and aspnet_User table. I can't auth, says it can't find my user.
Has anyone tried to create a new membership user via T-SQL inserts? I have to do it this way because our create user code in C#/ASP.NET does not work at the moment.
-
Don Thomas Boyle over 6 yearswhere do i get [aspnet_Membership_CreateUser] or did my sql server admin lock me out ... ?
-
Michael about 5 yearsSame problem here. You won't have the aspnet_Membership_CreateUser command if you let visual studio set up the membership provider tables.