简体   繁体   中英

WebMatrix.WebData can't read membership information from a separate ASP.NET application

I have an existing project, which uses ASP.Net, System.Web.Security, and WebMatrix.WebData for the membership system. This application is where all the users are created, updated, etc, and stored to a database.

I also have a separate application, which is just a plain Azure worker role, which needs to read some of the membership information back from the database. For example, I need to view what roles a particular user is in. I expected the sample program below to show the different roles - "Administrator", "Account Manager", etc, and also for certain users to be associated with those roles. I have checked in the database, and the associations are all stored correctly in the 'webpages_Membership' tables etc.

Now the problem is, my second application can't see any of the roles created by the first one. I've tried to copy as much of the relevant configuration settings from the first project to the second. I realize WebMatrix.WebData as a package is deprecated, it's included in Microsoft.AspNet.WebPages.WebData , I've just been trying to keep everything exactly the same between the two projects.

public static void Main()
{
    if (!WebSecurity.Initialized)
    {
        Console.WriteLine("Initializing WebSecurity");
        WebSecurity.InitializeDatabaseConnection("CustomDbContext", "UserProfile", "Id", "Username", autoCreateTables: false);
    }

    using (var db = new CustomDbContext())
    {
        Console.WriteLine("All Roles : {0}", Roles.GetAllRoles().ToString());

        foreach (var user in db.UserProfiles)
        {
            Console.WriteLine("User      : {0}", user.UserName);
            Console.WriteLine("    Roles : {0}", Roles.GetRolesForUser(user.UserName).ToString());
        }
    }

    Console.Read();
}

Packages.config:

<?xml version="1.0" encoding="utf-8"?>
<packages>
  <package id="EntityFramework" version="6.1.3" targetFramework="net461" />
  <package id="Microsoft.AspNet.WebPages.Data" version="2.0.20710.0" targetFramework="net461" />
  <package id="Microsoft.AspNet.WebPages.WebData" version="2.0.20710.0" targetFramework="net461" />
  <package id="CustomDataModels" version="1.0.0" targetFramework="net461" />
  <package id="WebMatrix.WebData" version="2.0.30506.0" targetFramework="net461" />
</packages>

App.config:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </configSections>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
  </startup>
  <connectionStrings>
    <add name="CustomDbContext" connectionString="XXX" providerName="System.Data.SqlClient" />
  </connectionStrings>
  <system.web>
    <authentication mode="Forms">
      <forms loginUrl="~/Account/Login" timeout="2880" />
    </authentication>
    <machineKey validationKey="XXX" decryptionKey="XXX" validation="SHA1" decryption="AES" />
    <roleManager enabled="true" defaultProvider="SimpleRoleProvider">
      <providers>
        <clear />
        <add name="SimpleRoleProvider" type="WebMatrix.WebData.SimpleRoleProvider, WebMatrix.WebData" />
      </providers>
    </roleManager>
    <membership defaultProvider="SimpleMembershipProvider">
      <providers>
        <clear />
        <add name="SimpleMembershipProvider" type="WebMatrix.WebData.SimpleMembershipProvider, WebMatrix.WebData" />
      </providers>
    </membership>
  </system.web>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="WebMatrix.Data" publicKeyToken="31bf3856ad364e35" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-1.0.0.0" newVersion="1.0.0.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
  </entityFramework>
</configuration>

Well this is embarrassing. At first, I was missing the configuration section from App.config:

<system.web>
  <roleManager enabled="true" defaultProvider="SimpleRoleProvider">
    <providers>
      <clear />
      <add name="SimpleRoleProvider" type="WebMatrix.WebData.SimpleRoleProvider, WebMatrix.WebData" />
    </providers>
  </roleManager>
  <membership defaultProvider="SimpleMembershipProvider">
    <providers>
      <clear />
      <add name="SimpleMembershipProvider" type="WebMatrix.WebData.SimpleMembershipProvider, WebMatrix.WebData" />
    </providers>
  </membership>
</system.web>

At this point, I was inspecting the contents of the arrays in the debugger, and seeing them empty. However, after I added that section, I resorted to just reading the console, and couldn't figure out why nothing I did changed the output.

Console.WriteLine("All Roles : {0}", Roles.GetAllRoles().ToString());

Spot anything wrong? I've been briefly looking at the output after every change I made, expecting to see a change in the output, but I was looking at array references instead of their actual contents!

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