ASP.Net Membership: Roles saved on Different Database

纵饮孤独 提交于 2019-12-11 07:48:15

问题


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?


回答1:


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>



回答2:


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>


来源:https://stackoverflow.com/questions/8911738/asp-net-membership-roles-saved-on-different-database

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