如何在不使用CreateUser的情况下在ASP.NET成员资格表中创建用户

时间:2023-01-04 03:41:32

I want to migrate users and posts from an existing forum I am using to a ASP.NET membership table without using CreateUser. Basically I want to maintain userIDs so that when I migrate posts they continue to associate with the correct users. Is there a way to do this or would I be better off just using CreateUser and then finding a way to re-assign the post-by-UserIDs to the correct new IDs?

我想将用户和帖子从我正在使用的现有论坛迁移到ASP.NET成员资格表而不使用CreateUser。基本上我想维护用户ID,以便在我迁移帖子时,他们继续与正确的用户相关联。有没有办法做到这一点,或者我会更好地使用CreateUser,然后找到一种方法将post-by-UserID重新分配给正确的新ID?

Thanks

谢谢

2 个解决方案

#1


11  

You could create a UserProfile table that you can use to join from the User table to your Post table. This will insulate you from the ASP.NET Membership Provider's internals and can be an extension point for additional user information.

您可以创建一个UserProfile表,您可以使用该表从User表连接到Post表。这将使您与ASP.NET成员资格提供程序的内部隔离,并且可以作为其他用户信息的扩展点。

Here's a stored procedure and associated functions that I use to create new users and their profiles:

这是我用来创建新用户及其配置文件的存储过程和相关功能:

CREATE PROCEDURE [dbo].[CreateUser] 
  @UserName nvarchar(256)
, @ClearTextPassword nvarchar(128)
, @Email nvarchar(256)
, @PostingID 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

SET @ApplicationName = 'YOUR_APPLICATION_NAME'
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


BEGIN TRANSACTION

DECLARE @UserId uniqueidentifier

EXECUTE [dbo].[aspnet_Membership_CreateUser] 
   @ApplicationName
  ,@UserName
  ,@Password
  ,@PasswordSalt
  ,@Email
  ,NULL
  ,NULL
  ,1
  ,@Now
  ,@Now
  ,@UniqueEmail
  ,@PasswordFormat
  ,@UserId OUTPUT

INSERT INTO [dbo].[UserProfile]
(
 [UserID]
,[PostingID]
)
VALUES
(
 @UserId
,@PostingID
)

COMMIT  

CREATE FUNCTION [dbo].[base64_decode] 
(@base64_text VARCHAR(max)) 
RETURNS VARBINARY(max)

WITH SCHEMABINDING, RETURNS NULL ON NULL INPUT

BEGIN

DECLARE @x XML; SET @x = @base64_text 
RETURN @x.value('(/)[1]', 'VARBINARY(max)')

END

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

#2


0  

What I would do is I would create a link table between your user's table and ASP membership table where membership ID (email address?) would be related to UserID (Integer?). Then I would build a routine for creating users and populating membership table with users data programmatically - it is a little easier to do through .NET code.

我会做的是我会在用户的表和ASP成员资格表之间创建一个链接表,其中成员资格ID(电子邮件地址?)将与UserID(整数?)相关。然后我将构建一个用于创建用户的例程,并以编程方式使用用户数据填充成员资格表 - 通过.NET代码更容易实现。

#1


11  

You could create a UserProfile table that you can use to join from the User table to your Post table. This will insulate you from the ASP.NET Membership Provider's internals and can be an extension point for additional user information.

您可以创建一个UserProfile表,您可以使用该表从User表连接到Post表。这将使您与ASP.NET成员资格提供程序的内部隔离,并且可以作为其他用户信息的扩展点。

Here's a stored procedure and associated functions that I use to create new users and their profiles:

这是我用来创建新用户及其配置文件的存储过程和相关功能:

CREATE PROCEDURE [dbo].[CreateUser] 
  @UserName nvarchar(256)
, @ClearTextPassword nvarchar(128)
, @Email nvarchar(256)
, @PostingID 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

SET @ApplicationName = 'YOUR_APPLICATION_NAME'
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


BEGIN TRANSACTION

DECLARE @UserId uniqueidentifier

EXECUTE [dbo].[aspnet_Membership_CreateUser] 
   @ApplicationName
  ,@UserName
  ,@Password
  ,@PasswordSalt
  ,@Email
  ,NULL
  ,NULL
  ,1
  ,@Now
  ,@Now
  ,@UniqueEmail
  ,@PasswordFormat
  ,@UserId OUTPUT

INSERT INTO [dbo].[UserProfile]
(
 [UserID]
,[PostingID]
)
VALUES
(
 @UserId
,@PostingID
)

COMMIT  

CREATE FUNCTION [dbo].[base64_decode] 
(@base64_text VARCHAR(max)) 
RETURNS VARBINARY(max)

WITH SCHEMABINDING, RETURNS NULL ON NULL INPUT

BEGIN

DECLARE @x XML; SET @x = @base64_text 
RETURN @x.value('(/)[1]', 'VARBINARY(max)')

END

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

#2


0  

What I would do is I would create a link table between your user's table and ASP membership table where membership ID (email address?) would be related to UserID (Integer?). Then I would build a routine for creating users and populating membership table with users data programmatically - it is a little easier to do through .NET code.

我会做的是我会在用户的表和ASP成员资格表之间创建一个链接表,其中成员资格ID(电子邮件地址?)将与UserID(整数?)相关。然后我将构建一个用于创建用户的例程,并以编程方式使用用户数据填充成员资格表 - 通过.NET代码更容易实现。