Copied to clipboard

Flag this post as spam?

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


  • Mark 255 posts 612 karma points
    Sep 02, 2010 @ 18:36
    Mark
    1

    Member Login

    Hi

    I've been pulling my hair out all day trying to get a member login working. In absence of any documentation on how to do it, here's what I've done:

    I have a user control with a username, password and submit button. Inside the submit button's click event handler I am getting the member object using Member.GetMemberFromLoginAndEncodedPassword and have verfied the returned member object is not null. Then I call Member.AddMemberToCache passing in the member object.

    When I then look in the cookies for the page I see  the following cookies: yourAuthCookie, umbracoMemberLogin, umbracoMemberGuid, umbracoMemberId, umbPanel_pHeight and umbPanel_pWidth.

    I assume so far so good at this point. I've also verified the member object is stored in the cache.

    Then, after the page loads I have another logout button. Inside the event handler for the button I call Member.GetCurrentMember, but this always returns a null object. I used reflector to see what this method does and saw that it makes a call to Member.CurrentMemberId, which checks the boolean value HttpContext.Current.User.Identity.IsAuthenticated, however, when debugging I can see that HttpContext.Current.User is null.

    Can anyone shed any light on what might be going on here? Am I missing some Forms Authentication or umbraco provider specific configuration that isn't set up out of the box?

    I've tried this on umbraco 4.5.1 and 4.5.2

  • Dirk De Grave 4541 posts 6021 karma points MVP 3x admin c-trib
    Sep 02, 2010 @ 21:34
    Dirk De Grave
    0

    Hi Marc,

    I'd strongly advice you to using the standard asp.net login controls for member authentication.

    Have a look at Morten's blog post about integrating member authentication into an umbraco site: http://www.mortenbock.dk/blog/2009/04/01/setting-up-membership-in-umbraco.aspx. It's a simple tutorial that all teach you the basics of setting up your authentication system.

    Feel free to ask more questions if you still have some after heaving read and followed Morten's article.

     

    Cheers,

    /Dirk

  • Hendrik Jan 71 posts 137 karma points
    Sep 03, 2010 @ 00:05
    Hendrik Jan
    0
    Using the default user control should work just fine,

    but if you realley want to check the user by yourself:

    using System.Web.Security; if( System.Web.Security.Membership.ValidateUser(username, password) )
    {
      FormsAuthentication.SetAuthCookie(username, true);
    }
  • Mark 255 posts 612 karma points
    Sep 03, 2010 @ 14:54
    Mark
    0

    Thanks for your help Neils and Dirk.

    Neils, I have tried using the code to check the user myself using the code you provided, Neils. But the FormsAuthenticationModule still doesn't create the HttpContext.Current.User object upon subsequent page requests. The object is null. I was under the impression the FormsAuthenticationModule automatically detected the authentication cookie and created the HttpContext.Current.User object? Have I missed something that needs to be done on subsequent page requests?

  • Masood Afzal 176 posts 522 karma points
    Sep 03, 2010 @ 15:15
    Masood Afzal
    0

            private void LoginUser(string username, string password)
            {
                bool useSession=true;   
                int timeout=20;
                Member loginMember = Member.GetMemberFromLoginNameAndPassword(username, password);
                if (loginMember != null)
                {
                    Member.AddMemberToCache(loginMember, useSession, new TimeSpan(0, timeout, 0));
                }
                else
                {
                   // Not logged in
                }
            }

  • Masood Afzal 176 posts 522 karma points
    Sep 03, 2010 @ 15:17
    Masood Afzal
    0

    Have you tried using asp.net Login control?

     

  • Mark 255 posts 612 karma points
    Sep 03, 2010 @ 15:34
    Mark
    0

    Thanks Masood. But this is where I get confused. Should I use the traditional ASP.NET FormsAuthentication methods or the umbraco umbraco.cms.businesslogic.member namespace (assuming the UmbracoMembershipProvider is a wrapper around the umbraco.cms.businesslogic.member namespace)?

    I was able to log in using the method you mentioned, except that I used the Member.GetMemberFromLoginAndEncodedPassword and encoded the password using the umbraco.providers.members.UmbracoMembershipProvider EncodePassword method. After calling the Member.AddMemberToCache(member) method (not using the AddMemberToCache(Member, bool, TimeSpan) overload) I saw the member record in the cache, and also saw the cookies I mention in my first post. But upon subsequent page requests I was unable to retrieve the logged in Member object when calling Member.GetCurrentMember(). That's when I noticed the HttpContext.Current.User object was not created (was null).

    Perhaps I am missing something glaringly obvious. I haven't used the ASP.NET Login control. I want to have a login control on my homepage, also on a second document type, as they are pages that will often be landed on and I just don't want to have to get the user to go to the login page. The only time I need to redirect to a separate login page is when a particular document type is requested (not the homepage or second document type I mention above) and a user is not logged in. This has all come about because this is the way the site has been designed, which was nothing to do with me! :)

    I'm still unable to access the logged in member from any page after performing the login stage. Any help you can provide is very much appreciated...

  • Dirk De Grave 4541 posts 6021 karma points MVP 3x admin c-trib
    Sep 03, 2010 @ 15:41
    Dirk De Grave
    0

    why don't you just add that asp.net login control onto those 2 specific pages (home page and second doc type pages) if most people land on those pages. 

     

    using the asp.net login control, you don't need to do anything additional, it''ll just work out of the box, as umbraco's authentication system is based on asp.net membership, you should really avoid using the member api directly (also because of performance)

     

    Hope this helps.

    Regards,

    /Dirk

  • Dirk De Grave 4541 posts 6021 karma points MVP 3x admin c-trib
    Sep 03, 2010 @ 15:42
    Dirk De Grave
    0

    Btw, first sentence should read as:

    why don't you just add that asp.net login control onto the template for those 2 specific pages (home page and second doc type pages) if most people land on those pages. 

     

    /Dirk

  • Masood Afzal 176 posts 522 karma points
    Sep 03, 2010 @ 15:49
    Masood Afzal
    0

    This code works fine for me though i would also suggest using asp.net login control. With asp.net login control, you don't need to call FormAuthenticate method.

    I have also noticed some issues with using umbraco api e.g. if logged in using umbraco api, then asp.net LoginName control doesn't display login name correctly.

    As noted by Dirk, the best choice is using asp.net login control (its just matter of droping a control on your web form)  and works perfectly for umbraco & .net.

    Regards,

    Masood

     

     

  • Mark 255 posts 612 karma points
    Sep 03, 2010 @ 15:52
    Mark
    0

    Thanks Dirk,

    I'll give it a whirl.

    I suspect my next question though, will be, how do I get at the umbraco.cms.businesslogic.member.Member object from the Principal object exposed via the login control so that I can see properties like the full name (Member.Text), as well as custom MemberType properties. Can't say I've investigated it yet...

  • Masood Afzal 176 posts 522 karma points
    Sep 03, 2010 @ 15:56
    Masood Afzal
    0

    You can use 'Member.GetCurrentMember()' and other umbraco api calls after logging in using asp.net login control.

  • Dirk De Grave 4541 posts 6021 karma points MVP 3x admin c-trib
    Sep 03, 2010 @ 16:15
    Dirk De Grave
    0

    Hi Mark,

    this is what you need: http://www.aaron-powell.com/umbraco-members-profiles

     

    Hope this helps.

    Regards,

    /Dirk

  • Mark 255 posts 612 karma points
    Sep 03, 2010 @ 18:07
    Mark
    0

    Hey guys. First I want to thank you all for your help so far. Unfortunately I'm still having what appears to be the same issue.

    Within the CMS I have done the following:

    1. I have created a Member Type with alias called TestType (haven't added any additional properties)
    2. I've created a Member of the alias type TestType

    On my home page I have created the following control structure:

            <asp:LoginView ID="LoginViewUmbraco" runat="server">
                <AnonymousTemplate>
                    <asp:Login runat="server" />
                </AnonymousTemplate>
                <LoggedInTemplate>
                    Welcome
                </LoggedInTemplate>
            </asp:LoginView>

    I've got no code behind hooked into any events for the controls, nor do I have any code interacting with the control properties or methods.

    I've then tried the following:

    1. I've tried loggin in with a member that does not exist. I get an error message within the control saying "Your login attempt was not successful. Please try again."
    2. I've tried loggin in again, this time with the member I created within the CMS. In this case the AnonymousTemplate is still displayed after postback (with username & password textboxes, remember me checkbox and login button still displayed). The Welcome text does not get displayed. The authentication cookie does appear in the browser cookies. But any attempt to get the logged in Member or HttpContext.Current.User in a subsequent request fails.

    Any ideas why the cookie would be created, but the FormsAuthenticationModule would fail to instantiate the user on subsequent page requests? I've spent two days on this... If not, can anyone verify that they do have this type of setup working on an umbraco version 4.5.1 and / or 4.5.2 installation?

    Thanks!

  • Dirk De Grave 4541 posts 6021 karma points MVP 3x admin c-trib
    Sep 03, 2010 @ 18:50
    Dirk De Grave
    0

    you're doing ok, just one minor thing you've forgotten: you need to change a settings in web.config to reflect the name of your member type

    <membership defaultProvider="UmbracoMembershipProvider" userIsOnlineTimeWindow="15">
          <providers>
            <clear />
            <add name="UmbracoMembershipProvider" type="umbraco.providers.members.UmbracoMembershipProvider" enablePasswordRetrieval="false" enablePasswordReset="false" requiresQuestionAndAnswer="false" defaultMemberTypeAlias="Another Type" passwordFormat="Hashed" />
            <add name="UsersMembershipProvider" type="umbraco.providers.UsersMembershipProvider" enablePasswordRetrieval="false" enablePasswordReset="false" requiresQuestionAndAnswer="false" passwordFormat="Hashed" />
          </providers>
        </membership>

    Just change this bit

    defaultMemberTypeAlias="Another Type"

    into

    defaultMemberTypeAlias="TestType"

    and you should be up and running. If not, let us know what's stilll not working.

     

    Cheers,

    /Dirk

     

     

  • Hendrik Jan 71 posts 137 karma points
    Sep 03, 2010 @ 20:40
    Hendrik Jan
    0

     

    using my login code from the last page an using mazoods code to get member wil work. I have used this on 4.5.1 before

    using System.Web.Security;
    
    if( System.Web.Security.Membership.ValidateUser(username, password) )
    {
      FormsAuthentication.SetAuthCookie(username, true);
    }
    using umbraco.cms.businesslogic.member;
    Member member = Member.GetCurrentMember();  

     

  • Mark 255 posts 612 karma points
    Sep 04, 2010 @ 12:45
    Mark
    0

    Thanks Dirk, but I had already updated the parameter in the weblconfig file:

    defaultMemberTypeAlias="Another Type"

    Setting it to TestType. Still not able to retrieve the curent member using HttpContext.Current.User or Member.GetCurrentMember(). The authentication cookie is there after login. I'm wondering if it coulld be an issue with IIS, or the fact that I'm using localhost (port 80), but I'm clutching at straws really. No idea what the problem is.

    Same with Neils example, after calling SetAuthCookie the cookie exists, but again, Member.GetCurrentMember() returns a null object. The Member.CurrentMemberId() method that is called by GetCurrentMember() is getting a null reference from HttpContext.Current.User when trying to check if  IsAuthenticated == true. I'm totally stumped... :o(

    Thanks for all your help. Any other ideas are more than welcome. Maybe I'm going to have to store the member id in a session variable...

  • Mark 255 posts 612 karma points
    Sep 04, 2010 @ 17:18
    Mark
    1

    Well I seem to have finally solved the problem, although I'm not sure why what I've done fixes the issue as it is beyond my knowledge of ASP.NET and IIS...

    My development machine is Vista running IIS 7.0. The umbraco web applicaton application pool running in Integrated managed pipeline mode.

    I found the following ASP.NET forum post:

    http://www.weask.us/entry/authentication-problem-windows-vista-cookie-written-httpcontext-current-user-null

    This suggests switching to classic managed pipeline mode, which is not an option, at least when running umbraco with appsetting:

    <add key="umbracoUseDirectoryUrls" value="true" />

    So I looked at some other blog posts regarding forms authentication and managed pipeline mode issues. In a couple of places the suggestion was to remove and add several modules within the system.webServer section of the web.config file:

            <remove name="FormsAuthenticationModule" />
            <add name="FormsAuthenticationModule" type="System.Web.Security.FormsAuthenticationModule" />
            <remove name="UrlAuthorization" />
            <add name="UrlAuthorization" type="System.Web.Security.UrlAuthorizationModule" />
            <remove name="DefaultAuthentication" />
            <add name="DefaultAuthentication" type="System.Web.Security.DefaultAuthenticationModule" />

    After doing this I was able to successfully call Member.GetCurrentMember() as well as obtain a reference to HttpContext.Current.User upon subsequent requests to pages.

    Perhaps a better ASP.NET professional than I can shed some light on what the issue was and why this fixed it. The issues I experienced may also not be an issue on other OS / IIS installations, hence Dirk, Neils and Masood not being able to repeat the same issue.

    Anyway, a problem solved, and its solution is documented here. 3 days on and I can now continue developing my customer's website... :-)

  • David Freitas 10 posts 31 karma points
    Mar 31, 2014 @ 02:01
    David Freitas
    1

    Check that your IIS process e.g. IIS_IUSRS has permission to the App_Data folder.

Please Sign in or register to post replies

Write your reply to:

Draft