Mono Support Use external DB for Membership & Roles 

Viewed 16571 time(s), 9 post(s), 3/17/2012 2:30:54 AM - by gstadter
3/17/2012 2:30:54 AM
669 Reputation 67 Total posts

Found this thread, but it is closed:
http://www.mono-software.com/Mono/Pages/Discussion/dtopic/xk2soFQjTEW7aZ8wAKcFng/Possible-integration-with-another-database/

Would anyone care to share with me what the procedures or steps would be to get an Instance of MonoX to use users and roles from a database other than the main one it sets up on first use?

Any direction or help would be greatly appreciated.
-Gary

1
3/17/2012 9:05:43 PM
7207 Reputation 956 Total posts

Hi Gary,
The information provided in that thread is still valid. MonoX relies on membership and role providers, and if your database follows the basic ASP.NET membership guidelines, it will be easy to do - you will just need to switch databases in the web.config. Alternatively, you could develop your own providers.

2
3/19/2012 5:06:03 PM
669 Reputation 67 Total posts

Thanks Denis. Not sure if I did something wrong or not, but the current state of my testing....

Login to my MonoX install uses IDs/passwords from external database exactly as expected. I even created a brand new acct and added it to the Admins group(all via membership app outside of MonoX app) and it was able to log into MonoX and was recognized as an admin.

My problem is that the MonoX app still shows users/roles from it's own db within the admin UI. So, it is "using" users/passwords/roles from the correct DB, but the admin UI is "showing" users/roles from the wrong db.

3
3/19/2012 5:22:38 PM
7207 Reputation 956 Total posts

Hi,
Everything works as expected. You will have to add users and roles from external DB to MonoX DB. They will not be used as such, but are needed to preserve referential integrity (many Web parts and pages have users and roles bound to them). In fact, users should be inserted automatically by the LoginModule.ascx Web part when it detects that your membership provider does not inherit from System.Web.Security.SqlMembershipProvider (but I guess in your scenario you do inherit from it).

I can send some sample source code that performs these tasks if you need it.

4
3/19/2012 6:56:07 PM
669 Reputation 67 Total posts

Wouldn't think of expecting you to write them for me, but if you already have some type of sample i could look at, that would be super!

5
3/19/2012 7:28:01 PM
7207 Reputation 956 Total posts

No problem. When initializing your tole provider, you should do something like this:

string[] roles = GetAllRoles(); // this should return a list of roles from external DB
using (MonoSoftware.MonoX.Repositories.MembershipRepository rep = MonoSoftware.MonoX.Repositories.MembershipRepository.GetInstance())
{
    foreach (string role in roles)
    {
        rep.CreateAndSaveNewRole(role);
    }
}

Login Web part in turns does something like this to insert a user that is not already present in the MonoX DB, so you can do something like that in the overridden login module (OnLoggedInRedirect event would be a good place to place code like this):

if (!(Membership.Provider is System.Web.Security.SqlMembershipProvider))
{               
    UserRepository repository = UserRepository.GetInstance();
    AspnetUsersEntity authenticatedUser = repository.GetUser(ctlLogin.UserName);
    if (authenticatedUser == null)
    {
        if (!repository.CreateUserManually(ctlLogin.UserName))
        {
            validationSummaryLogin.AddMessage(DefaultResources.ErrorMessage_Login);
            return;
        }
    }
}





6
3/20/2012 4:06:37 PM
669 Reputation 67 Total posts

Thank you for the elegant solution, Denis!

7
3/20/2014 9:57:37 AM
5 Reputation 1 Total posts

Hello, 
Does anyone know if this still relevant in the free version please? We've changed membership in the config file and while we login we can not do anything once logged in - would this need some re-factoring after buying the code?

The error we get is:
    Exception type: ORMQueryExecutionException 
    Exception message: An exception was caught during the execution of an action query: The INSERT statement conflicted with the FOREIGN KEY constraint "FK_Page_aspnet_Users". The conflict occurred in database "MonoXTrial", table "dbo.aspnet_Users", column 'UserId'.

which kind of indicated MonoX is trying to use the original membership database. Are there any other steps we have to do?

Many thanks in advanced!

Background
We have all our users in the asp.net membership module of our cms and wanted to add a community part to the site, which we'd be happy to pay for. The one condition is that we must reuse the membership of the cms.

8
3/20/2014 1:05:58 PM
15993 Reputation 2214 Total posts

Hi,

you are getting the error because you don't have a user in your database that can be used as an owner of the newly inserted record in the pages table. This is an issue related to database referential integrity that you can't overcome without database synchronization or maybe by changing existing schema and removing references to user tables which I definitely don't recommend. Same will happen when you try to add e.g. comment on some users wall etc. so the best approach would be to create a synchronization between these to databases so you can use all of the MonoX goodies. 

Recommendation
- create a sync job and sync only Aspnet_Users, Aspnet_Membership, Aspnet_Roles, Aspnet_UsersInRoles tables
- create your own membership provider - as you did
- as MonoX database would be "secondary" one, I recommend turning off the registration in appSettings as users will register on your primary Database/WebSite
- consider turning off some parts of the profile related to password etc.

Regards

9
This is a demo site for MonoX. Please visit Mono Software for more info.