MonoX support board

Start the conversation, ask questions and share tips and solutions with fellow developers.

Non-registered users can only browse through our support boards. Please register now if you want to post your questions. It takes a second and it is completely free. Alternatively, you can log in without registration using your credentials at major sites such as Google, Microsoft Live, OpenId, Facebook, LinkedIn or Yahoo.

Use external DB for Membership & Roles  (Mono Support )

Viewed 6454 time(s), 9 post(s) 3/17/2012 2:30:54 AMby gstadter
gstadter

gstadter

3/17/2012 2:30:54 AM
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
This content has not been rated yet. 
669 Reputation 67 Total posts
denis

denis

3/17/2012 9:05:43 PM
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.
This content has not been rated yet. 
7117 Reputation 947 Total posts
gstadter

gstadter

3/19/2012 5:06:03 PM
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.
This content has not been rated yet. 
669 Reputation 67 Total posts
denis

denis

3/19/2012 5:22:38 PM
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.
This content has not been rated yet. 
7117 Reputation 947 Total posts
gstadter

gstadter

3/19/2012 6:56:07 PM
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!
This content has not been rated yet. 
669 Reputation 67 Total posts
denis

denis

3/19/2012 7:28:01 PM
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;
        }
    }
}





This content has not been rated yet. 
7117 Reputation 947 Total posts
gstadter

gstadter

3/20/2012 4:06:37 PM
Thank you for the elegant solution, Denis!
This content has not been rated yet. 
669 Reputation 67 Total posts
Bizzle

Bizzle

3/20/2014 9:57:37 AM
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.
This content has not been rated yet. 
5 Reputation 1 Total posts
khorvat

khorvat

3/20/2014 1:05:58 PM
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
This content has not been rated yet. 
15663 Reputation 2163 Total posts