📜 ⬆️ ⬇️

We use the Membership API in the existing Database

image
At present, a small amount of Internet resources does not require user authentication. Therefore, at the beginning of my training, I had to become familiar with the possibilities of ASP.NET to implement this mechanism. A little later, I found out about the existence of the Membership API and decided to use it. Fortunately, Visual Studio with the Web Site Administration Tool easily allows you to configure everything to work using the basic settings. All that is needed is to select an authentication method using forms and create a user, after which a database file with the required table structure will be placed in the App_Data directory. I gladly used it for a long time, not knowing the troubles. Until one day it was necessary to slightly redo an existing project, in which authorization was not originally intended.

Since work with users was not planned, I boldly got myself a database, filled it with the tables I needed and gradually developed the project. The project began to grow gradually and at some point, I decided, nevertheless, to implement an authentication mechanism. Out of habit, I went to WAT and started to get a user. The studio successfully created a database file for me with the name aspnetdb.mdf as usual. But this is not exactly what I needed. Of course, I could use this database for authentication, and my previous table for the project, but then I will not be able to use foreign keys between the databases and I will lose a lot of features that I wanted to implement.
Then I, a little confused, ran into the wall. I did not know how to approach this problem. One option was to transfer the tables from the old database to the new one, but I didn’t like this option at all. I was sure that there must be a different solution. Having a little rummaged on the Internet, I learned that it is necessary to use the aspnet_regsql.exe tool. If you start without parameters, you can use the wizard. Only the master will not see the SQL Express server, and in my case it is used. I had to parse the command line parameters, which resulted in the following command:

aspnet_regsql.exe –A all –C "Data Source=.\SQLEXPRESS;Integrated Security=True;User Instance=True" –d "D:\aspnet_projects\myproject\App_Data\myolddbname.mdf"

where:
-A Installs application services. Valid options are: all — to install all services, m — designed for Membership, r — role services, p — to support user profiles, c — personalize parts of Web pages.
-C Allows you to specify the complete connection string.
-d The name of the database in which to install the necessary services.

After applying the command in my old database, the generated tables for working with users appeared. Overjoyed by this convenience, I again ran into WAT to register the first user. Naturally, I was waiting for failure again.
As it turned out a little later, you need to configure the Membership provider in the web.config of my web application. What for should the corresponding code be placed in the <system.web> section:
')
<membership defaultProvider="MyMembershipProvider">
<providers>
<add name="MyMembershipProvider" type="System.Web.Security.SqlMembershipProvider" connectionStringName="MyConnectionString" applicationName="/" />
</providers>
</membership>

Thus, I indicated my supplier’s name and connection string. This time, the user was successfully created and placed in my database. Then I continued to develop the project already successfully using user authorization.

The article is more suitable for beginners. Experienced developers are unlikely to discover something new.

For assistance in publishing a special thank you to XaocCPS

Source: https://habr.com/ru/post/69567/


All Articles