Get user created date in sitecore

2019-08-29 06:42发布

问题:

How to get created date of a user in sitecore. I am creating a script in c# to list out all sitecore users and its profile fields such as Last active date, Last Updated Date etc. I have got all users in a list using -

var users = Sitecore.Security.Accounts.UserManager.GetUsers();

foreach (Sitecore.Security.Accounts.User user in users)
{  
    ltLastActiveDate = user.Profile.LastActivityDate;
   ltCreatedDate = user.Profile // ? No idea
}

I have no idea how to get created date for user.

回答1:

Try it:

        var users = Sitecore.Security.Accounts.UserManager.GetUsers();
        foreach (Sitecore.Security.Accounts.User user in users)
        {
            var membershipUser = System.Web.Security.Membership.GetUser(user.Name, false);
            if (membershipUser != null)
            {
                var date = membershipUser.CreationDate;
            }
        }

cheers.



回答2:

The Sitecore user properties are really just complimentary to the standard .NET membership provider that already takes care of a lot of stuff like this. As such you should convert the Sitecore user (Sitecore.Security.Accounts.User) to a .NET user (System.Web.Security.MembershipUser) in order to find the creation date.

Here is an example:

System.Web.Security.MembershipUser membershipUser = System.Web.Security.Membership.GetUser(user.Name);
if (membershipUser != null)
{
    DateTime creationDate = membershipUser.CreationDate;
}


标签: c# sitecore