0

Couple of questions:

I have a database i created under the app_data folder. Is there anyway to associate this database with an EXISTING aspnetdb which is also under the same folder? If so could anyone guide me?

If not then what would be the best way to create my own database under the APP_CODE folder which would also utilise asp .net membership? By that i mean i could check for username and roles theyre in, in code (If User.IsinRole("........") etc and use the icon inside the project to open the membership page and add/modify/delete users too?

Thanks

3 Answers3

0

You can create a MembershipProvider. An example is available at code project, Custom Membership Providers

Patrick
  • 17,669
  • 6
  • 70
  • 85
0

All you need to do is change the connection string to point to the same database for both. Of course, you will have to copy the schema from one to the other, and any data you need.

Erik Funkenbusch
  • 92,674
  • 28
  • 195
  • 291
  • Ok this sounds like what im after. How could i copy the schema over? I suppose this would leave all the membership code i have written intact? –  Oct 09 '12 at 18:28
  • Lots of ways. If you're using Entity Framework, depending on the way you're doing things, it can regenerate your schema. Or you can script your schema to text files and then just change the database name... basically, however you created the tables in the first place is what you do again. – Erik Funkenbusch Oct 09 '12 at 18:42
  • Strangely enough i created an empty site and dont have any web config entries to show the connection string for aspnetdb although it does run/work. What i did initially was added my login control which created the aspnetdb, then created my second db manually. So in this case i assume your suggesting to create the aspnetdb and then add my own tables manually? –  Oct 09 '12 at 19:07
0

You can create your custom Membership Provider because you have custom dataBase, not the generated dataBase with aspnet_regsql.

public class CustomMembershipProvider : MembershipProvider
{   
    public override MembershipUser CreateUser(string username, 
       string password, string email, string passwordQuestion, 
       string passwordAnswer, bool isApproved, 
       object providerUserKey, out MembershipCreateStatus status)
    {
        throw new NotImplementedException();
    }

    public override MembershipUser GetUser(string username, bool userIsOnline)
    {
        throw new NotImplementedException();
    }

    public override bool ValidateUser(string username, string password)
    {
        throw new NotImplementedException();
    }

    public override int MinRequiredPasswordLength
    {
        get { throw new NotImplementedException(); }
    }

    public override bool RequiresUniqueEmail
    {
        get { throw new NotImplementedException(); }
    }
}

You register your memberShip provider

<connectionStrings>
    <add name="ApplicationServices" 
      connectionString="Server=your_server;Database=your_db;
                         Uid=your_user_name;Pwd=your_password;"
      providerName="System.Data.SqlClient" />
</connectionStrings>

<authentication mode="Forms">
  <forms loginUrl="~/Account/LogOn" timeout="2880" />
</authentication>

<membership defaultProvider="CustomMembershipProvider">
  <providers>
    <clear/>
    <add name="CustomMembershipProvider" 
        type="CustomMembership.Models.CustomMembershipProvider"
        connectionStringName="AppDb"
        enablePasswordRetrieval="false"
        enablePasswordReset="true"
        requiresQuestionAndAnswer="false"
        requiresUniqueEmail="false"
        maxInvalidPasswordAttempts="5"
        minRequiredPasswordLength="6"
        minRequiredNonalphanumericCharacters="0"
        passwordAttemptWindow="10"
        applicationName="/" />
  </providers>
Aghilas Yakoub
  • 28,516
  • 5
  • 46
  • 51
  • What is it about the question that makes you think a custom membership provider is required? He just seems to not want to use two databases, and put both his tables and asp.net tables in the same database. All that requires is a connection string change. – Erik Funkenbusch Oct 09 '12 at 18:41
  • @Mystere Because he creates your custom database so schema of database is custom => so he needs custom services in order to create his objects as membershipuser – Aghilas Yakoub Oct 09 '12 at 18:45
  • I don't see anywhere in his question where he says he creates a custom membership schema. He's talking about using asp.net and using his own tables (that are not related to membership) – Erik Funkenbusch Oct 09 '12 at 18:47
  • Guys without trying to get myself confused :( i did write "which would also utilise asp .net membership? By that i mean i could check for username and roles theyre in, in code (If User.IsinRole("........") etc and use the icon inside the project to open the membership page and add/modify/delete users too?" in other words i want all the basic features to use ASP .Net membership and my second database - all under one database but also being able to use the usual method to access the asp .Net Administration centre to create roles/users etc. Thanks –  Oct 09 '12 at 19:18