Copied to clipboard

Flag this post as spam?

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


  • Christian Fischer 11 posts 122 karma points
    May 11, 2021 @ 10:16
    Christian Fischer
    0

    Member Login not working properly, login is just refreshed (codeshare example)

    Dear Umbraco community

    I'm new to Umbraco and MVC in general and I'm currently crawling the web for examples on how to setup certain things. For now, I got a basic page running and also collected some experience with SurfaceControllers and Models. Now I'm trying to integrate a Member Login for a multi-site scenario:

    enter image description here

    I started with the example from Paul Seal: https://codeshare.co.uk/blog/tutorial-how-to-create-member-login-and-logout-form-in-umbraco-mvc/

    My current model:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations;
    using System.Linq;
    using System.Web;
    
    namespace WebFrontend.Models
    {
        public class MemberLoginModel
        {
            [Display(Name = "Username")]
            [Required(ErrorMessage = "Please enter an username.")]
            public string Username { get; set; }
    
            [Display(Name = "Password")]
            [Required(ErrorMessage ="Please enter a password.")]
            [DataType(DataType.Password)]
            public string Password { get; set; }
    
            [Display(Name = "RememberMe")]
            public bool RememberMe { get; set; }
    
            [Display(Name = "RedirectUrl")]
            public string RedirectUrl { get; set; }
    
        }
    }
    

    And the corresponding surface controler:

    using WebFrontend.Models;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    using System.Web.Security;
    using Umbraco.Web.Mvc;
    
    namespace WebFrontend.Controllers
    {
        public class MemberController : SurfaceController
        {
            public ActionResult RenderLogin()
            {
                return PartialView("Members/_MemberLogin", new MemberLoginModel());
            }
    
            [HttpPost]
            [ValidateAntiForgeryToken]
            public ActionResult SubmitLogin(MemberLoginModel model, string returnUrl)
            {
                if (ModelState.IsValid)
                {
                    if (Membership.ValidateUser(model.Username, model.Password))
                    {
                        FormsAuthentication.SetAuthCookie(model.Username, model.RememberMe);
                        var myUser = System.Web.Security.Membership.GetUser(model.Username);
                        if (myUser != null)
                        {
                            returnUrl = myUser.Comment;
                        }
                            UrlHelper myHelper = new UrlHelper(HttpContext.Request.RequestContext);
                        if (myHelper.IsLocalUrl(returnUrl))
                        {
                            return Redirect(returnUrl);
                        }
                        else
                        {
                            return Redirect("/c500_portal/");
                        }
                    }
                    else
                    {
                        ModelState.AddModelError("", "The username or password provided is incorrect.");
                    }
                }
                return CurrentUmbracoPage();
            }
    
            public ActionResult RenderLogout()
            {
                return PartialView("Members/_MemberLogout", null);
            }
    
            public ActionResult SubmitLogout()
            {
                TempData.Clear();
                Session.Clear();
                FormsAuthentication.SignOut();
                return Redirect("/portallogin/");
            }
        }
    }
    

    And the partial view for the login page:

    @inherits Umbraco.Web.Mvc.UmbracoViewPage<WebFrontend.Models.MemberLoginModel>
    @using Umbraco.Web
    
    @if (!String.IsNullOrEmpty(Request.QueryString["signout"]))
    {
        FormsAuthentication.SignOut();
    }
    
    @if (!Umbraco.MemberIsLoggedOn())
    {
        using (Html.BeginUmbracoForm("SubmitLogin", "Member", System.Web.Mvc.FormMethod.Post, new { id = "login" }))
        {
    
            @Html.AntiForgeryToken()
    
            <fieldset>
                @Html.HiddenFor(m => m.RedirectUrl)
    
                <h4 class="ttm-textcolor-white">Platform Login</h4>
                <div>
                    @Html.TextBoxFor(m => m.Username, new { @class = "form-control", placeholder = "Username" })
                    @Html.ValidationMessageFor(m => m.Username, "", new { @class = "ttm-textcolor-white" })
                </div>
                <div>
                    @Html.PasswordFor(m => m.Password, new { @class = "form-control", placeholder = "Password" })
                    @Html.ValidationMessageFor(m => m.Password, "", new { @class = "ttm-textcolor-white" })
                </div>
                <div>
                    @Html.LabelFor(m => m.RememberMe, "", new { @class = "ttm-textcolor-white" })
                    @Html.CheckBoxFor(m => m.RememberMe, new { @class = "form-control" })
                </div>
                <div>
                    @Html.ValidationSummary(false, "", new { @class = "ttm-textcolor-white" })
                </div>
    
                <div class="clearfix"></div>
                <div class="mt-35">
                    <button class="ttm-btn ttm-btn-size-md ttm-btn-style-border ttm-btn-color-skincolor ttm-btn-bgcolor-white" name="login" type="submit">Login</button>
                </div>
                <div class="clearfix"></div>
                <div class="mt-35">
                    <a class="reset_pass  ttm-textcolor-white" href="mailto:info@xxx">Lost your password?</a>
                </div>
                <div class="clearfix"></div>
                <div class="separator">
                    <p class="change_link  ttm-textcolor-white">
                        New?
                        <a href="mailto:info@xxx" class="to_register  ttm-textcolor-white"> Get in contact! </a>
                    </p>
                </div>
                <div class="clearfix"></div>
            </fieldset>
    
        }
    }
    else
    {
        Html.RenderAction("RenderLogout", "Member");
    }
    

    When I enter the correct username and password, the login page is simply refreshed, nothing else happens.

    Can anyone please point me in the right direction ;)

    Thanks and have a good day! Chris

  • Christian Fischer 11 posts 122 karma points
    May 11, 2021 @ 13:57
    Christian Fischer
    0

    from web.config comment:

    <modules>
      <remove name="FormsAuthenticationModule" />
    </modules>
    

    Additional information:

    Stackoverflow: MVC 5 disabling Formsauthentication

  • Christian Fischer 11 posts 122 karma points
    May 11, 2021 @ 14:58
    Christian Fischer
    0

    Even with this fix, as soon as I re-enable the Site protection, it's repeating the login page after the redirect.

    Current Controller part looks like this:

    public ActionResult SubmitLogin(MemberLoginModel model, string returnUrl)
    {
        if (!ModelState.IsValid)
        {
            ModelState.AddModelError("", "There was an error, please try again.");
            return CurrentUmbracoPage();
        }
        else
        {
            if (Members.Login(model.Username, model.Password))
            {
                FormsAuthentication.SetAuthCookie(model.Username, model.RememberMe);
    
                var myUser = Members.GetByUsername(model.Username);
                if (myUser != null)
                {
                    returnUrl = myUser.GetProperty("PortalRedirectUrl").GetValue().ToString();
                }
                UrlHelper myHelper = new UrlHelper(HttpContext.Request.RequestContext);
                if (myHelper.IsLocalUrl(returnUrl))
                {
                    return Redirect(returnUrl);
                }
                else
                {
                    return Redirect("/c500_portal/");
                }
            }
            else
            {
                ModelState.AddModelError("", "The username or password provided is incorrect.");
            }
        }
        return RedirectToCurrentUmbracoUrl();
    }
    

    Members.Login() returns true but access to the restricted page is still not granted (login is reloading)

  • Christian Fischer 11 posts 122 karma points
    May 12, 2021 @ 09:14
    Christian Fischer
    0

    I got now deeper into debugging and tried also with the standard Macros provided for Login / Status:

    The login is working without any error (in my code Members.Login(model.Username, model.Password) results in TRUE and the cookies are created), but if I go to the status page and do a @loginStatusModel.IsLoggedIn, I get a FALSE

    Umbraco Version is 8.13 and I already did an 'Update-Package UmbracoCMS -Reinstall -Safe'

  • Christian Fischer 11 posts 122 karma points
    May 12, 2021 @ 16:14
    Christian Fischer
    0

    Few hours later... :)

    I went through all installed packages and found out, that the Nuget UmbracoIdentity was installed on an early version to integrate MS Authentication but no configuration was done. (Member Properties, as suggested on the git page) So I assumed, that this package leads to a misbehavior with the standard login and removed it.

    This broke my site with:

    The following errors occurred while attempting to load the app.
    - The OwinStartupAttribute.FriendlyName value 'UmbracoDefaultOwinStartup' does not match the given value 'UmbracoIdentityStartup' in Assembly
    

    I fixed this by replacing "UmbracoIdentityStartup" in the Web.config appSettings with:

    <add key="owin:appStartup" value="UmbracoDefaultOwinStartup" />
    

    But the login is still not working.

  • Christian Fischer 11 posts 122 karma points
    May 13, 2021 @ 09:06
    Christian Fischer
    101

    The issue was the NuGet Package UmbracoIdentity. As soon as you install this package, classical Umbraco Identity Provider is broken and uninstalling does not fix the issue. I ended up first starting to customize UmbracoIdentity to fit my layout but with every update I have to merge the customizations.

    Now I started over with a complete and fresh reinstall of Umbraco and transfered all my custom code again. Now its working flawlessly.

    Lessons learned:

    • If you want to use 3rd party auth with UmbracoIdentity, you have to use this solution for the whole Login procedure as soon as its installed. Make sure to follow also the installation instructions on the git page

    • If you don't want to use this, never install it or it gets messsy (uninstall is nor working properly and Umbraco gets broken)

    • Also the NuGet MembersListView is making problems with the Umbraco Version I have installed (8.13), I didn't dig deeper in this.

    Glad if this helps anyone struggling with the same issues than I did :)

    Occured Error (Messages):

    • Login not working, login page is always reloading, LoggedIn Status is false

    • UmbracoIdentity Uninstall Error:

      The following errors occurred while attempting to load the app. - The OwinStartupAttribute.FriendlyName value 'UmbracoDefaultOwinStartup' does not match the given value 'UmbracoIdentityStartup' in Assembly

    • Umbraco complained about a lot of missing libraries after installing MembersListView NuGet

Please Sign in or register to post replies

Write your reply to:

Draft