Copied to clipboard

Flag this post as spam?

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


  • Fengelz 106 posts 221 karma points
    Jul 31, 2014 @ 12:44
    Fengelz
    0

    Members.Login

    Hola. I'm in the early stages of creating a site which will solely be for logged in members. I've spent a good amount of hours trying to get the login functionality working.

    I've tried the pre constructed macros in Umbraco the .NET membership class and now this:

    var login = Members.Login("test", "1234");
    <p>@login</p>
    if(login)
    {
        <p>@Members.IsLoggedIn()</p>
    
        <p>@Members.GetCurrentLoginStatus().IsLoggedIn</p>
    }
    

    This basically writes out:

    True

    False

    False

    I can see in the Members section in the backend that the test member is logged in by the "last login date" but I can't figure out how to check if the member is logged in persistantly across the pages.

    Shouldn't I be able to do it this simply? Or is there something I'm totally missing?

    Cheers - Sune

  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    Jul 31, 2014 @ 12:50
    Dennis Aaen
    0

    Hi Sune,

    If you are using Umbraco 7, you could take a look at the pre-defined Razor code snippets for a login form, and there is also a snippet that should give you the login status.

    The pre-define snippet for getting login status looks like this:

    @inherits Umbraco.Web.Macros.PartialViewMacroPage

    @using System.Web.Mvc.Html
    @using ClientDependency.Core.Mvc
    @using Umbraco.Web
    @using Umbraco.Web.Models
    @using Umbraco.Web.Controllers

    @{
        var loginStatusModel = Members.GetCurrentLoginStatus();

        Html.EnableClientValidation();
        Html.EnableUnobtrusiveJavaScript();
        Html.RequiresJs("/umbraco_client/ui/jquery.js");
        Html.RequiresJs("/umbraco_client/Application/JQuery/jquery.validate.min.js");
        Html.RequiresJs("/umbraco_client/Application/JQuery/jquery.validate.unobtrusive.min.js");

        var logoutModel = new PostRedirectModel();
       
        @*
            Here you can specify a redirect URL for after logging out, by default umbraco will simply
            redirect to the current page. Example to redirect to the home page:
           
            logoutModel.RedirectUrl = "/";
        *@
    }

    @* NOTE: This RenderJsHere code should be put on your main template page where the rest of your script tags are placed *@
    @Html.RenderJsHere()

    @if (loginStatusModel.IsLoggedIn)
    {
        <p>You are currently logged in as @loginStatusModel.Name</p>

        using (Html.BeginUmbracoForm<UmbLoginStatusController>("HandleLogout"))
        {
             <fieldset>
                 <legend>Logout</legend>
                 <button>Logout</button>
             </fieldset>
       
            @Html.HiddenFor(m => logoutModel.RedirectUrl)
        }
    }

    Hope this can pull you in the rigtht direction.

    /Dennis

  • Fengelz 106 posts 221 karma points
    Jul 31, 2014 @ 12:56
    Fengelz
    0

    Yeah, as I wrote, I tried those already without luck. :/ I implemented the loginstatus macro in my master template and the login macro on my login page but added

    @else { <p>You are not logged in </p> }
    

    after

    @if (loginStatusModel.IsLoggedIn){
    <p>You are currently logged in as @loginStatusModel.Name</p>
    
    using (Html.BeginUmbracoForm<UmbLoginStatusController>("HandleLogout"))
    {
         <fieldset>
             <legend>Logout</legend>
             <button>Logout</button>
         </fieldset>
    
        @Html.HiddenFor(m => logoutModel.RedirectUrl)
    }
    

    }

    And consequently got the message: You are not logged in

    Thanks for the quick reply though :)

  • Heather Floyd 610 posts 1033 karma points MVP 6x c-trib
    Apr 28, 2015 @ 02:53
    Heather Floyd
    0

    I am having a similar issue.

    I have a profile-editing page for a member. When the page loads, I know the member is logged in because when I set a breakpoint in the "RenderForm" code, I see that loginStatusModel.IsLoggedIn == TRUE and I am able to get their current member properties and populate the custom model for the form.

    However, when I make a change to the form data and click the "submit" button, triggering the "HandleSubmit" code, loginStatusModel.IsLoggedIn == FALSE.

    I just can't figure out why the member is getting logged-out on form submit. Am I missing something obvious here?

    Here is a simplified version of my code:

    MemberAccountForm Model

    public class MemberAccountForm
    {
        public string EmailLogin { get; set; }
    
        public string ContactName { get; set; }
    
        public string CompanyName { get; set; }
    
        ... other properties
    }
    

    MemberAccountFormController

    public class MemberAccountFormController : SurfaceController
    {
        [ChildActionOnly]
        public ActionResult RenderForm(IPublishedContent CurrentPage)
        {
            ...
            var loginStatusModel = Members.GetCurrentLoginStatus(); <----- This has data & is TRUE
    
            if (loginStatusModel.IsLoggedIn)
            {
                //Get the Current Member using MembershipHelper (cause it's faster)
                var membershipUsername = Members.GetCurrentMember().GetPropertyValue("Username").ToString();
    
                //Access Member using MemberService for property getting                   
                var m = ApplicationContext.Services.MemberService.GetByUsername(membershipUsername);
    
                var currMemProfile = new MemberAccountForm();
                currMemProfile.CompanyName = m.GetValue<string>("companyName");
                currMemProfile.ContactName = m.GetValue<string>("contactName");
                currMemProfile.EmailLogin = m.Username;
                currMemProfile.OriginalUserName = m.Username;
    
                return PartialView("MemberAccountForm", currMemProfile);
            }
            else
            {
                return PartialView("MemberAccountForm", new MemberAccountForm { });
            }
        }
    
        [HttpPost]
        [ActionName("HandleSubmit")]
        public ActionResult HandleSubmit(MemberAccountForm model)
        {
            if (ModelState.IsValid)
            {
                var loginStatusModel = Members.GetCurrentLoginStatus(); <!---- Now this is FALSE and all the values are NULL
    
                if (loginStatusModel.IsLoggedIn)
                {
                    ...Do stuff to update & save properties & add "success" message to ViewData
                }
    
            }
            else
            {
                //model not valid
                //Do not save, but return current umbraco page
    
                ...Add model errors to ViewData
            }
    
            //Add current form model to ViewBag
            ViewData["Model"] = model;
            return CurrentUmbracoPage();
    }
    

    "MemberAccountForm" partial view

    @using some stuff...
    @model MemberAccountForm
    
    @{
        ...Code to show errors & messages, if present in ViewData...
        @RenderForm()
    }
    
    @helper RenderForm()
    {
        if (User.Identity.IsAuthenticated)
        {
            using (Html.BeginUmbracoForm("HandleSubmit", "MemberAccountForm"))
            {
    
                    <label>
                        <span>Company Name</span>
                        @Html.EditorFor(x => Model.CompanyName, new { @type = "text" })
                    </label>
    
                    <label>
                        <span>Contact Name</span>
                        @Html.EditorFor(x => Model.ContactName, new { @type = "text" })
                    </label>
    
                    <label>
                        <span>Email Address</span>
                        @Html.EditorFor(x => Model.EmailLogin, new { @type = "email" })
                    </label>
    
                    ..some other fields...
    
                <button type="submit">Save</button>
            }
        }
    }
    

    Any suggestions appreciated!

    ~Heather

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies