SqlMembershipProvider: use my own database as user store database

社会主义新天地 提交于 2019-12-22 10:44:43

问题


I'm developing an ASP.NET application with Visual Studio 2008 SP1 and C#. I'm also using Sql Server 2008 SP1.

I'm trying to add the tables created with the command aspnet_regsql -S (local) -E -A m to my own database.

If I run that command it will create a database (Aspnetdb database) with four tables. I want to know if there is a command that creates those tables in my own database (called myDatabase, for example).

And, when the tables are created. How can I connect authentication to myDatabase?

Thank you


回答1:


aspnet_regsql-C "Data Source=.\SQLEXPRESS;Integrated Security=True;User Instance=True" ...

will generate in database given by connection string

Also you should configure MembershipProvider to work with your database. Just add this to your web.config

<configuration>
  <connectionStrings>
    <add name="SqlServices" connectionString="Data Source=.\SQLEXPRESS;Integrated Security=True;User Instance=True" />
  </connectionStrings>
  <system.web>
    <authentication mode="Forms" >
      <forms loginUrl="login.aspx"
        name=".ASPXFORMSAUTH" />
    </authentication>

    <membership defaultProvider="SqlProvider"
      userIsOnlineTimeWindow="15">
      <providers>
        <add 
          name="SqlProvider" 
          type="System.Web.Security.SqlMembershipProvider" 
          connectionStringName="SqlServices"
          applicationName="MyApplication"
          enablePasswordRetrieval="false"
          enablePasswordReset="true"
          requiresQuestionAndAnswer="true"
          requiresUniqueEmail="false"
          passwordFormat="Hashed"
          maxInvalidPasswordAttempts="5"
          passwordAttemptWindow="10" />
      </providers>
    </membership>
  </system.web>
</configuration>



回答2:


According to the output from "aspnet_regsql -?", there is a paramter -d that you can use, thus making your command:

 aspnet_regsql -S (local) -E -A -d myDatabase

Alternatively you could call it with the "-sqlexportonly filenamegoes.here" parameter to generate a Sql Script that you can review and then apply to your database:

 aspnet_regsql -S (local) -E -A -sqlexportonly output.sql

The MSDN documentation for SqlMembershipProvider explains how to change where it looks for its database, specifically the line connectionStringName="SqlServices" in the Example.




回答3:


You can specify the -C <connection string> option instead of the -S (local) option and specify a database name in the connection string. This allows to to run the create scripts on an existing database. See here for the aspnet_regsql options.



来源:https://stackoverflow.com/questions/2333705/sqlmembershipprovider-use-my-own-database-as-user-store-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!