1

Using Sitecore 10.1.2

I would like to log the CMS user information like username, email, and logged-in timestamp as soon as a user logs into the CMS.

The details would be captured in a custom table with some business logic.

We have both native CMS & Azure AD accounts.

Could you please suggest the method, processor or pipeline where I can add my custom logic?

Update:

Following the suggestion, I have implemented this.

These are the existing settings from ShowConfig.aspx.

enter image description here

My patch config:

<?xml version="1.0"?>
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
  <sitecore>
    <pipelines>
      <processors>
        <loggedin argsType="Sitecore.Pipelines.LoggedIn.LoggedInArgs">
          <processor mode="on" type="MyProject.Foundation.Accounts.CMS.CmsLoginAudit.SaveAuditLog, MyProject.Foundation.Accounts" resolve="true"/>
        </loggedin>
      </processors>
    </pipelines>
  </sitecore>
</configuration>

And the class:

namespace MyProject.Foundation.Accounts.CMS
{
    public class CmsLoginAudit
    {
        public void SaveAuditLog()
        {
            if (Context.IsLoggedIn)
            {
                User user = Context.User;
                MembershipUser membershipUser = Membership.GetUser(user.Name);
                var userId = membershipUser?.ProviderUserKey;
                var userIp = GetUserIpAddress();
                string userEmail = membershipUser.Email;
            }
        }
    }
}

The ShowConfig.aspx does include the the patch config settings.

enter image description here

But the debugger isn't hitting the SaveAuditLog method. What am I missing here.

3 Answers 3

2

I think you need a processor in the loggedin pipeline. This should be executed after each login.

<processors>
   <loggedin>
     <processor mode="on" type="...." resolve="true"/>
   </loggedin>
</processors>
1

Your SaveAuditLog method is not triggered due to the pipeline/processor execution order. If you check with /showconfig.aspx, <owin.cookieAuthentication.signedIn> pipeline related processor gets executed as by default identity server is enabled.

You can disable the identity server if not using Sitecore.Owin.Authentication.IdentityServer.Disabler.config.example from \App_Config\Include\Examples\ Rename the file to .config

Also, update the class to use LoggedInProcessor.

 public class CmsLoginAudit : LoggedInProcessor
    {
        private readonly BaseAuthenticationManager authenticationManager;
        public BaseLog Log { get; }


        public CmsLoginAudit(BaseAuthenticationManager authenticationManager, BaseLog log)
        {
            this.authenticationManager = authenticationManager;
            Log = log;
        }

        public override void Process(LoggedInArgs args)
        {
            Assert.ArgumentNotNull(args, "args");
            Sitecore.Security.Accounts.User activeUser = authenticationManager.GetActiveUser();
            Log.Info(string.Format("Email {0} UserName {1} LoggedInTime {2}", activeUser.Profile.Email, activeUser.Profile.UserName, Sitecore.DateUtil.ToIsoDate(System.DateTime.Now)), this);
        }
    }

I'm just trying to log the info here and the result looks like this:

30576 18:17:44 INFO Email [email protected] UserName sitecore\TestUser LoggedInTime 20230116T1817441

patch file:

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
    <sitecore>
        <processors>
            <loggedin>
                <processor patch:before="processor[position()=last()]" method="Process"  mode="on" type="ExploreSitecore.Feature.Test.CmsLoginAudit, ExploreSitecore.Feature.Test" resolve="true"/>
            </loggedin>
        </processors>
    </sitecore>
</configuration>


If you are using the identity server then <owin.cookieAuthentication.signedIn> works fine.

2
  • Sorry, could be a dumb question but how do I decide whether to enable or disable identityserver. We have logins from AzureAD as well. Also, isn't identityserver a default for native CMS login?
    – sukesh
    Jan 17 at 8:53
  • 1
    Sitecore Identity (SI) is a mechanism to log in to Sitecore. It was introduced in Sitecore 9.1. It builds on the Federated Authentication functionality introduced in Sitecore 9.0 and the Sitecore Identity server, which is based on IdentityServer4. It provides a separate identity provider and allows you to set up SSO (Single Sign-On) across Sitecore services and applications. If you are not using any sso/federated authentication, you can disable it. Yes, Sitecore 9.1.0 and later enabled the Sitecore Identity by default. Jan 17 at 10:18
0

Calling my class like this, worked for me

<?xml version="1.0"?>
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
  <sitecore>
    <pipelines>
      <owin.cookieAuthentication.signedIn>
        <processor  patch:before="*[@type='Sitecore.Owin.Authentication.Pipelines.CookieAuthentication.SignedIn.CreateTicket, Sitecore.Owin.Authentication']"
            type="MyProject.Foundation.Accounts.CMS.CmsLoginAudit.SaveAuditLog, MyProject.Foundation.Accounts" resolve="true"/>
      </owin.cookieAuthentication.signedIn>
    </pipelines>
  </sitecore>
</configuration>

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.