Copied to clipboard

Flag this post as spam?

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


  • GianniDPC 14 posts 80 karma points
    Aug 10, 2022 @ 11:51
    GianniDPC
    0

    CookieAuthentication not working in website

    CookieAuthentication seems not to be working as expected in my Umbraco 10 project. I'm using Umbraco 10.1.0.

    It seems to be creating the Authentication Cookie but User.Identity.Name is always NULL.

    I added this line to ConfigureServices:

    services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
                .AddCookie(options =>
                {
                    options.LoginPath = "/Login";
                    options.Cookie.Name = "RubioAuthCookie";
                    options.ExpireTimeSpan = TimeSpan.FromMinutes(20);
                    options.SlidingExpiration = true;
                });
    

    and I added these lines to the Configure method:

    app.UseUmbraco()
                    .WithMiddleware(u =>
                    {
                        u.UseBackOffice();
                        u.UseWebsite();
    
                        u.AppBuilder.UseAuthentication();
                        u.AppBuilder.UseAuthorization();
    

    This is the code from within my Login action:

    [Route("/Account/Login")]
    public async Task<IActionResult> LoginAsync()
    {
        var claims = new List<Claim>
        {
            new Claim(ClaimTypes.Name, "Gianni"),
        };
    
        var identity = new ClaimsIdentity(claims,
            CookieAuthenticationDefaults.AuthenticationScheme);
    
        await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, new ClaimsPrincipal(identity), new AuthenticationProperties { IsPersistent = true});
    
        return Redirect("/");
    }
    

    What am I doing wrong or what could be causing this issue?

  • Sof 1 post 71 karma points
    Sep 15, 2022 @ 10:31
    Sof
    0

    Hello, I just came up with the same issue. Did you maybe find a solution to this problem?

  • Per Collin 1 post 21 karma points
    Oct 12, 2022 @ 08:22
    Per Collin
    0

    I'm also curious about a solution. Facing the same issue 🤔 Any news?

  • Gianni Declercq 8 posts 80 karma points
    Oct 12, 2022 @ 20:40
    Gianni Declercq
    2

    Below is some example code that should help you get started:

    Add AddUserCookieAuthentication(cookieName: "MyAuthCookie") after AddComposers and before Build() in ConfigureServices (Startup.cs). The code to make this method available is below.

    Also add these lines to app.UseUmbraco().WithMiddleware..

    u.AppBuilder.UseAuthentication();
    u.AppBuilder.UseAuthorization();
    

    Middleware..

    using Microsoft.AspNetCore.Authentication.Cookies;
    using Microsoft.Extensions.DependencyInjection;
    using Umbraco.Cms.Core.DependencyInjection;
    
    
    namespace MyProject.Extensions
    {
        public static class UmbracoBuilderExtensions
        {
            public static IUmbracoBuilder AddUserCookieAuthentication(this IUmbracoBuilder builder, string cookieName)
            {
                builder.AddMemberExternalLogins(logins =>
                {
                    logins.AddMemberLogin(
                        memberAuthenticationBuilder =>
                        {
                            string strSchemeName = CookieAuthenticationDefaults.AuthenticationScheme;
    
                            memberAuthenticationBuilder.AddCookie(strSchemeName, objCookieAuthenticationOptions =>
                            {
                                objCookieAuthenticationOptions.Cookie.Name = cookieName;
                                objCookieAuthenticationOptions.LoginPath = "/";
                            });
    
                            builder.Services.AddAuthentication(options =>
                            {
                                options.DefaultAuthenticateScheme = strSchemeName;
                            });
                            builder.Services.AddAuthorization();
                        });
                });
                return builder;
            }
        }
    }
    
  • imadtbro 2 posts 22 karma points
    1 week ago
    imadtbro
    0

    Hi Everyone!

    On my quest to add Cookie Authentication for some front end user systems, I found this thread and it has helped immensely! I was able to use the code suggested by Gianna Declercq above.

    Using the code I was able to user the .Net Core Identity system to properly create claims and log a user in and out, with the cookie being properly set.

    With that said, I have encountered another issue. When logging into the back office, when a front end user is logged in, the back office is broken, and I see this error in the console:

    Possibly unhandled rejection: The user object is invalid, the remainingAuthSeconds is required.
    

    I suspect it has something to do with how the front end cookie is interacting or being picked up by the back office, but I don't know what the solution is.

    Has anyone seen this issue?

    EDIT: We are using Umbraco 13.

  • GianniDPC 14 posts 80 karma points
    1 week ago
    GianniDPC
    0

    I'm not sure this is the solution but make sure when logging in or out you are using the right Authentication Scheme.

  • Huw Reddick 1929 posts 6697 karma points MVP 2x c-trib
    Oct 12, 2022 @ 08:26
  • Shelly 9 posts 79 karma points
    Oct 17, 2023 @ 11:46
    Shelly
    0

    Hello, you showed us how to add the authentication cookie, within the Configure method, but as I saw, when I put a debugger on it, it does not come to the break point. I understood from this link: https://cornehoskam.com/posts/umbraco-8-versus-9-dependency-injection, that startup.cs is not accessible, unless we write it as a class that implements Composer. Like this:

    enter image description here

    using IOCDocs.NotificationHandlers;
    using IOCDocs.Services;
    using Microsoft.Extensions.DependencyInjection;
    using Umbraco.Cms.Core.Composing;
    using Umbraco.Cms.Core.DependencyInjection;
    using Umbraco.Cms.Core.Notifications;
    namespace IOCDocs
    {
        public class MyComposer : IComposer
        {
            public void Compose(IUmbracoBuilder builder)
            {
                builder.AddNotificationHandler<ContentTypeSavedNotification, ContentTypeSavedHandler>();
                builder.Services.AddSingleton<IFooBar, Foobar>();
            }
        }
    }
    

    My question is, is there a similar way to add the authentication cookie in this manner? of Composers?

    Thanks!!

Please Sign in or register to post replies

Write your reply to:

Draft