Copied to clipboard

Flag this post as spam?

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


  • Ferdy Hoefakker 214 posts 248 karma points
    Feb 26, 2014 @ 15:22
    Ferdy Hoefakker
    0

    Login surface controller

    Hi all,

    I am trying to add login functionality to a page in Umbraco 7. However, when using the controller (Html.BeginUmbracoForm<UmbLoginController>) I ALWAYS get redirected to the homepage. Which I do not want. I need the user remain on the page he was at when he logged in. How do I do this?

    -Ferdy

  • David Brendel 792 posts 2970 karma points MVP 3x c-trib
    Feb 26, 2014 @ 15:52
    David Brendel
    0

    Do you get redirected when visiting the Login page or after the member has logged in?

    How does your controller look like?

    Normally I would save the url from which the user was redirected to login in a hidden field of my form and after sucessfull login redirect to the url of that page.

  • Ferdy Hoefakker 214 posts 248 karma points
    Feb 27, 2014 @ 09:42
    Ferdy Hoefakker
    0

    Sorry, other projects drew my attention for a bit.

    Anyway, the moment I hit the login button, I get logged in and redirected to the homepage. As for the controIer I am using, it's one supplied by Umbraco. This is the entire view (it's the default code you get when selecting login template when creating a partial view in the settings section):

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage

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


        @{
            var loginModel = new LoginModel();

            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");
        }

        @Html.RenderJsHere()

        @using (Html.BeginUmbracoForm<UmbLoginController>("HandleLogin"))
        {
            <fieldset>
                <legend>Login</legend>

                @Html.ValidationSummary(true)

                @Html.LabelFor(m => loginModel.Username)
                @Html.TextBoxFor(m => loginModel.Username)
                @Html.ValidationMessageFor(m => loginModel.Username)
                <br />

                @Html.LabelFor(m => loginModel.Password)
                @Html.PasswordFor(m => loginModel.Password)
                @Html.ValidationMessageFor(m => loginModel.Password)
                <br />

                <button>Login</button>
            </fieldset>
        }

    So yeah, the controller "UmbLoginController" is the controller I am using, but I do not know what the code looks like.

  • David Brendel 792 posts 2970 karma points MVP 3x c-trib
    Feb 27, 2014 @ 10:43
    David Brendel
    0

    Ok had a small look at the Umbraco source.

    The UmbLoginController just redirects your member to the homepage.

    Seems there is no way to change this behavior. So if you want more controller over this you have to implement the login on your own. Would recommend to have a look at Warren Buckleys package which is also available from nuget.

  • Ferdy Hoefakker 214 posts 248 karma points
    Feb 27, 2014 @ 10:52
    Ferdy Hoefakker
    0

    My colleague and I just came to the same conclusion and decided to go implement our own controller. Thanks for the suggestion though.

    -Ferdy

  • Ferdy Hoefakker 214 posts 248 karma points
    Feb 27, 2014 @ 14:55
    Ferdy Hoefakker
    1

    Sorry for the double post, but I wanted to add an additional question.

    I have the following model:

    public class BmMemberModel
        {
            [Required]
            public string Username { get; set; }
           
            [Required]
            public string Password { get; set; }

            public string loginRedirectUrl { get; set; }

            public string logoutRedirectUrl { get; set; }
        }


    And the following Controller:

    public class BmMemberSurfaceController : SurfaceController
        {
            [HttpPost]
            public ActionResult MemberLogin([Bind(Prefix = "BmMemberModel")]BmMemberModel model)
            {
                if (ModelState.IsValid)
                {
                    var m = Member.GetMemberFromLoginNameAndPassword(model.Username, model.Password);
                    if (m != null)
                        Member.AddMemberToCache(m);
                }

                return CurrentUmbracoPage();        
            }
        }

    And finally, my view:

    {
    var memberModel = new BmMemberModel();
    }
    @using (Html.BeginUmbracoForm<BmMemberSurfaceController>("MemberLogin"))
    {
        memberModel.loginRedirectUrl = "";
           
        <fieldset>
            <legend>Login</legend>

            @Html.ValidationSummary(true)

            @Html.LabelFor(m => memberModel.Username)
            @Html.TextBoxFor(m => memberModel.Username)
            @Html.ValidationMessageFor(m => memberModel.Username)
            <br />

            @Html.LabelFor(m => memberModel.Password)
            @Html.PasswordFor(m => memberModel.Password)
            @Html.ValidationMessageFor(m => memberModel.Password)
            <br />

            <button>Login</button>
        </fieldset>
    }

    Now, I am still somewhat new to the whole MVC thing, so maybe I am missing something obvious. But, whenever I hit submit, the model I get back is NULL and gives null reference exceptions in my controller. But, I kinda just changed the names of the version that is in Umbraco.

    What am I doing wrong/missing?

    -Ferdy

  • Ferdy Hoefakker 214 posts 248 karma points
    Feb 27, 2014 @ 16:53
    Ferdy Hoefakker
    0

    Okay, I figured out my problem! With the examples I read, it said that

    [Bind(Prefix="BmMemberModel")]

    would give all the elements in the form the prefix "BmMemberModel". This was wrong as I now found it. It means that it will only react to fields prefixed with "BmMemberModel". To get the fields prefixed, I just need to name the Model on the page "BmMemberModel".

    Sorry if this is obvious or anything, but like I said, I am pretty new to the whole MVC thing and it threw me for quite a loop!

    -Ferdy

  • Tajamal 87 posts 175 karma points
    Mar 09, 2016 @ 10:06
    Tajamal
    0

    Hi Fredy

    How does your complete solution look like. I am going through this right now and will need all heads up.

    Have you used Partial view or just view ? and i didn't understand the last thing with model, can you show it ?

  • Ferdy Hoefakker 214 posts 248 karma points
    Mar 09, 2016 @ 12:35
    Ferdy Hoefakker
    0

    Hi Tajamal,

    I'm sorry, this was a post from a little over 2 years ago. I do not have access to the code. Perhaps if you could post your view/model/controller we could help you figure it out?

    -Ferdy

  • Tajamal 87 posts 175 karma points
    Mar 09, 2016 @ 12:47
    Tajamal
    0

    Hi Ferdy

    Thanks

    I am migrating my code to umbraco at the time and soon as i reach there i may post something.

    /Tajamal

  • David Brendel 792 posts 2970 karma points MVP 3x c-trib
    Feb 27, 2014 @ 17:17
    David Brendel
    0

    Glad you solved your problem.

    Haven't used the [Bind]-thing before. New to me and don't know why you should use it. Will have a read on it.

Please Sign in or register to post replies

Write your reply to:

Draft