Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • nickornotto 397 posts 900 karma points
    Aug 23, 2017 @ 12:52
    nickornotto
    0

    Re-authenticate IMember after username/ email change

    I am changing username (email which is the same) of a IMember programatically in my umbraco app.

                if(model.Email != member.Email)
                {
                    member.Email = model.Email;
                    member.Username = model.UsernameIsEmail ? model.Email : model.Username;
                    updateLogin = true;
                }
    

    After I save the member's new username and email I cannot longer access member only pages and getting the following error:

    [NullReferenceException: Object reference not set to an instance of an object.]
       Umbraco.Web.Security.MembershipHelper.IsMemberAuthorized(Boolean allowAll, IEnumerable`1 allowTypes, IEnumerable`1 allowGroups, IEnumerable`1 allowMembers) +136
       Umbraco.Web.Security.WebSecurity.IsMemberAuthorized(Boolean allowAll, IEnumerable`1 allowTypes, IEnumerable`1 allowGroups, IEnumerable`1 allowMembers) +145
       Umbraco.Web.Mvc.MemberAuthorizeAttribute.AuthorizeCore(HttpContextBase httpContext) +385
       System.Web.Mvc.AuthorizeAttribute.OnAuthorization(AuthorizationContext filterContext) +160
       System.Web.Mvc.ControllerActionInvoker.InvokeAuthorizationFilters(ControllerContext controllerContext, IList`1 filters, ActionDescriptor actionDescriptor) +97
       System.Web.Mvc.Async.<>c__DisplayClass21.<BeginInvokeAction>b__19(AsyncCallback asyncCallback, Object asyncState) +743
       System.Web.Mvc.Async.WrappedAsyncResult`1.CallBeginDelegate(AsyncCallback callback, Object callbackState) +14
       System.Web.Mvc.Async.WrappedAsyncResultBase`1.Begin(AsyncCallback callback, Object state, Int32 timeout) +128
    ...
    

    How to re-authenticate logged user after username update?

    I tried recall

                if(updateLogin)
                {
                    _membershipHelper.Login(model.Username, model.Password);
                }
    

    but it didn't help.

    Thanks

  • Osman Coskun 164 posts 398 karma points
    Dec 20, 2017 @ 20:12
    Osman Coskun
    0

    Hello,

    Did you find a solution?

  • nickornotto 397 posts 900 karma points
    Jan 05, 2018 @ 16:06
    nickornotto
    0

    Nothing so far

  • Thomas Morris 35 posts 133 karma points MVP c-trib
    Aug 07, 2018 @ 14:39
    Thomas Morris
    1

    I had some success with this.

    Members.Logout();
    FormsAuthentication.SetAuthCookie(member.Username, true);
    

    Which effectively re-authenticates the user with the new username. If you look at the Umbraco source code you can see the login logic here:

    https://github.com/umbraco/Umbraco-CMS/blob/7ee510ed386495120666a78c61497f58ff05de8f/src/Umbraco.Web/Security/MembershipHelper.cs#L287

Please Sign in or register to post replies

Write your reply to:

Draft