Copied to clipboard

Flag this post as spam?

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


  • None 9 posts 99 karma points
    May 24, 2021 @ 04:52
    None
    0

    Umbraco member is not getting logged IN

    My code to log in an Umbraco member is like this

     if (Membership.ValidateUser(model.Username, model.Password))
            {
                FormsAuthentication.SetAuthCookie(model.Username, false);
                TempData["LoginSuccess"] = true;
                var log = Umbraco.MembershipHelper.Login(model.Username, model.Password);
                var mem = Membership.GetUser();
    
            }
    

    Although variable log shows true in debugging, variable mem returns as null only. What am I missing here?

  • Huw Reddick 1770 posts 6157 karma points MVP c-trib
    May 24, 2021 @ 07:30
    Huw Reddick
    1

    I'm not that familiar with Umbraco 7, but in 8 I generally do the following

    if(Members.Login(model.siemail, model.sipassword)){
            FormsAuthentication.SetAuthCookie(model.siemail, false);
            var member = Members.GetByUsername(model.siemail);
    }
    

    I have only ever used GetUser() in a view not in a controller

  • None 9 posts 99 karma points
    May 24, 2021 @ 07:37
    None
    0

    I believe we both are using same code to get he user logged

     FormsAuthentication.SetAuthCookie(model.siemail, false);
    

    But I need to get the current logged user not any user by his username. Hence used Membership.GetUser() . And for me in View also Membership.GetUser() is not working. Even Umbraco.MemberIsLoggedOn() is returning false for me in the view.

  • Huw Reddick 1770 posts 6157 karma points MVP c-trib
    May 24, 2021 @ 08:30
    Huw Reddick
    0

    My code is getting the user you that just logged in, just because it is getting them by username doesn't mean it is a different member.

    Like I said I have only ever used getuser() in a view and it works fine there, so maybe just doesn't work in a controller

  • None 9 posts 99 karma points
    May 24, 2021 @ 09:03
    None
    0

    As I explained above In my case the issue is that the member is not getting logged in, So there is no point in getting a member by username. But I appreciate you trying to answer my question :)

  • Huw Reddick 1770 posts 6157 karma points MVP c-trib
    May 24, 2021 @ 09:42
    Huw Reddick
    0

    Are you able to check that the cookies are being written?

  • Huw Reddick 1770 posts 6157 karma points MVP c-trib
    May 24, 2021 @ 10:00
    Huw Reddick
    0

    I believe the problem is because you are checking getuser immediately after writing the Auth ticket and this will not work as it has not been loaded as it requires a web request for the browser to load the ticket which is why it works in my view. If after login you redirect to another page or reload the login page I think you should find that getuser works as expected.

  • None 9 posts 99 karma points
    May 24, 2021 @ 12:56
    None
    0

    When login gets completed, I can see a new cookie named .ASPXAUTH

  • None 9 posts 99 karma points
    May 24, 2021 @ 13:02
    None
    0

    I have changed my code according to your suggestion

                    if (Membership.ValidateUser(model.Username, model.Password))
                {
                    FormsAuthentication.SetAuthCookie(model.Username, false);
                    return Redirect("/myprofile");
                }
    

    And tried to get the user in myprofile view. But still I am getting NULL for Membership.GetUser(). I have started thinking that I may be missing any important entry in webconfig or so. But I am not sure what all are the inportant entires in webconfig to make this member login work.

  • Huw Reddick 1770 posts 6157 karma points MVP c-trib
    May 24, 2021 @ 13:23
    Huw Reddick
    1

    do you have an ASP.Net _sessionId cookie as well ?

    These are the two cookies I get when logging in as a member

    enter image description here

  • None 9 posts 99 karma points
    May 24, 2021 @ 13:57
    None
    0

    This is how it looks for me

    enter image description here

  • None 9 posts 99 karma points
    May 31, 2021 @ 04:58
    None
    100

    My issue was that I was missing below section in web.config

          <authentication mode="Forms">
        <forms name="yourAuthCookie" loginUrl="login.aspx" protection="All" path="/" />
      </authentication>
    

    @reddick 's answer helped me to find this

  • Huw Reddick 1770 posts 6157 karma points MVP c-trib
    May 31, 2021 @ 11:54
    Huw Reddick
    0

    Glad you finally sorted it.

Please Sign in or register to post replies

Write your reply to:

Draft