Skip to main content

Fix for Umbraco GetCurrentMember System.InvalidOperationException: No member object found

It is possible that you don't store your member data in Umbraco database any longer and you have got an external service that provides your member data, so that your web users can login to your website using this external service. If this is the case then you might see the following error happening in the background which is also causing some performance issues.

An error occurred in GetCurrentMember. System.InvalidOperationException: No member object found with username [email protected] at umbraco.cms.businesslogic.member.Member.GetCurrentMember()




This exception is most likely happening due to your project references to built in Umbraco membership provider, hence first step should be checking your Umbraco project's Web.config for UmbracoMembershipProvider. If the setting is similar to the following, then this is the reason that you see these exceptions. In that case, follow the 3 steps below to fix this problem.

<add name="UmbracoMembershipProvider" type="Umbraco.Web.Security.Providers.MembersMembershipProvider, Umbraco" minRequiredNonalphanumericCharacters="0" minRequiredPasswordLength="10" useLegacyEncoding="false" enablePasswordRetrieval="false" enablePasswordReset="false" requiresQuestionAndAnswer="false" defaultMemberTypeAlias="Member" passwordFormat="Hashed" allowManuallyChangingPassword="false" />

Step 1: Custom membership provider: Create a new custom Membership provider, derive it from Umbraco's MembersMembershipProvider. Override all the methods and only update the methods that you need according to your project's needs. For my case, I only need GetUser method to return the logged in user data.

using System; using System;

using System.Linq;

using System.Web.Security;

using Umbraco.Web.Security.Providers;

 

namespace MYPROJECT.Web.Presentation.Security

{

    /// <summary>

    /// Custom membership provider that we use to replace the normal Umbraco one as we are not storing any members in the standard Umbraco membership system.

    /// Instead members are logging in using SSO and using claims based auth, claims are provided by both MYPROJECT SSO and additionally come from Crm Api

    /// It is almost entirely unimplemeted apart from the GetUser method which is called by various umbraco functions especially from a few places in Umbraco Forms

    /// All other methods will throw a NotImplemented Exception

    /// </summary>

    /// <remarks>

    /// We have to inherit the Umbraco membership provider rather than the standard System.Web.Security.Membership otherwise umbraco roles don't work

    /// when protecting content using Umbraco public access UI due to this Umbraco issue https://github.com/umbraco/Umbraco-CMS/issues/5469

    /// </remarks>

    public class MYPROJECTMembershipPvovider : MembersMembershipProvider

    {

        /// <summary>

        /// Returns a user based on the username.

        /// As we do not have any backing store to look up user details this will only ever return a user if username requested is the currently logged in user.

        /// In this case we can get the details from the current claims principle, otherwise we will return null

        /// </summary>

        /// <param name="username"></param>

        /// <param name="userIsOnline"></param>

        /// <returns></returns>

        public override MembershipUser GetUser(string username, bool userIsOnline)

        {

            //If no username then just return null

            if (string.IsNullOrWhiteSpace(username))

            {

                return null;

            }

 

            //check requested user is the same as the current logged in user

            if (System.Security.Claims.ClaimsPrincipal.Current?.Identity?.Name == username)

            {

                var cp = System.Security.Claims.ClaimsPrincipal.Current;

 

                //Try and extract crm contactId from the users claims (where we put it during login)

                var crmIdClaim = cp.Claims.FirstOrDefault(i => i.Type == Services.IdentityService.CrmContactIdClaim)?.Value;

                Guid? crmContactId = null;

                if (crmIdClaim != null)

                {

                    crmContactId = Guid.Parse(crmIdClaim);

                }

 

                var user = new MYPROJECTMembershipUser(

                            Membership.Provider.Name, //Providername

                            cp.Claims.FirstOrDefault(i => i.Type == "name")?.Value, //Name

                            crmContactId, // provideruserkey

                            cp.Claims.FirstOrDefault(i => i.Type == Services.IdentityService.EmailClaim)?.Value, //email

                            null, //password question

                            null, //comment

                            true, //isApproved

                            false, //isLockedOut

                            DateTime.MinValue, //creation date 

                            DateTime.Now, //last login date

                            DateTime.Now, //last activity date

                            DateTime.MinValue, //last pasword change

                            DateTime.MinValue, //last lockout date

                            crmContactId);

 

                return user;

            }

            else

            {

                return null;

            }

        }

 

        #region #region NotImplemented Properties and Methods

 

        public override string DefaultMemberTypeAlias

        {

            get

            {

                throw new NotImplementedException();

            }

        }

 

        public override MembershipUser GetUser(object providerUserKey, bool userIsOnline)

        {

            throw new NotImplementedException();

        }

 

        public override bool ChangePassword(string username, string oldPassword, string newPassword)

        {

            throw new NotImplementedException();

        }

 

        public override bool ChangePasswordQuestionAndAnswer(string username, string password, string newPasswordQuestion, string newPasswordAnswer)

        {

            throw new NotImplementedException();

        }

 

        public override MembershipUser CreateUser(string username, string password, string email, string passwordQuestion, string passwordAnswer, bool isApproved, object providerUserKey, out MembershipCreateStatus status)

        {

            throw new NotImplementedException();

        }

 

        public override bool DeleteUser(string username, bool deleteAllRelatedData)

        {

            throw new NotImplementedException();

        }

 

        public override MembershipUserCollection FindUsersByEmail(string emailToMatch, int pageIndex, int pageSize, out int totalRecords)

        {

            throw new NotImplementedException();

        }

 

        public override MembershipUserCollection FindUsersByName(string usernameToMatch, int pageIndex, int pageSize, out int totalRecords)

        {

            throw new NotImplementedException();

        }

 

        public override MembershipUserCollection GetAllUsers(int pageIndex, int pageSize, out int totalRecords)

        {

            throw new NotImplementedException();

        }

 

        public override int GetNumberOfUsersOnline()

        {

            throw new NotImplementedException();

        }

 

        public override string GetPassword(string username, string answer)

        {

            throw new NotImplementedException();

        }

 

        public override string GetUserNameByEmail(string email)

        {

            throw new NotImplementedException();

        }

 

        public override string ResetPassword(string username, string answer)

        {

            throw new NotImplementedException();

        }

 

        public override bool UnlockUser(string username)

        {

            throw new NotImplementedException();

        }

 

        public override void UpdateUser(MembershipUser user)

        {

            throw new NotImplementedException();

        }

 

        public override bool ValidateUser(string username, string password)

        {

            throw new NotImplementedException();

        }

 

        protected override MembershipUser PerformCreateUser(string memberTypeAlias, string username, string password, string email, string passwordQuestion, string passwordAnswer, bool isApproved, object providerUserKey, out MembershipCreateStatus status)

        {

            throw new NotImplementedException();

        }

 

        protected override bool PerformChangePassword(string username, string oldPassword, string newPassword)

        {

            throw new NotImplementedException();

        }

 

        protected override bool PerformChangePasswordQuestionAndAnswer(string username, string password, string newPasswordQuestion, string newPasswordAnswer)

        {

            throw new NotImplementedException();

        }

 

        protected override string PerformGetPassword(string username, string answer)

        {

            throw new NotImplementedException();

        }

 

        protected override string PerformResetPassword(string username, string answer, string generatedPassword)

        {

            throw new NotImplementedException();

        }

 

        #endregion NotImplemented Properties and Methods

    }

}

Step 2: Custom Membership User: Create your custom membership user.

using System;

using System.Web.Security;

 

namespace MYPROJECT.Web.Presentation.Security

{

    /// <summary>

    /// Represents a logged in MYPROJECT user, inherits standard membership user and adds additional fields that we need populated from claims

    /// </summary>

    public class MYPROJECTMembershipUser : MembershipUser

    {

        public Guid? CrmContactId { get; private set; }

 

        public MYPROJECTMembershipUser(string providerName,

                                       string name,

                                       object providerUserKey,

                                       string email,

                                       string passwordQuestion,

                                       string comment,

                                       bool isApproved,

                                       bool isLockedOut,

                                       DateTime creationDate,

                                       DateTime lastLoginDate,

                                       DateTime lastActivityDate,

                                       DateTime lastPasswordChangedDate,

                                       DateTime lastLockoutDate,

                                       Guid? crmContactId) : base(

                                           providerName, name, providerUserKey, email, passwordQuestion, comment,

                                           isApproved, isLockedOut, creationDate, lastLoginDate, lastActivityDate,

                                           lastPasswordChangedDate, lastLockoutDate)

        {

            CrmContactId = crmContactId;

        }

    }

}

Step 3: Change in the Umbraco project's Web.config: Change UmbracoMembershipProvider to use your custom membership provider class.

  <add name="UmbracoMembershipProvider" type="MYPROJECT.Web.Presentation.Security.MYPROJECTMembershipPvovider, MYPROJECT.Web.Presentation" />


That is all, hope this helps!

Comments

Popular posts from this blog

How to fix Git push error: "RPC failed; curl 56 HTTP/2 stream 7 was reset send-pack: unexpected disconnect while reading sideband packet fatal: the remote end hung up unexpectedly"

Problem Today I saw the following problem when I tried to push my changes to a Git server after doing some work for upgrading an Umbraco v7 project to v8.18.8.  Possible reasons After some investigations, it seems like this could be because of the following reasons; Git is not happy with the amount of changes that are being pushed into the server.  There are possible limitations on the server about the size/amount of files that you can push. Your internet connection is not good and stable enough. Your Git client's version is old. Solution options For me, the easiest option was connecting to another Wifi and trying again. Apparently, this option helped quite a few people, so it is worth giving it a try. Unfortunately, it didn't work for me. A bad internet connection wasn't an option for me either, as my internet is pretty fast (500 Mbps). Similarly, my Git client version was the latest version (git version 2.41.0.windows.3).  On StackOverflow, there were a lot of recommendat

How to use JQuery Ajax Methods for Async ASP.NET MVC Action Methods

Making repeatedly calls to async methods can be a nightmare. In this case, it makes sense to use 2 ajax methods, instead of one. Here is a simple solution to overcome this problem. See that  ajaxcalls   is emptied after the success response for the first ajax call and then the second ajax method is used to make one single call to the async action method. Hope it helps. View: @section Scripts{     < script type ="text/javascript">         var smartDebitObject = new Object();         smartDebitObject.MembershipNumber = $( "#MembershipNumber" ).val();         smartDebitObject.ProfileId = $( "#ProfileId" ).val();         smartDebitObject.FirstName = $( "#FirstName" ).val();         smartDebitObject.LastName = $( "#LastName" ).val();         smartDebitObject.AddressLine1 = $( "#AddressLine1" ).val();         smartDebitObject.Postcode = $( "#Postcode" ).val();         smartDebitObject

How to fix Umbraco v9 "Boot Failed : Umbraco failed to boot, if you are the owner of the website please see the log file for more details." error

If you have started working with Umbraco v9 and done your first Azure deployment to one of your testing environments, there is a possibility that you might see the following "Boot Failed" error. Error: Checking the logs In order to understand the problem, you should check the Umbarco log file.  The default location for this file is umbraco/Logs and this file contains the Machine name, along with the date information. You can reach this file via Azure's Kudu Service  or alternatively, you can get download your Azure App Service's publish profile and connect your App Service via an FTP application, i.e. FileZilla. See the FileZilla screen below; Once you get your log file, you can download it to your local machine and open it with a text editor, i.e. Notepad++. When you open it, you will see all logs, including the error message. Please be aware, as most things with Umbraco, logging is also customizable, so you can either use the default Umbraco logging which is Micros