|
Recently, the Membership framework in 2.0 is used in the project. This article will discuss how to scale it to meet the needs of the actual project. During the period, after constantly exploring and understanding, we also found some relevant information. The following will provide a complete solution-to expand through table Association. Of course, this is just an idea for implementation. We will discuss it here.
There are also a lot of related articles on the Internet to discuss the solution, but most of them are synopsis, without detailed guidance on how developers who are new to the framework can implement it. The proposed solution is to associate data using the userid field. I believe that any developer who wants to use Membership is familiar with the data tables starting with the basic aspnet, they also know the meaning of each field.
Most developers may encounter the following problems: the default aspnet_Users is insufficient to meet user-defined information, and many large fields are not suitable for storing in the Profile, you can use the following method to register a user: when a user clicks the register button, first use Membership. the CreateUser method registers a user in aspnet_Users. Before registering a user, you can set the registration conditions required in your project, for example, the same name is not allowed (the conditions in the Code are: only one showfolk with this name can be found in a zone ).
If (! ShowfolkSys. Exists (name, section ))
{
// Enable membership to create a user
Try
{
MembershipUser newUser = Membership. CreateUser (UserName. Text, Password. Text );
}
Catch (MembershipCreateUserException exception)
{
ErrorMessage. Text = GetErrorMessage (exception. StatusCode );
Return;
}
Catch (HttpException ex)
{
ErrorMessage. Text = ex. Message;
Return;
}
}
Then, insert the registration information into your table. To associate the aspnet_Users table, you must add the userid (GUID) field to the table you have defined ).
Next, we will first provide a stored procedure for creating a user: InsertUser
Create procedure InsertUser
@ UserName VARCHAR (50) = NULL,
@ InstitutionID INT = NULL,
@ Email VARCHAR (50) = NULL,
@ Phone VARCHAR (50) = NULL,
@ Mobile VARCHAR (50) = NULL
AS
BEGIN
DECLARE @ UserId uniqueidentifier
SELECT @ UserId = UserId FROM dbo. aspnet_Users where lower (@ UserName) = LoweredUserName -- AND @ ApplicationId = ApplicationId
IF (@ UserId is not null)
BEGIN
INSERT Users (
Userid,
Username,
InstitutionID,
Email,
Phone,
Mobile
)
VALUES
(
@ Userid,
@ UserName,
@ InstitutionID,
@ Email,
@ Phone,
@ Mobile
)
END
END
------------------------------------------------
GO
The first two sentences of the stored procedure are to query the UserId field in aspnet_Users through the value of @ UserName, and then insert it into the custom table. In this way, the default aspnet_Users table can be used in the member role management section, while the custom information can be used in the table.
Below are my settings for membership in Web. Config, removing the default special characters and unique Email address restrictions in the password. For reference.
<Membership defaultProvider = "AspNetMembershipProvider" userIsOnlineTimeWindow = "20">
<Providers>
<Remove name = "AspNetSqlMembershipProvider"/>
<Clear/>
<Add name = "AspNetMembershipProvider"
RequiresQuestionAndAnswer = "false"
Type = "System. Web. Security. SqlMembershipProvider"
ConnectionStringName = "SqlServices"
EnablePasswordRetrieval = "false"
MinRequiredNonalphanumericCharacters = "0"
MinRequiredPasswordLength = "6"
EnablePasswordReset = "false"
PasswordFormat = "Hashed"
RequiresUniqueEmail = "false"
ApplicationName = "BJWL"/>
</Providers>
</Membership> |