Since SQL Server 2005 express provided by MS was not installed before vs.net 2005 was installed, it takes some twists and turns to be exhausted when using membership and roles.
The author uses the SQL Server 2000 database. To use membership, register the membership table in the specified database in the "Visual Studio 2005 command prompt" window, for example, aspnet_regsql-e-s "localhost"-a m. If you want to use roles, register
Aspnet_regsql-e-s "localhost"-a r. For details about the aspnet_regsql command, enter aspnet_regsql /? .
After the database is created, configure web. config as follows:
<! -- Rolemanager -->
< Rolemanager Defaultprovider = "Sqlprovider"
Enabled = "True"
Cacherolesincookie = "True"
Cookiename = ". Asproles"
Cookietimeout = "30"
Cookiepath = "/"
Cookierequiressl = "False"
Cookieslidingexpiration = "True"
Cookieprotection = "All" >
< Providers >
< Clear />
< Add
Name = "Sqlprovider"
Connectionstringname = "Mysqlconnection"
Applicationname = "Rolemanager"
Type = "System. Web. Security. sqlroleprovider, system. Web, version = 2.0.0.0, culture = neutral, publickeytoken = b03f5f7f11d50a3a" />
</ Providers >
</ Rolemanager >
<! -- Role manager end -->
<! -- Membership -->
< Membership Defaultprovider = "Sqlprovider" Userisonlinetimewindow = "15" >
< Providers >
< Clear />
< Add
Name = "Sqlprovider"
Type = "System. Web. Security. sqlmembershipprovider"
Connectionstringname = "Mysqlconnection"
Applicationname = "Membership"
Enablepasswordretrieval = "False"
Enablepasswordreset = "True"
Requiresquestionandanswer = "True"
Requiresuniqueemail = "True"
Passwordformat = "Hashed" />
</ Providers >
</ Membership >
<! -- Membership end -->
Configure the database connection string in the <connectionstrings> node.
< Add Name = "Mysqlconnection" Connectionstring = "Data Source = localhost; initial catalog = aspnetdb; persist Security info = true; user id = sa; Password = 123" />
when using membership. createuser, The aspnet_applications table generates a record based on the applicationname In the configured membership node, and each aspnet_users and aspnet_membership generate a record. When using roles. when addusertorole () is used, the author intentionally writes an error in username and inserts the result successfully. aspnet_user generates a record, and aspnet_usersinroles also has a record. That is to say, when roles is used. when addusertorole () is used, no matter whether the user is in membership or not, it will be successfully added to roles. Isn't the logic provided by Microsoft wrong?
note that the membership and roles data are in the same dB. Can they be separated? The test results are acceptable. Use aspnet_regsql-e-s testserver-a r to create a roles-related table and database on testserver. Add a database connection node change connectionstringname in rolemanager to rolesqlconnection. call roles. addusertorole () and aspnet_user tables also generate records without checking aspnet_membership.
I personally think this solution has both advantages and disadvantages. It may be used in large enterprise applications separately to improve efficiency. However, in small systems, if the Program Member calls addusertorole () in write permission management and forgets to perform the check, data confusion may occur.