Copied to clipboard

Flag this post as spam?

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


  • Ayo Adesina 445 posts 1059 karma points
    Jun 12, 2024 @ 14:27
    Ayo Adesina
    0

    Umbraco Membership, creating a login Screen. (13.2.2)

    I'm encountering an issue with user authentication on my Umbraco website, running version 13.2.2. I'm attempting to create protected pages that require users to login, but despite configuring the login form and controller, authentication doesn't seem to be working as expected.

    Here's the setup:

    Login View (Login.cshtml):

    @inherits Umbraco.Cms.Web.Common.Views.UmbracoViewPage
    @{
        Layout = null;
    }
    
    <!DOCTYPE html>
    <html>
    <head>
        <title>Login</title>
    </head>
    <body>
        <h1>Login</h1>
        <form method="post" action="@Url.Action("HandleLogin", "Member")">
            <div>
                <label for="username">Username:</label>
                <input type="text" id="username" name="username" required />
            </div>
            <div>
                <label for="password">Password:</label>
                <input type="password" id="password" name="password" required />
            </div>
            <button type="submit">Login</button>
        </form>
    
        @if (ViewBag.LoginError != null)
        {
            <p style="color:red">@ViewBag.LoginError</p>
        }
    </body>
    </html>
    

    Controller (MemberController.cs):

    public class MemberController : SurfaceController
    {
        private readonly IMemberSignInManager _memberSignInManager;
    
        public MemberController(IUmbracoContextAccessor umbracoContextAccessor,
                                IUmbracoDatabaseFactory databaseFactory,
                                ServiceContext services,
                                AppCaches appCaches,
                                IProfilingLogger profilingLogger,
                                IPublishedUrlProvider publishedUrlProvider,
                                IMemberSignInManager memberSignInManager)
            : base(umbracoContextAccessor, databaseFactory, services, appCaches, profilingLogger, publishedUrlProvider)
        {
            _memberSignInManager = memberSignInManager;
        }
    
        [HttpPost]
        public IActionResult HandleLogin(string username, string password)
        {
            if (ModelState.IsValid)
            {
                var result = _memberSignInManager.PasswordSignInAsync(username, password, false, false).Result;
    
                if (result.Succeeded)
                {
                    return Redirect("/vat-search/");
                }
                else
                {
                    ViewBag.LoginError = "Invalid username or password";
                    return CurrentUmbracoPage();
                }
            }
    
            return CurrentUmbracoPage();
        }
    }
    

    When attempting to access protected pages, the login view is displayed as expected. However, upon submitting the login credentials, I'm redirected to:

    /umbraco/surface/member/HandleLogin
    

    Could anyone provide guidance on what additional steps might be necessary in terms of configuring Startup.cs or setting up a new route to ensure that user authentication works correctly?

    Any insights or suggestions would be greatly appreciated. Thank you!

  • Huw Reddick 1929 posts 6697 karma points MVP 2x c-trib
    Jun 12, 2024 @ 15:26
    Huw Reddick
    0

    if you debug your code, is the signin result succeeded?

  • Ayo Adesina 445 posts 1059 karma points
    Jun 12, 2024 @ 17:26
    Ayo Adesina
    0

    I’ll have to check when o get back to the office, but I’m pretty sure it’s not even getting to the controller, I think it’s something to do with routes, I’ll update this when I check properly with some break points.

    Thanks for your reply

Please Sign in or register to post replies

Write your reply to:

Draft