Using the Membership API in an existing Database

    image
       Currently, a small amount of Internet resources is dispensed with without user authorization. Therefore, at the beginning of my training, I had to get acquainted with the capabilities of ASP.NET to implement this mechanism. A little later, I learned about the existence of the Membership API and decided to use it. Fortunately, Visual Studio with the help of the Web Site Administration Tool easily allows you to configure everything for work, using basic settings. All that is needed is to choose an authentication method using forms and create a user, after which a database file with the necessary table structure will be placed in the App_Data directory. I gladly used it for a long time, not knowing the troubles. So far, one fine day, it was not necessary to slightly redo the already 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 little by little 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. Studio successfully created me a DB file with the name aspnetdb.mdf as usual. But this is not quite 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 bunch of features that I really wanted to implement.
       Then I, a little bewildered, ran into a wall. I did not know how to approach this problem. One option was to transfer tables from the old database to the new one, but I did not like this option. I was sure that there should be a different solution. Having rummaged a bit 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 capabilities of the wizard. Only the wizard will not see the SQL Express server, and in my case it is he who is used. I had to parse the command line parameters, which resulted in the following command:, where: -A Installs application services. Valid options: all - to install all services, m - designed for Membership, r

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


    - role services, p - to support user profiles, c - personalization of parts of Web pages.
    -C Allows you to specify a complete connection string.
    -d The name of the database in which to install the necessary services.

       After applying the command, the generated tables for working with users appeared in my old database. Happy with this convenience, I again ran to WAT to register the first user. Naturally, failure again awaited me.
       As it turned out a little later, you need to configure the Membership provider in the web.config of my web application. Why to the sectionyou need to put the appropriate code:    Thus, I specified the name of my provider and the connection string. This time, the user was successfully created and placed in my database. Next, I continued to develop the project already successfully using user authorization. This article is more suitable for beginners. Experienced developers are unlikely to discover something new. Special thanks to XaocCPS for help with posting .












    Also popular now: