1
votes

I'm trying to seed the DNN database with users and roles from an XML file I have with the users and the roles they should be a member of. How can I seed the database in a way that DNN will pick up the existing user account and associated roles, when the user logs on with the Google Authentication provider?

We're using Google Apps to do the authentication, and that works when you do the account validation as DNN manager manually, and assign roles manually.

I tried entering in Users, UserRoles, UserPortals and aspnet_Membership, aspnet_Users. But that doesn't work. When I try to log on I get a message that the user account is already in use.

Edit The problem seems to be that I'm not able to fill the AuthenticationToken column in the UserAuthentication table. I don't know how the values that are inserted into this column are constructed.

Is the Google Authentication provider for DNN itself also Open Source so I can take a look at how this works? I haven't been able to find code, but maybe I didn't search long/good enough :)

2
Are you tried to insert User manually on those tables (Users, UserRoles, UserPortals and aspnet_Membership, aspnet_Users)?? OR you have specific script to insert users on those tables from an XML file??Keval Gangani
I have a C# program that performs the inserts/updates. The problem seems to be that I can't add information in the UserAuthentication, because I don't know how the AuthenticationToken column should be filled.Sander Rijken
Okay...one more question...Google authentication provider extension implemented by your self OR it is already exist in DNN store or somewhere else?Keval Gangani
I'm using the one that ships with DNN (or maybe it's provided by them)Sander Rijken

2 Answers

0
votes

Here is some documentation from DNN on how to enable some of the oAuth provider implementations including the Google provider which I assume is the one you are using.

Notice the section for "Configuration of Registration Options in Site Settings" where it explains the registration options. All authentication providers should automatically create DNN user accounts upon the first successful authentication. Which means you shouldn't have to seed the database with users beforehand. It looks like in DNN 7.4, there were changes to how the accounts are created based on the Site Settings Registration type.

If this is not happening for you or you need to update user information from the source (google) in a very specific way, you may need to customize your own authentication provider. I have a tutorial that explains the basics of this on DNNHero.com.

0
votes

I have had some research, merge all reference code and developed a working script for you.

You have to still do some modification in my script as your requirement. My script developed for add users programmatically in DNN website. I think you need to do foreach loop to insert all your Google users from XML file. You can do it to call Dim status As UserCreateStatus = CreateUser(Me.PortalId) line in foreach loop.

Step 1: Create role "Google User" in your DNN website.

Step 2: Create member variable of MembershipProvider in your class.

Private Shared memberProvider As DotNetNuke.Security.Membership.MembershipProvider = DotNetNuke.Security.Membership.MembershipProvider.Instance()

Step 3: Set your XML user data in below method.

Private Shared Function GetUserInfo(ByVal fiPortalId As Integer) As UserInfo
    Dim a As New UserInfo
    a.FirstName = FirstName
    a.LastName = LastName
    a.PortalID = fiPortalId
    a.Email = EMail
    a.Username = UserName
    a.DisplayName = DisplayName


    Dim objMembership As UserMembership = New UserMembership
    objMembership.Approved = True
    objMembership.CreatedDate = DateTime.Now
    objMembership.Email = EMail
    objMembership.Username = UserName
    objMembership.Password = Password

    a.Membership = objMembership
    a.IsSuperUser = False
    Return a
End Function

Step 4: Create user method.

Public Shared Function CreateUser(ByVal fiPortalId As Integer) As UserCreateStatus
    Dim createStatus As UserCreateStatus = UserCreateStatus.AddUser
    Dim user As UserInfo = GetUserInfo(fiPortalId)
    'Create the User

    createStatus = memberProvider.CreateUser(user)
    If createStatus = UserCreateStatus.Success Then
        'Dim objEventLog As New Services.Log.EventLog.EventLogController
        'objEventLog.AddLog(objUser, PortalController.GetCurrentPortalSettings, UserController.GetCurrentUserInfo.UserID, "", Services.Log.EventLog.EventLogController.EventLogType.USER_CREATED)
        DataCache.ClearPortalCache(user.PortalID, False)
        addRoleToUser(user, "Google User", DateTime.Now.AddYears(25))
       End If
    Return createStatus
End Function

Step 5: Apply role to all users.

Public Shared Function addRoleToUser(ByRef user As UserInfo, ByVal roleName As String, ByRef expiry As DateTime) As Boolean
    Dim rc As Boolean = False
    Dim roleCtl As RoleController = New RoleController
    Dim newRole As RoleInfo = roleCtl.GetRoleByName(user.PortalID, roleName)

    If newRole IsNot Nothing And user IsNot Nothing Then
        roleCtl.AddUserRole(user.PortalID, user.UserID, newRole.RoleID, DateTime.MinValue, expiry)
        user = UserController.GetUserById(user.PortalID, user.UserID)
        rc = user.IsInRole(roleName)
    End If

    Return rc
End Function

I had test above script in my computer and it works. Please let me know if you have any questions.