Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AspNetCore 2.0 Claims always empty

I am working on converting a DotNet 4.5 MVC/WebAPI application to AspNetCore 2.0, and I'm having some trouble getting my Cookie authentication working again. When I set the cookie and try to access a secure method, I can't get there. When I go into an anonymous method and inspect the user object, it is empty - no authentication type, no claims, etc.

I have followed this article as best I can: https://docs.microsoft.com/en-us/aspnet/core/security/authentication/cookie?tabs=aspnetcore2x. I am not using Identity.

My code in startup.cs ConfigureServices is as follows:

  services.AddAuthentication("ACE_AUTH")                    
                    .AddCookie("ACE_AUTH",  options =>
                    {
                        options.AccessDeniedPath = "/Home/Index/";
                        options.LoginPath = "/Home/Index/";
                    });

My code in the Configure method:

app.UseAuthentication();

The Principal is fully populated when this is called. Where I am setting my cookie:

 await HttpContext.SignInAsync("ACE_AUTH", samlData.Principal);

Nothing I have tried has caused my claims to show up when attempting to Authenticate the user.

like image 385
Danny Ellis Jr. Avatar asked Aug 28 '17 17:08

Danny Ellis Jr.


People also ask

What is a Claims principal?

A claims principal has a collection of ClaimsIdentity objects that is accessible through the Identities property. Each ClaimsIdentity in the collection contains one or more claims. The Claims property returns all of the claims from all of the claims identities in this collection.

What is ClaimsIdentity in asp net core?

In . NET Core, the ClaimsIdentity class represents a user in your application. It helps describe who they are and helps manage the list of claims which describe what they can do.

What is Aspnetcore identity?

ASP.NET Core Identity: Is an API that supports user interface (UI) login functionality. Manages users, passwords, profile data, roles, claims, tokens, email confirmation, and more.


Video Answer


1 Answers

For people not reading comments (almost read over it myself):

Joey: "What solved the issue for me was moving app.UseAuthentication above app.UseMvc. It does say that in the docs but it is well hidden."

... This works... Why it doesn't throw an Exception when you do it afterwards is behind me.

like image 156
Dirk Boer Avatar answered Sep 29 '22 12:09

Dirk Boer