简体   繁体   中英

ASP.Net Membership: Roles saved on Different Database

I'm learning about how to manipulate the Membership class.

I have added the Membership database to my website's SQL Database using aspnet_regsql.exe and so far I can list and add users without a problem. However, I decided to add some roles using the ASP.Net Configuration Tool. The roles were added, but did not show up in the aspnet_Roles table of my SQL database.

I have since noticed ASPNETDB.MDF in my VS2010 App_Data folder for the website in question and the Roles are appearing in there. The users are not in that database but are in the SQL DB configured in my web.config file:

<configuration> 
    <connectionStrings>
        <add name="myConnString" connectionString="Data Source=SQLDB;Initial Catalog=myDatabase;User ID=myUser;Password=myPassword" providerName="System.Data.SqlClient" />
    </connectionStrings>
    <system.web>
        <authentication mode="Forms" />
        <compilation debug="true" targetFramework="4.0">
            <assemblies>
                <add assembly="System.Web.DataVisualization, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
            </assemblies>
        </compilation>
        <machineKey validationKey="LOTSOFNUMBERS"
        decryptionKey="MORENUMBERS" validation="SHA1" decryption="AES"/>
        <membership  defaultProvider="SqlProvider">
            <providers>
                <clear/>
                <add name="SqlProvider"
                connectionStringName="myConnString"
                type="System.Web.Security.SqlMembershipProvider"
                applicationName="myApp"
                requiresUniqueEmail="true"
                minRequiredPasswordLength="8"
                minRequiredNonalphanumericCharacters="0"
                requiresQuestionAndAnswer="false"
                passwordFormat="Encrypted"
                enablePasswordRetrieval="true"
                />
            </providers>
        </membership>
        <roleManager enabled="true" />
        <pages theme="myTheme">
        </pages>
        <authorization>
            <deny users="?"/>
        </authorization>
    </system.web>
</configuration>

If the users can be saved, modified and deleted using the ASP.Net Configuration Tool on my database, then I would have expected the Roles to do the same? What step am I missing?

You need to set the role provider configuration, like your membership provider

<roleManager enabled="true" defaultProvider="TheRoleProvider">
 <providers>
  <clear/>
   <add name="TheRoleProvider" 
        type="System.Web.Security.SqlRoleProvider"
        connectionStringName="myConnString" 
        applicationName="/"/>
 </providers>
</roleManager>

You are missing roleManager. Here is an example:

<roleManager enabled="true">
    <providers>
        <clear />
        <add connectionStringName="myConnString" applicationName="/" name="AspNetSqlRoleProvider" type="System.Web.Security.SqlRoleProvider" />
        <add applicationName="/" name="AspNetWindowsTokenRoleProvider" type="System.Web.Security.WindowsTokenRoleProvider" />
    </providers>
</roleManager>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM