Umbraco 13 .Net Core Cookie Authentication
i

imadtbro

10 months ago
Hi everyone, I'm working on an Umbraco 13 project and have been trying to set up Cookie based authentication (https://learn.microsoft.com/en-us/aspnet/core/security/authentication/cookie?view=aspnetcore-8.0) We have an external data source being used for front-end user accounts, to which all I want to do is used the Cookie based authentication to control the authentication process. I found some older code in the Umbraco forums that lead me to this current set up of code: Image1 for my AddUserCookieAuthentication builder extension Image2 for how I add this extenstion in Startup.cs Image3 for adding the use Authentication to the app in Startup.cs Image4 is where my code for logging the user in. All of this works great. I can properly check if a user is logged in and authenticated using "HttpContext.User.Identity.IsAuthenticated" and obtain the data from the Claims. I just ran into an issue where if I am logged in on the frontend, the back office is broken for me. (I can still open an incognito window and login there and everything works fine) In the console I see this error:
Possibly unhandled rejection: The user object is invalid, the remainingAuthSeconds is required.
I'm pretty sure it has to do with how I'm using the cookie authentication. Is there any way to fix this? Or is there anyway to better setup the cookie authentication than how I am doing it currently? If anymore information is needed, please let me know and I can provide it! Thank you! https://cdn.discordapp.com/attachments/1305939609548492870/1305939609758470214/image.png?ex=6734da8a&is=6733890a&hm=43e0a7c12f81eed0e65cab3da0dae71e03d4f720cf3d7ce571719b10f29483df& https://cdn.discordapp.com/attachments/1305939609548492870/1305939610261520484/image.png?ex=6734da8a&is=6733890a&hm=6d2816d406f193c1c2c9ddd794c9db2d00e3fb234e5f47c8e2277e7012ea2c26& https://cdn.discordapp.com/attachments/1305939609548492870/1305939610530086973/image.png?ex=6734da8a&is=6733890a&hm=faea64d792ab91d041149de01e37786e136a4956126bee4d6aba6b094ba5e68f& https://cdn.discordapp.com/attachments/1305939609548492870/1305939610786074714/image.png?ex=6734da8a&is=6733890a&hm=7b63405b97a65717deb380f2c7c98522505d7057ab4b134a10be884c7fc6fc86&
Vite configuration using minimal hosting model
d

Dan

over 1 year ago
Hi, I'm using Vite to handle the front-end assets on a new site, and using Vite.AspNetCore for the middleware. This set-up works on a v13 project which was upgraded from v12 using both Startup.cs and Program.cs files, but doesn't on a brand new v13 project using the minimal hosting model. The Program.cs on the new site is:
using Vite.AspNetCore;
using Vite.AspNetCore.Extensions;

WebApplicationBuilder builder = WebApplication.CreateBuilder(args);

builder.CreateUmbracoBuilder()
    .AddBackOffice()
    .AddWebsite()
    .AddDeliveryApi()
    .AddComposers()
    .Build();

builder.Services.AddViteServices(new ViteOptions()
{
    Server = new ViteServerOptions
    {
        Port = 5173,
        Https = true,
    }
});

WebApplication app = builder.Build();

await app.BootUmbracoAsync();

app.UseHttpsRedirection();

app.UseUmbraco()
    .WithMiddleware(u =>
    {
        u.UseBackOffice();
        u.UseWebsite();
    })
    .WithEndpoints(u =>
    {
        u.UseInstallerEndpoints();
        u.UseBackOfficeEndpoints();
        u.UseWebsiteEndpoints();
    });

if (app.Environment.IsDevelopment())
{
    app.UseViteDevelopmentServer();
}

await app.RunAsync();
Everything else is pretty much the same (similar`package.json`, identical
vite.config.js
, same folder structure, same reserved paths [including
~/@vite/,~/@id/
] in
appsettings.Development.json
etc). The assets are output from Master.cshtml file like this:
<environment include="Development">
    <script type="module" defer src="~/@@vite/client"></script> <!-- Load Vite -->
    <script type="module" defer src="~/scripts/main.js"></script> <!-- Define the entry point -->
</environment>
However, when running the new site both
/@vite/client
and
/scripts/main.js
are throwing 404s. The middleware configuration (above) looks okay AFAIK and all else is very similar so I'm struggling to understand why one works and the other doesn't. Could anyone suggest what might be awry here?