1

I am trying to add AP.Net membership to my database. I have had success with this in the past so I have some idea of what I am doing. My project is a VB.Net website with Framework 4.5.2. I used asp_regsql.exe in framework 4 to add the schema to my db. I created the following web.config entries (per MSDN articles):

<connectionStrings>
<add name="AFKMSConnectionString" providerName="System.Data.SqlClient" connectionString="working as intended" />    

  <system.web>
<authentication mode="Forms" >
  <forms loginUrl="Account/Login.aspx"
    name=".ASPXFORMSAUTH" />
</authentication>
<!--<authorization>
  <deny users="?" />
</authorization>-->
<compilation debug="true" targetFramework="4.5.2"/>
<httpRuntime targetFramework="4.5.2"/>
<pages>
  <namespaces>
    <add namespace="System.Web.Optimization"/>
    <add namespace="Microsoft.AspNet.Identity"/>
  </namespaces>
  <controls>
    <add assembly="Microsoft.AspNet.Web.Optimization.WebForms" namespace="Microsoft.AspNet.Web.Optimization.WebForms" tagPrefix="webopt"/>
  </controls>
</pages>
<membership defaultProvider="SqlMembershipProvider" userIsOnlineTimeWindow="15">
  <providers>
    <clear/>
    <add
    name="SqlMembershipProvider"
    type="System.Web.Security.SqlMembershipProvider"
    connectionStringName="AFKMSConnectionString"
    applicationName="AFKMS"
    enablePasswordRetrieval="false"
    enablePasswordReset="true"
    requiresQuestionAndAnswer="false"
    requiresUniqueEmail="true"
    passwordFormat="Hashed" />
  </providers>
</membership>
<profile enabled ="true" defaultProvider="SqlProvider">
  <providers>
    <clear />
    <add name="SqlProvider"
      type="System.Web.Profile.SqlProfileProvider"
      connectionStringName="AFKMSConnectionString"
      applicationName="AFKMS"
      description="SqlProfileProvider for SampleApplication" />
  </providers>
</profile>
<roleManager enabled ="true"
           defaultProvider ="SqlRoleProvider" >
  <providers>
    <clear/>
    <add name ="SqlRoleProvider"
         type="System.Web.Security.SqlRoleProvider"
         connectionStringName="AFKMSConnectionString"
         applicationName="AFKMS"/>

  </providers>
</roleManager>

My connection string works for my current functions but when I try to register a user I get the following error:

Directory lookup for the file "C:\Users\Dan\Source\Repos\AFKMS\AFKMS\App_Data\aspnet-AFKMS-140073d4-6858-4f19-9555-0edfbaadd43a.mdf" failed with the operating system error 2(The system cannot find the file specified.). CREATE DATABASE failed. Some file names listed could not be created. Check related errors.

This comes from Accounts/Register,

Dim result = manager.Create(user, Password.Text)

Why is it trying to create a database? What does it look like I am missing?

Dan Wier
  • 334
  • 5
  • 16
  • You do not have a connectionstring in your `web.config`. The membership and role provider configuration sections refer to a connection string, but it isn't present. @ahmed's answer is correct. – Glenn Ferrie Nov 28 '16 at 17:45
  • I left out the connection string for my quote. It works for all other application functions I currently have. – Dan Wier Nov 28 '16 at 17:59
  • I think the default for an ASP.Net 4.5.2 application is probably Identity Framework 2.0, not the older Membership Framework, hence the reason it's not using your connection strings. – Zhaph - Ben Duguid Nov 28 '16 at 21:26
  • I tried aspnet_regsql from framework 2 (after removing the other) and got the same result. – Dan Wier Nov 28 '16 at 23:08

2 Answers2

1
<connectionStrings>
<add name="AFKMSConnectionString" connectionString="data source=.;Initial Catalog=your_db_name;integrated security=True" providerName="System.Data.SqlClient" />
</connectionStrings>

can you provide us with code of how you create users? you mentioned: Dim result = manager.Create(user, Password.Text) i think you need to use:

Membership.CreateUser(UserName, Password, Email) 
1

I'm assuming you've created a new ASP.NET Web Application, using the Web Forms templates?

In which case you appear to be attempting to configure the newer ASP.NET Identity system with the original Membership Provider configuration settings.

As Ahmed noted, you mention the call in the register page to manager.Create(user, Password.Text) - this is using the ApplicationUserManager from ASP.NET Identity.

If you look in the App_Start folder of your project, you should find a file named IdentityConfig.vb (assuming you're using VB.NET), which has a method Create that returns the ApplicationUserManager - this in turn calls the ApplicationDbContext class that is created in the Models folder, that will be defined to use a connection called "DefaultConnection" - by default this is set to be a standalone .mdf file in the App_Data folder named after the project.

You should change this to point at your connection string, but be aware that this uses Entity Framework to create the database schema for you - it should just add it to the existing database, but you should ensure you've backed it up before you start in case it drops everything first.

Community
  • 1
  • 1
Zhaph - Ben Duguid
  • 26,785
  • 5
  • 80
  • 117
  • I don't see an App_Start folder, nor do I find an IdentityConfig.vb file. I will go through the link to the ASP.NET Identity site you linked and read up to see if I can find something applicable. – Dan Wier Nov 28 '16 at 23:14
  • To be honest I don't have the VB templates installed so was going by my C# templates - you should be able to search the project for the class that `manager` is a type of and follow it from there - do you have a models folder? – Zhaph - Ben Duguid Nov 28 '16 at 23:17
  • I'm not using MVC, I'm using webforms. I did find one other reference to defaultconnection and changed it but did not see there being any improvement – Dan Wier Nov 29 '16 at 15:19
  • ASP.Net Identity is the default with both MVC and web forms (and I assumed you were using web forms). – Zhaph - Ben Duguid Nov 29 '16 at 16:49