[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?
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..
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.
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:
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?
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:
and I added these lines to the Configure method:
This is the code from within my Login action:
What am I doing wrong or what could be causing this issue?
Hello, I just came up with the same issue. Did you maybe find a solution to this problem?
I'm also curious about a solution. Facing the same issue 🤔 Any news?
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..
Middleware..
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:
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.
I'm not sure this is the solution but make sure when logging in or out you are using the right Authentication Scheme.
https://stackoverflow.com/questions/55128952/user-identity-name-is-null-in-cookie-authentication-in-net-core
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:
My question is, is there a similar way to add the authentication cookie in this manner? of Composers?
Thanks!!
is working on a reply...