Contact Sign out?

Graham Mott asked on February 19, 2020 16:28


I'm looking into improving my contact management and want to associate the users closer to the contacts. This equates to new contacts on cookie acceptance and users on account creation with the contact then being associated to the user.

The problem I have encountered is when the user signs out their contact is maintained so there would be the risk that when a second user then logs in on the same session (low chance on my site but still possible) it would try associating the contact for the first user with this second user.

To avoid this I wanted to change the current contact to a new anonymous contact essentially signing the user out of their contact.

Is this possible and if so how would it be implemented?

Thanks, Graham

Correct Answer

Graham Mott answered on February 20, 2020 14:15

OK I have got it working for anyone else that comes into this problem:

// Signs out the current user
    DefaultAuthenticationTypes.ApplicationCookie, DefaultAuthenticationTypes.ExternalCookie);

//Removes current contact (will explain this below)

//Manage my consent and set the current contact to visitor
var consent = ConsentInfoProvider.GetConsentInfo(ResourceConstants.ConsentPolicyName);

//create new anonymous contact
var newAnomContact = new ContactInfo()
    ContactLastName = $"Anonymous - {DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")}",
    ContactMonitored = true

//assign anonymous contact to current contact and accepts t&c's
mcurrentContactProvider.SetCurrentContact(newAnomContact); //Instance of ICurrentContactProvider
consentAgreementService.Agree(newAnomContact, consent);

So while very frustratingly working my way through the contact management I eventually solved the creation of the anonymous contact and setting them to current.

There were 3 major problems

  • The lack of info on the connections between all the contact management portions e.g. ContactManagementContext being able to get the current contact but not set it or ICurrentContactProvider being able to set the current contact but not get it without a user.

  • The problem that if a ContactManagementContext.Clear() removes the current contact but is only required because if the current contact isn't cleared ICurrentContactProvider.SetCurrentContact() does not take importance and while it appears to create a new contact fine all the activities are still logged as the previous contact

  • The fact that if ContactMonitored is not set to true the activities aren't logged for that contact (you would quite reasonably assume that it is either dependent on a separate value the consentagreementservice sets or that the consentagreementservice would set the contactmonitored to true)

I cannot believe that it is actually taking so much individual actions and about a day or two of research to mostly luck my way through what should really be a pretty reasonable action.

0 votesVote for this answer Unmark Correct answer

Recent Answers

Brenden Kehren answered on February 19, 2020 16:44

Users can be associated with Contacts. You have to provide that link of a user to a contact. A contact is never authenticated so technically speaking you can't log a contact out only a user. But when that user logs out, the contact GUID is still stored in the cookie, which is supposed to happen. When a new user logs in and that user is associated with a contact, that contact cookie will be updated with the new GUID for that contact. This functionality is out of the box.

To associate a user account to a contact, you'd need to do this in the API by setting the ContactUserID = the user logging in UserID. This should happen by default.

If you want to totally clear that contact cookie, you'd be infringing on the base functionality of contact tracking and have to modify some base code.

0 votesVote for this answer Mark as a Correct answer

Dmitry Bastron answered on February 19, 2020 16:49

Hi Graham,

Kentico allows customization of this behavior. Basically, you need to:

  • Implement ICurrentUserContactProvider interface (you can copy Kentico's default implementation to begin with)
  • Amend the logic of GetContactForCurrentUserAndContact method. For example, you can check here if current contact's email is not equal current user's email - return null and Kentico will create a new anonymous contact for that user. Or try to find existing contact for user's email, it's up to you. This code will be called on login before assigning the contact.
  • Register your custom implementation like this: [assembly: RegisterImplementation(typeof(ICurrentUserContactProvider), typeof(CustomCurrentUserContactProvider))]

And that's it.

1 votesVote for this answer Mark as a Correct answer

Graham Mott answered on February 20, 2020 12:57

Hey Dmitry,

That doesn't do what I'm after as I am after an anonymous user and contact. What I am at currently is:

 var newAnomContact = new ContactInfo()
    ContactLastName = $"Anonymous - {DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")}"
 ICurrentContactProvider.SetCurrentContact(newAnomContact); //this is an instance of that interface
 consentAgreementService.Agree(newAnomContact, consent);

This does create a new contact that I would then expect to now be the current contact as I am using the current contact provider and setting the current contact but then my activities are still made against the user I just logged out.

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.