3

I'm tracking visitors information into my mongoDB and I can identify them as contacts (through the email). I'm now trying to have a login (with password) area where users can login and see some information. I'm not clear how can I do this? Can I still use my mongoDB Contact or is this different?

5
  • Are you looking to reveal the data from xDB to the user? Or are you wondering how to make a logged in user be associated to a user in xDB?
    – Chris Auer
    Nov 30, 2016 at 6:43
  • I reckon I'm clear on how to reveal the data to the user. I'm wondering how the login process works with mongoDB contacts?
    – Snapper
    Nov 30, 2016 at 6:59
  • The code below Tracker.Current.Session.Identify(domainUser); takes the user you just logged in as and links it to the contact that Sitecore has already created for the user in the CreateVistit pipeline.
    – Chris Auer
    Nov 30, 2016 at 7:04
  • Right - I can identify them through the email. But how can I get to validate their login with a password?
    – Snapper
    Nov 30, 2016 at 7:06
  • 1
    In the code below, Sitecore.Security.Authentication.AuthenticationManager.Login(domainUser, model.Password, model.RememberMe)) domain user being an email or a domain\user
    – Chris Auer
    Nov 30, 2016 at 7:10

1 Answer 1

5

Once you log the user in, you can process some out of the box Sitecore facets or your custom ones.

Setting the user's info in xDB

[HttpPost]
[AllowAnonymous]
public ActionResult Login(LoginModel model, string returnUrl)
{
  if (ModelState.IsValid)
  {
    Sitecore.Security.Domains.Domain domain = Sitecore.Context.Domain;
    string domainUser = domain.Name + @"\" + model.UserName;
    if (Sitecore.Security.Authentication.AuthenticationManager.Login(domainUser, model.Password, model.RememberMe))
    {
      AnalyticsHelper.SetVisitTagsOnLogin(domainUser, false);
      return RedirectToLocal(returnUrl);
    }
  }

  // If we got this far, something failed, redisplay form
  ModelState.AddModelError("", "The user name or password provided is incorrect.");
  return View(model);
}

A helper function to assign the user's data to the xDB facets.

public static void SetVisitTagsOnLogin(string domainUser, bool IsNewUser)
{
    string name = Sitecore.Context.User.Profile.FullName;
    if (name == String.Empty) name = Sitecore.Context.User.LocalName;
    Tracker.Current.Contact.Tags.Add("Username", domainUser);
    Tracker.Current.Contact.Tags.Add("Full name", name);

    Tracker.Current.Contact.Identifiers.AuthenticationLevel = AuthenticationLevel.PasswordValidated;
    Tracker.Current.Session.Identify(domainUser);

    if (IsNewUser)
    {
        IContactPersonalInfo personalFacet = Tracker.Current.Contact.GetFacet<IContactPersonalInfo>("Personal");
        personalFacet.FirstName = GetFirstName(name);
        personalFacet.Surname = GetSurName(name);

        IContactEmailAddresses addressesFacet = Tracker.Current.Contact.GetFacet<IContactEmailAddresses>("Emails");
        IEmailAddress address;
        if (!addressesFacet.Entries.Contains("work_email"))
        {
            address = addressesFacet.Entries.Create("work_email");
            address.SmtpAddress = GetEmailAddressFromUser(domainUser);
            addressesFacet.Preferred = "work_email";
        }
    }
}

Getting the user's info out of xDB

The benefit of xDB is that all the user's facet data is on there current contact object. Just call for the facet to get the data.

var contact = Tracker.Current.Contact;
var emailAddresses = contact.GetFacet<IContactEmailAddresses>("Personal");

You can find some more information about getting the facet data here. http://sitecore-community.github.io/docs/xDB/contact-facets/

As described on this page, https://doc.sitecore.net/sitecore_experience_platform/setting_up__maintaining/xdb/contacts/identifying_contacts. The domain user in the code above can be:

  • A user login name
  • A user ID in a security system
  • An email address
4
  • I understand the concept of facets and the possibility of extend them. What is confusing me is where/how I store the domainUser. Is this different from the xDB contact? Is it a sitecore user (extranet)?
    – Snapper
    Nov 30, 2016 at 6:58
  • 1
    Please see this link. doc.sitecore.net/sitecore_experience_platform/…. The domain name can be a email, domain/username or the Guid of the user.
    – Chris Auer
    Nov 30, 2016 at 7:06
  • So on the registration process I'll need to create a Sitecore.Security.Accounts.User user and then I can use the same email/user to identify it against the contact on xDB?
    – Snapper
    Nov 30, 2016 at 7:13
  • 1
    Yes, create the user and then call SetVisitorTagsOnLogin in the code above. That will link the new user and add emails into xDB. stackoverflow.com/questions/11840886/…
    – Chris Auer
    Nov 30, 2016 at 7:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.